Skip to content

Commit e0163ab

Browse files
authored
Add support for reallocarray libc function (#24775)
Fixes: #24771
1 parent 8caba6e commit e0163ab

File tree

6 files changed

+51
-2
lines changed

6 files changed

+51
-2
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#define _BSD_SOURCE
2+
#include <errno.h>
3+
#include <stdlib.h>
4+
5+
#ifdef __EMSCRIPTEN__
6+
/* Must be weak so that lsan can override */
7+
weak
8+
#endif
9+
void *reallocarray(void *ptr, size_t m, size_t n)
10+
{
11+
if (n && m > -1 / n) {
12+
errno = ENOMEM;
13+
return 0;
14+
}
15+
16+
return realloc(ptr, m * n);
17+
}

test/code_size/test_codesize_hello_dylink_all.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"a.out.js": 246735,
3-
"a.out.nodebug.wasm": 597789,
4-
"total": 844524,
3+
"a.out.nodebug.wasm": 597855,
4+
"total": 844590,
55
"sent": [
66
"IMG_Init",
77
"IMG_Load",
@@ -2962,6 +2962,7 @@
29622962
"readv",
29632963
"realloc",
29642964
"realloc_in_place",
2965+
"reallocarray",
29652966
"realpath",
29662967
"recv",
29672968
"recvfrom",
@@ -4656,6 +4657,7 @@
46564657
"$readlink",
46574658
"$readlinkat",
46584659
"$readv",
4660+
"$reallocarray",
46594661
"$realpath",
46604662
"$recv",
46614663
"$recvfrom",

test/other/test_reallocarray.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <assert.h>
2+
#include <stdlib.h>
3+
#include <stdio.h>
4+
#include <string.h>
5+
6+
int main(void) {
7+
char* p = calloc(4, sizeof(char));
8+
assert(p);
9+
10+
strcpy(p, "foo");
11+
puts(p);
12+
13+
p = reallocarray(p, 32, sizeof(char));
14+
assert(p);
15+
16+
strcpy(p, "foobar");
17+
puts(p);
18+
19+
free(p);
20+
return 0;
21+
}

test/other/test_reallocarray.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
foo
2+
foobar

test/test_other.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16395,3 +16395,6 @@ def test_TextDecoder_invalid(self):
1639516395

1639616396
err = self.expect_fail([EMCC, test_file('hello_world.c'), '-sTEXTDECODER=3'])
1639716397
self.assertContained('#error "TEXTDECODER must be either 1 or 2"', err)
16398+
16399+
def test_reallocarray(self):
16400+
self.do_other_test('test_reallocarray.c')

tools/system_libs.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,6 +1294,10 @@ def get_files(self):
12941294
path='system/lib/libc/musl/src/linux',
12951295
filenames=['getdents.c', 'gettid.c', 'utimes.c', 'statx.c', 'wait4.c', 'wait3.c'])
12961296

1297+
libc_files += files_in_path(
1298+
path='system/lib/libc/musl/src/malloc',
1299+
filenames=['reallocarray.c'])
1300+
12971301
libc_files += files_in_path(
12981302
path='system/lib/libc/musl/src/sched',
12991303
filenames=['sched_yield.c'])

0 commit comments

Comments
 (0)