Skip to content

Commit

Permalink
Restore line in newlocale.c that was lost in emscripten-core#4813 (em…
Browse files Browse the repository at this point in the history
…scripten-core#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 emscripten-core#4813 is that
the musl older version of musl we were using prior to emscripten-core#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
  • Loading branch information
sbc100 authored Mar 3, 2021
1 parent d710e2d commit 78fa15d
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 4 deletions.
3 changes: 3 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ See docs/process.md for more on how version tagging works.

Current Trunk
-------------
- Calls to `newlocale` (and `new std::locale` in C++) with arbirary names will
now succeed. This is the behaviour of musl libc which emscripten had
previously inadvertently disabled.
- System libraries are now compiled with debug info (`-g`). This doesn't
affect release builds (builds without `-g`) but allows DWARF debugging of
types defined in system libraries such as C++ STL types (#13078).
Expand Down
2 changes: 2 additions & 0 deletions system/lib/libc/musl/src/locale/newlocale.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ locale_t __newlocale(int mask, const char *name, locale_t loc)
if (j==1 && tmp.cat[LC_CTYPE]==&__c_dot_utf8)
return UTF8_LOCALE;

if ((loc = malloc(sizeof *loc))) *loc = tmp;

return loc;
}

Expand Down
File renamed without changes.
File renamed without changes.
16 changes: 16 additions & 0 deletions tests/core/test_newlocale.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <locale.h>
#include <stdio.h>

void test(const char* name) {
locale_t loc = newlocale(LC_ALL_MASK, name, 0);
if (loc)
printf("newlocale '%s' succeeded\n", name);
else
printf("newlocale '%s' failed\n", name);
}

int main(int argc, char* argv[]) {
test("C");
test("waka");
return 0;
}
2 changes: 2 additions & 0 deletions tests/core/test_newlocale.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
newlocale 'C' succeeded
newlocale 'waka' succeeded
11 changes: 11 additions & 0 deletions tests/core/test_setlocale.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <stdio.h>
#include <locale.h>

int main(int argc, char* argv[]) {
char* rtn;
rtn = setlocale(LC_ALL, "C");
printf("done setlocale 'C': '%s'\n", rtn);
rtn = setlocale(LC_ALL, "waka");
printf("done setlocale 'waka': '%s'\n", rtn);
return 0;
}
2 changes: 2 additions & 0 deletions tests/core/test_setlocale.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
done setlocale 'C': 'C;C;C;C;C;C'
done setlocale 'waka': 'waka;waka;waka;waka;waka;waka'
10 changes: 8 additions & 2 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7295,8 +7295,14 @@ def test_noexitruntime(self):
def test_minmax(self):
self.do_runf(path_from_root('tests', 'test_minmax.c'), 'NAN != NAN\nSuccess!')

def test_locale(self):
self.do_run_in_out_file_test('tests', 'test_locale.c')
def test_localeconv(self):
self.do_run_in_out_file_test('tests', 'core', 'test_localeconv.c')

def test_newlocale(self):
self.do_run_in_out_file_test('tests', 'core', 'test_newlocale.c')

def test_setlocale(self):
self.do_run_in_out_file_test('tests', 'core', 'test_setlocale.c')

def test_vswprintf_utf8(self):
self.do_run_in_out_file_test('tests', 'vswprintf_utf8.c')
Expand Down
12 changes: 10 additions & 2 deletions tests/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -4679,8 +4679,16 @@ def test_locale_wrong(self):
}
''')
self.run_process([EMCC, 'src.cpp', '-s', 'EXIT_RUNTIME', '-s', 'DISABLE_EXCEPTION_CATCHING=0'])
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']))
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))
self.assertContained('''\
Constructed locale "C"
This locale is the global locale.
This locale is the C locale.
''', self.run_js('a.out.js', args=['C']))
self.assertContained('''\
Constructed locale "waka"
This locale is not the global locale.
This locale is not the C locale.
''', self.run_js('a.out.js', args=['waka']))

def test_cleanup_os(self):
# issue 2644
Expand Down

0 comments on commit 78fa15d

Please sign in to comment.