Author: Venkatesh Srinivasan
Date: 2026-11-20T10:50:23-08:00
New Revision: 36846ed77c2f4ce9afa0f08043a67243994099a2

URL: 
https://github.com/llvm/llvm-project/commit/36846ed77c2f4ce9afa0f08043a67243994099a2
DIFF: 
https://github.com/llvm/llvm-project/commit/36846ed77c2f4ce9afa0f08043a67243994099a2.diff

LOG: [Clang][Sema] Add fortify warnings for fread_unlocked, fwrite_unlocked, 
and fgets_unlocked (#244378)

Add `-Wfortify-source` compile-time bounds checking for the GNU unlocked
`<stdio.h>` functions `fread_unlocked`, `fwrite_unlocked`, and
`fgets_unlocked`.

- Register `fread_unlocked`, `fwrite_unlocked`, and `fgets_unlocked` as
`GNULibBuiltin<"stdio.h">` builtins in `Builtins.td` (matching `fread`,
`fwrite`, and `fgets`, without `NoThrow` since they are cancellation
points).
- Reuse the existing `BIfread`, `BIfwrite`, and `BIfgets` logic in
`Sema::checkFortifiedBuiltinMemoryFunction` to diagnose destination
overflows (`fread_unlocked`), source overreads (`fwrite_unlocked`), and
too-large or negative sizes (`fgets_unlocked`).
- Add Sema tests covering direct calls, `diagnose_as_builtin` wrappers,
C++ non-`extern "C"` lookalikes, and strict `-std=c99` non-GNU mode, and
update `ReleaseNotes.md`.

Part of #142230

Assisted-by: Gemini

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.md
    clang/include/clang/Basic/Builtins.td
    clang/lib/Sema/SemaChecking.cpp
    clang/test/Sema/attr-diagnose-as-builtin.c
    clang/test/Sema/builtins-gnu-mode.c
    clang/test/Sema/warn-fortify-source.c

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index fa41617847468..00def42401423 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -569,6 +569,10 @@ documentation](CIR/index.md) for more information 
regarding ClangIR's status and
   `explicit_bzero` is called with a size argument larger than the destination
   buffer.
 
