]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
libio: Add __nonnull for FILE * arguments of fclose and freopen
authorXi Ruoyao <xry111@xry111.site>
Fri, 21 Apr 2023 06:21:33 +0000 (14:21 +0800)
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>
Tue, 16 May 2023 09:56:32 +0000 (06:56 -0300)
Calling fclose or freopen with a null FILE * is undefined behavior, and
doing so in practice will cause a SIGSEGV.  So it seems suitable for
__nonnull.

This will help the compiler to warn for some buggy code, like
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109570.
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
libio/stdio.h

index 2387590d6a7cf3a26dd518ef0745337cb5c0c509..4cf9f1c01276c261d2f9a13d15c41ddeab1e12b5 100644 (file)
@@ -180,7 +180,7 @@ extern int renameat2 (int __oldfd, const char *__old, int __newfd,
 
    This function is a possible cancellation point and therefore not
    marked with __THROW.  */
-extern int fclose (FILE *__stream);
+extern int fclose (FILE *__stream) __nonnull ((1));
 
 #undef __attr_dealloc_fclose
 #define __attr_dealloc_fclose __attr_dealloc (fclose, 1)
@@ -269,7 +269,7 @@ extern FILE *fopen (const char *__restrict __filename,
    marked with __THROW.  */
 extern FILE *freopen (const char *__restrict __filename,
                      const char *__restrict __modes,
-                     FILE *__restrict __stream) __wur;
+                     FILE *__restrict __stream) __wur __nonnull ((3));
 #else
 # ifdef __REDIRECT
 extern FILE *__REDIRECT (fopen, (const char *__restrict __filename,
@@ -290,7 +290,7 @@ extern FILE *fopen64 (const char *__restrict __filename,
   __attribute_malloc__ __attr_dealloc_fclose __wur;
 extern FILE *freopen64 (const char *__restrict __filename,
                        const char *__restrict __modes,
-                       FILE *__restrict __stream) __wur;
+                       FILE *__restrict __stream) __wur __nonnull ((3));
 #endif
 
 #ifdef __USE_POSIX