Skip to content

Commit e691941

Browse files
authored
Fix __stdio_close to correctly set errno (#22046)
Fixes: #22033
1 parent a6e1f5c commit e691941

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

system/lib/libc/musl/src/stdio/__stdio_close.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ weak_alias(dummy, __aio_close);
1111
int __stdio_close(FILE *f)
1212
{
1313
#ifdef __EMSCRIPTEN__
14-
return __wasi_fd_close(__aio_close(f->fd));
14+
return __wasi_syscall_ret(__wasi_fd_close(__aio_close(f->fd)));
1515
#else
1616
return syscall(SYS_close, __aio_close(f->fd));
1717
#endif

test/test_files.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,26 @@
44
// found in the LICENSE file.
55

66
#include <assert.h>
7+
#include <errno.h>
78
#include <fcntl.h>
89
#include <stdio.h>
910
#include <stdlib.h>
1011
#include <string.h>
1112
#include <sys/stat.h>
1213
#include <unistd.h>
1314

15+
void test_fclose() {
16+
FILE* f = fopen("temp.txt", "w");
17+
int fd = fileno(f);
18+
// Close the underlying FD, which should then cause fclose to
19+
// fail.
20+
int ret = close(fd);
21+
assert(ret == 0);
22+
ret = fclose(f);
23+
printf("fclose error: %s\n", strerror(errno));
24+
assert(ret == EOF);
25+
}
26+
1427
// Reading
1528
void test_reading() {
1629
FILE *file = fopen("somefile.binary", "rb");
@@ -161,6 +174,7 @@ void test_tempfiles() {
161174
}
162175

163176
int main() {
177+
test_fclose();
164178
test_reading();
165179
test_stdstreams();
166180
test_writing();

0 commit comments

Comments
 (0)