+- `-Wfortify-source` now diagnoses calls to `fread_unlocked`,
+  `fwrite_unlocked`, and `fgets_unlocked` when the requested size exceeds the
+  corresponding buffer (or is negative for `fgets_unlocked`).
+
 - The `cannot overload a member function` diagnostic now describes the previous
   declaration first, matching the order in which the declarations appear in the
   source. (#GH219803)

diff  --git a/clang/include/clang/Basic/Builtins.td 
b/clang/include/clang/Basic/Builtins.td
index c9318d965317b..58599ad98825d 100644
--- a/clang/include/clang/Basic/Builtins.td
+++ b/clang/include/clang/Basic/Builtins.td
@@ -4020,6 +4020,21 @@ def ExplicitBZero : GNULibBuiltin<"string.h"> {
   let Prototype = "void(void*, size_t)";
 }
 
+def FreadUnlocked : GNULibBuiltin<"stdio.h"> {
+  let Spellings = ["fread_unlocked"];
+  let Prototype = "size_t(void*, size_t, size_t, FILE*)";
+}
+
+def FwriteUnlocked : GNULibBuiltin<"stdio.h"> {
+  let Spellings = ["fwrite_unlocked"];
+  let Prototype = "size_t(const void*, size_t, size_t, FILE*)";
+}
+
+def FgetsUnlocked : GNULibBuiltin<"stdio.h"> {
+  let Spellings = ["fgets_unlocked"];
+  let Prototype = "char*(char*, int, FILE*)";
+}
+
 def ObjcMsgSend : ObjCLibBuiltin<"objc_message.h"> {
   let Spellings = ["objc_msgSend"];
   let Prototype = "id(id, SEL, ...)";

diff  --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 822cd37a05cbe..c8bb2d873d7c6 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -1607,19 +1607,22 @@ void 
Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD,
     Checker.checkSourceOverread(/*SrcArgIdx=*/0, /*SizeArgIdx=*/2);
     break;
   }
-  case Builtin::BIfread: {
+  case Builtin::BIfread:
+  case Builtin::BIfread_unlocked: {
     DiagID = diag::warn_fortify_source_overflow;
     AccessSize = Checker.ComputeExplicitObjectSizeArgumentProduct(1, 2);
     BufferSize = Checker.ComputeSizeArgument(0);
     break;
   }
-  case Builtin::BIfwrite: {
+  case Builtin::BIfwrite:
+  case Builtin::BIfwrite_unlocked: {
     DiagID = diag::warn_fortify_source_overread;
     AccessSize = Checker.ComputeExplicitObjectSizeArgumentProduct(1, 2);
     BufferSize = Checker.ComputeSizeArgument(0);
     break;
   }
-  case Builtin::BIfgets: {
+  case Builtin::BIfgets:
+  case Builtin::BIfgets_unlocked: {
     AccessSize = Checker.EvaluateIntegerArgument(1);
 
     if (AccessSize && AccessSize->isNegative()) {

diff  --git a/clang/test/Sema/attr-diagnose-as-builtin.c 
b/clang/test/Sema/attr-diagnose-as-builtin.c
index b80ba71d47682..8367bc2c9d20e 100644
--- a/clang/test/Sema/attr-diagnose-as-builtin.c
+++ b/clang/test/Sema/attr-diagnose-as-builtin.c
@@ -65,6 +65,8 @@ void call_memcpy2(void) {
 // Maybe this could be relaxed if it proves too restrictive.
 void failure_type(void *dest, char val, size_t n) 
__attribute__((diagnose_as_builtin(__builtin_memset, 1, 2, 3))) {} // 
expected-error {{'diagnose_as_builtin' attribute parameter types do not match: 
parameter 2 of function 'failure_type' has type 'char', but parameter 2 of 
function '__builtin_memset' has type 'int'}}
 
+typedef struct _IO_FILE FILE;
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -73,6 +75,9 @@ extern int sscanf(const char *input, const char *format, ...);
 extern void *memccpy(void *dst, const void *src, int c, size_t n);
 extern void *memset_explicit(void *dst, int c, size_t n);
 extern void explicit_bzero(void *dst, size_t n);
+extern size_t fread_unlocked(void *ptr, size_t size, size_t nmemb, FILE 
*stream);
+extern size_t fwrite_unlocked(const void *ptr, size_t size, size_t nmemb, FILE 
*stream);
+extern char *fgets_unlocked(char *s, int size, FILE *stream);
 
 #ifdef __cplusplus
 }
@@ -112,6 +117,24 @@ void warn_my_mem_zero_and_memccpy(void) {
   my_explicit_bzero(0, dst, 11); // expected-warning{{'explicit_bzero' will 
always overflow; destination buffer has size 10, but size argument is 11}}
 }
 
+size_t my_fread_unlocked(int extra, void *ptr, size_t size, size_t nmemb, FILE 
*fp)
+    __attribute__((diagnose_as_builtin(fread_unlocked, 2, 3, 4, 5)));
+size_t my_fwrite_unlocked(int extra, const void *ptr, size_t size, size_t 
nmemb, FILE *fp)
+    __attribute__((diagnose_as_builtin(fwrite_unlocked, 2, 3, 4, 5)));
+char *my_fgets_unlocked(int extra, char *s, int size, FILE *fp)
+    __attribute__((diagnose_as_builtin(fgets_unlocked, 2, 3, 4)));
+
+void warn_my_stdio_unlocked(FILE *fp) {
+  char buf[4];
+  my_fread_unlocked(0, buf, 2, 2, fp);
+  my_fread_unlocked(0, buf, 2, 3, fp); // expected-warning{{'fread_unlocked' 
will always overflow; destination buffer has size 4, but size argument is 6}}
+  my_fwrite_unlocked(0, buf, 2, 2, fp);
+  my_fwrite_unlocked(0, buf, 2, 3, fp); // expected-warning{{'fwrite_unlocked' 
will always read past the end of the source buffer; source buffer has size 4, 
but the size is 6}}
+  my_fgets_unlocked(0, buf, 4, fp);
+  my_fgets_unlocked(0, buf, 5, fp); // expected-warning{{'fgets_unlocked' size 
argument is too large; destination buffer has size 4, but size argument is 5}}
+  my_fgets_unlocked(0, buf, -1, fp); // expected-warning{{'fgets_unlocked' 
size argument is negative}}
+}
+
 #ifdef __cplusplus
 
 template <class T>

diff  --git a/clang/test/Sema/builtins-gnu-mode.c 
b/clang/test/Sema/builtins-gnu-mode.c
index 6c7cc20679ae6..f1e8a0780cab3 100644
--- a/clang/test/Sema/builtins-gnu-mode.c
+++ b/clang/test/Sema/builtins-gnu-mode.c
@@ -18,3 +18,6 @@ int siglongjmp;
 int strlcpy;
 int strlcat;
 int explicit_bzero;
+int fread_unlocked;
+int fwrite_unlocked;
+int fgets_unlocked;

