From e1833e54a76a6fdfda845c22412e68bcdf3e264a Mon Sep 17 00:00:00 2001 From: zijunzhao Date: Wed, 26 Apr 2023 21:43:30 +0000 Subject: [PATCH] Set __BIONIC_COMPLICATED_NULLNESS for the vsnprintf family When annotating the netinet directory aosp/2552567, we realize the argment s for vsnprintf family can be null only if the buffer size is 0. So we correct them and add some tests to verify our assumption. Bugs: b/245972273 Test: adb shell Change-Id: I51063286272be0daee0d7c1453a374b1f5674481 --- libc/include/stdio.h | 8 ++++---- tests/fortify_test.cpp | 4 ++++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/libc/include/stdio.h b/libc/include/stdio.h index e748faaae..d7b65e486 100644 --- a/libc/include/stdio.h +++ b/libc/include/stdio.h @@ -143,9 +143,9 @@ int vdprintf(int __fd, const char* _Nonnull __fmt, va_list __args) __printflike( (defined(__cplusplus) && __cplusplus <= 201103L) char* _Nullable gets(char* _Nonnull __buf) __attribute__((deprecated("gets is unsafe, use fgets instead"))); #endif -int sprintf(char* _Nonnull __s, const char* _Nonnull __fmt, ...) +int sprintf(char* __BIONIC_COMPLICATED_NULLNESS __s, const char* _Nonnull __fmt, ...) __printflike(2, 3) __warnattr_strict("sprintf is often misused; please use snprintf"); -int vsprintf(char* _Nonnull __s, const char* _Nonnull __fmt, va_list __args) +int vsprintf(char* __BIONIC_COMPLICATED_NULLNESS __s, const char* _Nonnull __fmt, va_list __args) __printflike(2, 0) __warnattr_strict("vsprintf is often misused; please use vsnprintf"); char* _Nullable tmpnam(char* _Nullable __s) __warnattr("tmpnam is unsafe, use mkstemp or tmpfile instead"); @@ -251,10 +251,10 @@ FILE* _Nullable freopen64(const char* _Nullable __path, const char* _Nonnull __m FILE* _Nullable tmpfile(void); FILE* _Nullable tmpfile64(void) __INTRODUCED_IN(24); -int snprintf(char* _Nullable __buf, size_t __size, const char* _Nonnull __fmt, ...) __printflike(3, 4); +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); int vscanf(const char* _Nonnull __fmt , va_list __args) __scanflike(1, 0); -int vsnprintf(char* _Nullable __buf, size_t __size, const char* _Nonnull __fmt, va_list __args) __printflike(3, 0); +int vsnprintf(char* __BIONIC_COMPLICATED_NULLNESS __buf, size_t __size, const char* _Nonnull __fmt, va_list __args) __printflike(3, 0); int vsscanf(const char* _Nonnull __s, const char* _Nonnull __fmt, va_list __args) __scanflike(2, 0); #define L_ctermid 1024 /* size for ctermid() */ diff --git a/tests/fortify_test.cpp b/tests/fortify_test.cpp index 4abee67dc..cc3080ddb 100644 --- a/tests/fortify_test.cpp +++ b/tests/fortify_test.cpp @@ -670,6 +670,10 @@ TEST_F(DEATHTEST, readlinkat_fortified) { ASSERT_FORTIFY(readlinkat(AT_FDCWD, "/dev/null", buf, ct)); } +TEST(TEST_NAME, snprintf_nullptr_valid) { + ASSERT_EQ(10, snprintf(nullptr, 0, "0123456789")); +} + extern "C" char* __strncat_chk(char*, const char*, size_t, size_t); extern "C" char* __strcat_chk(char*, const char*, size_t);