Skip to content

Commit 99028a8

Browse files
authored
Fix crash in getifaddrs (#24364)
We don't support `AF_NETLINK` sockets so just fail early here. Replaces #24187
1 parent 2437331 commit 99028a8

File tree

4 files changed

+24
-0
lines changed

4 files changed

+24
-0
lines changed

src/lib/libsockfs.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,15 @@ addToLibrary({
4444
return FS.createNode(null, '/', {{{ cDefs.S_IFDIR | 0o777 }}}, 0);
4545
},
4646
createSocket(family, type, protocol) {
47+
// Emscripten only supports AF_INET
48+
if (family != {{{ cDefs.AF_INET }}}) {
49+
throw new FS.ErrnoError({{{ cDefs.EAFNOSUPPORT }}});
50+
}
4751
type &= ~{{{ cDefs.SOCK_CLOEXEC | cDefs.SOCK_NONBLOCK }}}; // Some applications may pass it; it makes no sense for a single process.
52+
// Emscripten only supports SOCK_STREAM and SOCK_DGRAM
53+
if (type != {{{ cDefs.SOCK_STREAM }}} && type != {{{ cDefs.SOCK_DGRAM }}}) {
54+
throw new FS.ErrnoError({{{ cDefs.EINVAL }}});
55+
}
4856
var streaming = type == {{{ cDefs.SOCK_STREAM }}};
4957
if (streaming && protocol && protocol != {{{ cDefs.IPPROTO_TCP }}}) {
5058
throw new FS.ErrnoError({{{ cDefs.EPROTONOSUPPORT }}}); // if SOCK_STREAM, must be tcp or 0.

test/other/test_getifaddrs.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <sys/types.h>
2+
#include <ifaddrs.h>
3+
#include <errno.h>
4+
#include <string.h>
5+
#include <stdio.h>
6+
7+
int main() {
8+
struct ifaddrs* ifaddrs = NULL;
9+
int rtn = getifaddrs(&ifaddrs);
10+
printf("getifaddrs => %d (%s)\n", rtn, strerror(errno));
11+
return 0;
12+
}

test/other/test_getifaddrs.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
getifaddrs => -1 (Address family not supported by protocol)

test/test_other.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16092,3 +16092,6 @@ def test_em_js_bool_macro_expansion(self):
1609216092
}
1609316093
''')
1609416094
self.do_runf('main.c')
16095+
16096+
def test_getifaddrs(self):
16097+
self.do_other_test('test_getifaddrs.c')

0 commit comments

Comments
 (0)