Skip to content

Commit 78fa15d

Browse files
authored
Restore line in newlocale.c that was lost in #4813 (#13584)
When musl was last upgraded this malloc line removed from newlocale.c, perhaps by accident. This restores the behaviour of musl which is to allow `newlocale` with arbitrary names to succeed. I verified that this is the case using musl on my desktop. The reason that test_locale_wrong was passing prior to #4813 is that the musl older version of musl we were using prior to #4813 rejected all locales except for "C" and "POSIX". Upstream version that we are currently based on: https://github.com/emscripten-core/musl/blob/v1.1.15/src/locale/newlocale.c#L44 Upstream version of musl that we were previously using: https://github.com/emscripten-core/musl/blob/0b44a0315b47dd8eced9f3b7f31580cf14bbfc01/src/locale/newlocale.c#L5
1 parent d710e2d commit 78fa15d

File tree

10 files changed

+54
-4
lines changed

10 files changed

+54
-4
lines changed

ChangeLog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ See docs/process.md for more on how version tagging works.
2020

2121
Current Trunk
2222
-------------
23+
- Calls to `newlocale` (and `new std::locale` in C++) with arbirary names will
24+
now succeed. This is the behaviour of musl libc which emscripten had
25+
previously inadvertently disabled.
2326
- System libraries are now compiled with debug info (`-g`). This doesn't
2427
affect release builds (builds without `-g`) but allows DWARF debugging of
2528
types defined in system libraries such as C++ STL types (#13078).

system/lib/libc/musl/src/locale/newlocale.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ locale_t __newlocale(int mask, const char *name, locale_t loc)
4141
if (j==1 && tmp.cat[LC_CTYPE]==&__c_dot_utf8)
4242
return UTF8_LOCALE;
4343

44+
if ((loc = malloc(sizeof *loc))) *loc = tmp;
45+
4446
return loc;
4547
}
4648

File renamed without changes.
File renamed without changes.

tests/core/test_newlocale.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <locale.h>
2+
#include <stdio.h>
3+
4+
void test(const char* name) {
5+
locale_t loc = newlocale(LC_ALL_MASK, name, 0);
6+
if (loc)
7+
printf("newlocale '%s' succeeded\n", name);
8+
else
9+
printf("newlocale '%s' failed\n", name);
10+
}
11+
12+
int main(int argc, char* argv[]) {
13+
test("C");
14+
test("waka");
15+
return 0;
16+
}

tests/core/test_newlocale.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
newlocale 'C' succeeded
2+
newlocale 'waka' succeeded

tests/core/test_setlocale.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <stdio.h>
2+
#include <locale.h>
3+
4+
int main(int argc, char* argv[]) {
5+
char* rtn;
6+
rtn = setlocale(LC_ALL, "C");
7+
printf("done setlocale 'C': '%s'\n", rtn);
8+
rtn = setlocale(LC_ALL, "waka");
9+
printf("done setlocale 'waka': '%s'\n", rtn);
10+
return 0;
11+
}

tests/core/test_setlocale.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
done setlocale 'C': 'C;C;C;C;C;C'
2+
done setlocale 'waka': 'waka;waka;waka;waka;waka;waka'

tests/test_core.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7295,8 +7295,14 @@ def test_noexitruntime(self):
72957295
def test_minmax(self):
72967296
self.do_runf(path_from_root('tests', 'test_minmax.c'), 'NAN != NAN\nSuccess!')
72977297

7298-
def test_locale(self):
7299-
self.do_run_in_out_file_test('tests', 'test_locale.c')
7298+
def test_localeconv(self):
7299+
self.do_run_in_out_file_test('tests', 'core', 'test_localeconv.c')
7300+
7301+
def test_newlocale(self):
7302+
self.do_run_in_out_file_test('tests', 'core', 'test_newlocale.c')
7303+
7304+
def test_setlocale(self):
7305+
self.do_run_in_out_file_test('tests', 'core', 'test_setlocale.c')
73007306

73017307
def test_vswprintf_utf8(self):
73027308
self.do_run_in_out_file_test('tests', 'vswprintf_utf8.c')

tests/test_other.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4679,8 +4679,16 @@ def test_locale_wrong(self):
46794679
}
46804680
''')
46814681
self.run_process([EMCC, 'src.cpp', '-s', 'EXIT_RUNTIME', '-s', 'DISABLE_EXCEPTION_CATCHING=0'])
4682-
self.assertContained('Constructed locale "C"\nThis locale is the global locale.\nThis locale is the C locale.', self.run_js('a.out.js', args=['C']))
4683-
self.assertContained('''Can't construct locale "waka": collate_byname<char>::collate_byname failed to construct for waka''', self.run_js('a.out.js', args=['waka'], assert_returncode=1))
4682+
self.assertContained('''\
4683+
Constructed locale "C"
4684+
This locale is the global locale.
4685+
This locale is the C locale.
4686+
''', self.run_js('a.out.js', args=['C']))
4687+
self.assertContained('''\
4688+
Constructed locale "waka"
4689+
This locale is not the global locale.
4690+
This locale is not the C locale.
4691+
''', self.run_js('a.out.js', args=['waka']))
46844692

46854693
def test_cleanup_os(self):
46864694
# issue 2644

0 commit comments

Comments
 (0)