diff  --git a/clang/test/Sema/warn-fortify-source.c 
b/clang/test/Sema/warn-fortify-source.c
index b3200ab20ab0b..9b4727528e2eb 100644
--- a/clang/test/Sema/warn-fortify-source.c
+++ b/clang/test/Sema/warn-fortify-source.c
@@ -55,6 +55,9 @@ char *fgets(char *s, int size, FILE *stream);
 void *memccpy(void *dst, const void *src, int c, size_t n);
 void *memset_explicit(void *dst, int c, size_t n);
 void explicit_bzero(void *dst, size_t n);
+size_t fread_unlocked(void *ptr, size_t size, size_t nmemb, FILE *stream);
+size_t fwrite_unlocked(const void *ptr, size_t size, size_t nmemb, FILE 
*stream);
+char *fgets_unlocked(char *s, int size, FILE *stream);
 
 #ifdef __cplusplus
 }
@@ -203,6 +206,31 @@ void call_fread_fwrite_fgets(FILE *fp) {
   fgets(src, 0, fp);
 }
 
+void call_fread_fwrite_fgets_unlocked(FILE *fp, size_t runtime_n,
+                                      int runtime_int) {
+  char buf[4];
+  fread_unlocked(buf, 2, 3, fp); // expected-warning {{'fread_unlocked' will 
always overflow; destination buffer has size 4, but size argument is 6}}
+  fread_unlocked(buf, 1ULL << 32, 1ULL << 32, fp); // expected-warning 
{{'fread_unlocked' will always overflow; destination buffer has size 4, but 
size argument is 18446744073709551616}}
+  fread_unlocked(buf, -1, 1, fp); // expected-warning {{'fread_unlocked' will 
always overflow; destination buffer has size 4, but size argument is 
18446744073709551615}}
+  fwrite_unlocked(buf, 2, 3, fp); // expected-warning {{'fwrite_unlocked' will 
always read past the end of the source buffer; source buffer has size 4, but 
the size is 6}}
+  fwrite_unlocked(buf, 1ULL << 32, 1ULL << 32, fp); // expected-warning 
{{'fwrite_unlocked' will always read past the end of the source buffer; source 
buffer has size 4, but the size is 18446744073709551616}}
+  fwrite_unlocked(buf, -1, 1, fp); // expected-warning {{'fwrite_unlocked' 
will always read past the end of the source buffer; source buffer has size 4, 
but the size is 18446744073709551615}}
+  fgets_unlocked(buf, 5, fp); // expected-warning {{'fgets_unlocked' size 
argument is too large; destination buffer has size 4, but size argument is 5}}
+  fgets_unlocked(buf, -1, fp); // expected-warning {{'fgets_unlocked' size 
argument is negative}}
+
+  fread_unlocked(buf, 2, 2, fp);
+  fread_unlocked(buf, 0, 10, fp);
+  fread_unlocked(buf, 10, 0, fp);
+  fread_unlocked(buf, runtime_n, 2, fp);
+  fwrite_unlocked(buf, 2, 2, fp);
+  fwrite_unlocked(buf, 0, 10, fp);
+  fwrite_unlocked(buf, 10, 0, fp);
+  fwrite_unlocked(buf, runtime_n, 2, fp);
+  fgets_unlocked(buf, 4, fp);
+  fgets_unlocked(buf, 0, fp);
+  fgets_unlocked(buf, runtime_int, fp);
+}
+
 void call_snprintf(double d, int n) {
   char buf[10];
   __builtin_snprintf(buf, 10, "merp");
@@ -453,12 +481,18 @@ namespace non_c_linkage {
 void *memccpy(void *dst, const void *src, int c, size_t n);
 void *memset_explicit(void *dst, int c, size_t n);
 void explicit_bzero(void *dst, size_t n);
+size_t fread_unlocked(void *ptr, size_t size, size_t nmemb, FILE *stream);
+size_t fwrite_unlocked(const void *ptr, size_t size, size_t nmemb, FILE 
*stream);
+char *fgets_unlocked(char *s, int size, FILE *stream);
 
-void call_non_builtins() {
+void call_non_builtins(FILE *fp) {
   char src[20], dst[10];
   memccpy(dst, src, '\0', 20);
   memset_explicit(dst, 0, 20);
   explicit_bzero(dst, 20);
+  non_c_linkage::fread_unlocked(dst, 2, 10, fp);
+  non_c_linkage::fwrite_unlocked(dst, 2, 10, fp);
+  non_c_linkage::fgets_unlocked(dst, 20, fp);
 }
 } // namespace non_c_linkage
 #endif


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to