Skip to content

Commit f5f4ff8

Browse files
authored
Fix conditional expression in musl's dup2. NFC. (#13740)
* Fix conditional expression in musl's dup2. NFC. The if/else branch of the conditional expression was reversed. * Use the appropriate error code 0 for success and -1 to indicate an error. Add a test case to catch this (this previously failed when `SYS_dup2` was `#undef`'d).
1 parent 1cfce09 commit f5f4ff8

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

system/lib/libc/musl/src/unistd/dup2.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ int dup2(int old, int new)
1313
while ((r=__syscall(SYS_dup2, old, new))==-EBUSY);
1414
#else
1515
if (old==new) {
16-
#if __EMSCRIPTEN__
17-
r = __syscall(SYS_fcntl, old, F_GETFD);
16+
#ifdef __EMSCRIPTEN__
17+
r = __wasi_fd_is_valid(old) ? 0 : -1;
1818
#else
19-
r = __wasi_fd_is_valid(old);
19+
r = __syscall(SYS_fcntl, old, F_GETFD);
2020
#endif
2121
if (r >= 0) return old;
2222
} else {

tests/unistd/dup.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,12 @@ int main() {
3939
printf("\n");
4040
errno = 0;
4141

42+
printf("DUP2 err\n");
43+
f = dup2(-2, -2);
44+
printf("f: %d\n", f == -1);
45+
printf("errno: %d\n", errno);
46+
printf("close(f): %d\n", close(f));
47+
errno = 0;
48+
4249
return 0;
4350
}

tests/unistd/dup.out

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,8 @@ f2,f3: 1
1313
close(f1): 0
1414
close(f2): 0
1515
close(f3): -1
16+
17+
DUP2 err
18+
f: 1
19+
errno: 8
20+
close(f): -1

0 commit comments

Comments
 (0)