From 78706d25d0897315f33169511431960e2dba0941 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Fri, 31 May 2024 22:55:22 +0000 Subject: [PATCH] : warn on some unused results. This is fairly conservative, touching only those functions (such as feof()) where it's clearly an error to not use the return value. Also fix a test that was ignoring the return value of feof() (because it was just checking whether the function could take the lock, and genuinely didn't care about the result). Change-Id: If2ade10ae87df45a8b9bfcb24828e460201fa9a1 --- libc/include/stdio.h | 54 ++++++++++++++++++++++---------------------- tests/stdio_test.cpp | 9 +++++--- 2 files changed, 33 insertions(+), 30 deletions(-) diff --git a/libc/include/stdio.h b/libc/include/stdio.h index 78114c38c..279c6584a 100644 --- a/libc/include/stdio.h +++ b/libc/include/stdio.h @@ -105,10 +105,10 @@ extern FILE __sF[] __REMOVED_IN(23, "Use stdin/stdout/stderr"); void clearerr(FILE* _Nonnull __fp); int fclose(FILE* _Nonnull __fp); -int feof(FILE* _Nonnull __fp); -int ferror(FILE* _Nonnull __fp); +__wur int feof(FILE* _Nonnull __fp); +__wur int ferror(FILE* _Nonnull __fp); int fflush(FILE* _Nullable __fp); -int fgetc(FILE* _Nonnull __fp); +__wur int fgetc(FILE* _Nonnull __fp); char* _Nullable fgets(char* _Nonnull __buf, int __size, FILE* _Nonnull __fp); int fprintf(FILE* _Nonnull __fp , const char* _Nonnull __fmt, ...) __printflike(2, 3); int fputc(int __ch, FILE* _Nonnull __fp); @@ -116,8 +116,8 @@ int fputs(const char* _Nonnull __s, FILE* _Nonnull __fp); size_t fread(void* _Nonnull __buf, size_t __size, size_t __count, FILE* _Nonnull __fp); int fscanf(FILE* _Nonnull __fp, const char* _Nonnull __fmt, ...) __scanflike(2, 3); size_t fwrite(const void* _Nonnull __buf, size_t __size, size_t __count, FILE* _Nonnull __fp); -int getc(FILE* _Nonnull __fp); -int getchar(void); +__wur int getc(FILE* _Nonnull __fp); +__wur int getchar(void); ssize_t getdelim(char* _Nullable * _Nonnull __line_ptr, size_t* _Nonnull __line_length_ptr, int __delimiter, FILE* _Nonnull __fp); ssize_t getline(char* _Nullable * _Nonnull __line_ptr, size_t* _Nonnull __line_length_ptr, FILE* _Nonnull __fp); @@ -201,17 +201,17 @@ int renameat2(int __old_dir_fd, const char* _Nonnull __old_path, int __new_dir_f #endif int fseek(FILE* _Nonnull __fp, long __offset, int __whence); -long ftell(FILE* _Nonnull __fp); +__wur long ftell(FILE* _Nonnull __fp); /* See https://android.googlesource.com/platform/bionic/+/main/docs/32-bit-abi.md */ #if defined(__USE_FILE_OFFSET64) int fgetpos(FILE* _Nonnull __fp, fpos_t* _Nonnull __pos) __RENAME(fgetpos64) __INTRODUCED_IN(24); int fsetpos(FILE* _Nonnull __fp, const fpos_t* _Nonnull __pos) __RENAME(fsetpos64) __INTRODUCED_IN(24); int fseeko(FILE* _Nonnull __fp, off_t __offset, int __whence) __RENAME(fseeko64) __INTRODUCED_IN(24); -off_t ftello(FILE* _Nonnull __fp) __RENAME(ftello64) __INTRODUCED_IN(24); +__wur off_t ftello(FILE* _Nonnull __fp) __RENAME(ftello64) __INTRODUCED_IN(24); # if defined(__USE_BSD) /* If __read_fn and __write_fn are both nullptr, it will cause EINVAL */ -FILE* _Nullable funopen(const void* _Nullable __cookie, +__wur FILE* _Nullable funopen(const void* _Nullable __cookie, int (* __BIONIC_COMPLICATED_NULLNESS __read_fn)(void* _Nonnull, char* _Nonnull, int), int (* __BIONIC_COMPLICATED_NULLNESS __write_fn)(void* _Nonnull, const char* _Nonnull, int), fpos_t (* _Nullable __seek_fn)(void* _Nonnull, fpos_t, int), @@ -221,10 +221,10 @@ FILE* _Nullable funopen(const void* _Nullable __cookie, int fgetpos(FILE* _Nonnull __fp, fpos_t* _Nonnull __pos); int fsetpos(FILE* _Nonnull __fp, const fpos_t* _Nonnull __pos); int fseeko(FILE* _Nonnull __fp, off_t __offset, int __whence); -off_t ftello(FILE* _Nonnull __fp); +__wur off_t ftello(FILE* _Nonnull __fp); # if defined(__USE_BSD) /* If __read_fn and __write_fn are both nullptr, it will cause EINVAL */ -FILE* _Nullable funopen(const void* _Nullable __cookie, +__wur FILE* _Nullable funopen(const void* _Nullable __cookie, int (* __BIONIC_COMPLICATED_NULLNESS __read_fn)(void* _Nonnull, char* _Nonnull, int), int (* __BIONIC_COMPLICATED_NULLNESS __write_fn)(void* _Nonnull, const char* _Nonnull, int), fpos_t (* _Nullable __seek_fn)(void* _Nonnull, fpos_t, int), @@ -234,22 +234,22 @@ FILE* _Nullable funopen(const void* _Nullable __cookie, int fgetpos64(FILE* _Nonnull __fp, fpos64_t* _Nonnull __pos) __INTRODUCED_IN(24); int fsetpos64(FILE* _Nonnull __fp, const fpos64_t* _Nonnull __pos) __INTRODUCED_IN(24); int fseeko64(FILE* _Nonnull __fp, off64_t __offset, int __whence) __INTRODUCED_IN(24); -off64_t ftello64(FILE* _Nonnull __fp) __INTRODUCED_IN(24); +__wur off64_t ftello64(FILE* _Nonnull __fp) __INTRODUCED_IN(24); #if defined(__USE_BSD) /* If __read_fn and __write_fn are both nullptr, it will cause EINVAL */ -FILE* _Nullable funopen64(const void* _Nullable __cookie, +__wur FILE* _Nullable funopen64(const void* _Nullable __cookie, int (* __BIONIC_COMPLICATED_NULLNESS __read_fn)(void* _Nonnull, char* _Nonnull, int), int (* __BIONIC_COMPLICATED_NULLNESS __write_fn)(void* _Nonnull, const char* _Nonnull, int), fpos64_t (* _Nullable __seek_fn)(void* _Nonnull, fpos64_t, int), int (* _Nullable __close_fn)(void* _Nonnull)) __INTRODUCED_IN(24); #endif -FILE* _Nullable fopen(const char* _Nonnull __path, const char* _Nonnull __mode); -FILE* _Nullable fopen64(const char* _Nonnull __path, const char* _Nonnull __mode) __INTRODUCED_IN(24); +__wur FILE* _Nullable fopen(const char* _Nonnull __path, const char* _Nonnull __mode); +__wur FILE* _Nullable fopen64(const char* _Nonnull __path, const char* _Nonnull __mode) __INTRODUCED_IN(24); FILE* _Nullable freopen(const char* _Nullable __path, const char* _Nonnull __mode, FILE* _Nonnull __fp); FILE* _Nullable freopen64(const char* _Nullable __path, const char* _Nonnull __mode, FILE* _Nonnull __fp) __INTRODUCED_IN(24); -FILE* _Nullable tmpfile(void); -FILE* _Nullable tmpfile64(void) __INTRODUCED_IN(24); +__wur FILE* _Nullable tmpfile(void); +__wur FILE* _Nullable tmpfile64(void) __INTRODUCED_IN(24); int snprintf(char* __BIONIC_COMPLICATED_NULLNESS __buf, size_t __size, const char* _Nonnull __fmt, ...) __printflike(3, 4); int vfscanf(FILE* _Nonnull __fp, const char* _Nonnull __fmt, va_list __args) __scanflike(2, 0); @@ -260,20 +260,20 @@ int vsscanf(const char* _Nonnull __s, const char* _Nonnull __fmt, va_list __args #define L_ctermid 1024 /* size for ctermid() */ char* _Nonnull ctermid(char* _Nullable __buf) __INTRODUCED_IN(26); -FILE* _Nullable fdopen(int __fd, const char* _Nonnull __mode); -int fileno(FILE* _Nonnull __fp); +__wur FILE* _Nullable fdopen(int __fd, const char* _Nonnull __mode); +__wur int fileno(FILE* _Nonnull __fp); int pclose(FILE* _Nonnull __fp); -FILE* _Nullable popen(const char* _Nonnull __command, const char* _Nonnull __mode); +__wur FILE* _Nullable popen(const char* _Nonnull __command, const char* _Nonnull __mode); void flockfile(FILE* _Nonnull __fp); int ftrylockfile(FILE* _Nonnull __fp); void funlockfile(FILE* _Nonnull __fp); -int getc_unlocked(FILE* _Nonnull __fp); -int getchar_unlocked(void); +__wur int getc_unlocked(FILE* _Nonnull __fp); +__wur int getchar_unlocked(void); int putc_unlocked(int __ch, FILE* _Nonnull __fp); int putchar_unlocked(int __ch); -FILE* _Nullable fmemopen(void* _Nullable __buf, size_t __size, const char* _Nonnull __mode) __INTRODUCED_IN(23); -FILE* _Nullable open_memstream(char* _Nonnull * _Nonnull __ptr, size_t* _Nonnull __size_ptr) __INTRODUCED_IN(23); +__wur FILE* _Nullable fmemopen(void* _Nullable __buf, size_t __size, const char* _Nonnull __mode) __INTRODUCED_IN(23); +__wur FILE* _Nullable open_memstream(char* _Nonnull * _Nonnull __ptr, size_t* _Nonnull __size_ptr) __INTRODUCED_IN(23); #if defined(__USE_BSD) || defined(__BIONIC__) /* Historically bionic exposed these. */ int asprintf(char* _Nullable * _Nonnull __s_ptr, const char* _Nonnull __fmt, ...) __printflike(2, 3); @@ -283,16 +283,16 @@ void setbuffer(FILE* _Nonnull __fp, char* _Nullable __buf, int __size); int setlinebuf(FILE* _Nonnull __fp); int vasprintf(char* _Nullable * _Nonnull __s_ptr, const char* _Nonnull __fmt, va_list __args) __printflike(2, 0); void clearerr_unlocked(FILE* _Nonnull __fp) __INTRODUCED_IN(23); -int feof_unlocked(FILE* _Nonnull __fp) __INTRODUCED_IN(23); -int ferror_unlocked(FILE* _Nonnull __fp) __INTRODUCED_IN(23); -int fileno_unlocked(FILE* _Nonnull __fp) __INTRODUCED_IN(24); +__wur int feof_unlocked(FILE* _Nonnull __fp) __INTRODUCED_IN(23); +__wur int ferror_unlocked(FILE* _Nonnull __fp) __INTRODUCED_IN(23); +__wur int fileno_unlocked(FILE* _Nonnull __fp) __INTRODUCED_IN(24); #define fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0) #define fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0) #endif #if defined(__USE_BSD) int fflush_unlocked(FILE* _Nullable __fp) __INTRODUCED_IN(28); -int fgetc_unlocked(FILE* _Nonnull __fp) __INTRODUCED_IN(28); +__wur int fgetc_unlocked(FILE* _Nonnull __fp) __INTRODUCED_IN(28); int fputc_unlocked(int __ch, FILE* _Nonnull __fp) __INTRODUCED_IN(28); size_t fread_unlocked(void* _Nonnull __buf, size_t __size, size_t __count, FILE* _Nonnull __fp) __INTRODUCED_IN(28); size_t fwrite_unlocked(const void* _Nonnull __buf, size_t __size, size_t __count, FILE* _Nonnull __fp) __INTRODUCED_IN(28); diff --git a/tests/stdio_test.cpp b/tests/stdio_test.cpp index 5cb634c5b..7cdfa425a 100644 --- a/tests/stdio_test.cpp +++ b/tests/stdio_test.cpp @@ -121,9 +121,10 @@ static void AssertFileIs(FILE* fp, const char* expected, bool is_fmemopen = fals EXPECT_SWPRINTF_N(expected, static_cast(wcslen(expected)), fmt __VA_OPT__(, ) __VA_ARGS__) TEST(STDIO_TEST, flockfile_18208568_stderr) { - // Check that we have a _recursive_ mutex for flockfile. flockfile(stderr); - feof(stderr); // We don't care about the result, but this needs to take the lock. + // Check that we're using a _recursive_ mutex for flockfile() by calling + // something that will take the lock. + ASSERT_EQ(0, feof(stderr)); funlockfile(stderr); } @@ -132,7 +133,9 @@ TEST(STDIO_TEST, flockfile_18208568_regular) { FILE* fp = fopen("/dev/null", "w"); ASSERT_TRUE(fp != nullptr); flockfile(fp); - feof(fp); + // Check that we're using a _recursive_ mutex for flockfile() by calling + // something that will take the lock. + ASSERT_EQ(0, feof(fp)); funlockfile(fp); fclose(fp); }