Skip to content

Commit df17bb0

Browse files
committed
Add support for reallocarray libc function
Fixes: #24771
1 parent c75db03 commit df17bb0

File tree

5 files changed

+43
-0
lines changed

5 files changed

+43
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#define _BSD_SOURCE
2+
#include <errno.h>
3+
#include <stdlib.h>
4+
5+
void *reallocarray(void *ptr, size_t m, size_t n)
6+
{
7+
if (n && m > -1 / n) {
8+
errno = ENOMEM;
9+
return 0;
10+
}
11+
12+
return realloc(ptr, m * n);
13+
}

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
@@ -16399,3 +16399,6 @@ def test_TextDecoder_invalid(self):
1639916399

1640016400
err = self.expect_fail([EMCC, test_file('hello_world.c'), '-sTEXTDECODER=3'])
1640116401
self.assertContained('#error "TEXTDECODER must be either 1 or 2"', err)
16402+
16403+
def test_reallocarray(self):
16404+
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)