[clang] [Clang][Sema] Add fortify warnings for fread, fwrite, and fgets (PR #204337)

Nick Desaulniers via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 16 14:07:22 PDT 2026


Radovan =?utf-8?q?Božić?= <radovan.bozic at htecgroup.com>,
Radovan =?utf-8?q?Božić?= <radovan.bozic at htecgroup.com>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/204337 at github.com>


================
@@ -120,6 +126,14 @@ void call_bcopy_bzero(void) {
   __builtin_bzero(dst, 11); // expected-warning {{'bzero' will always overflow; destination buffer has size 10, but size argument is 11}}
 }
 
+void call_fread_fwrite_fgets(FILE *fp) {
+  char src[4];
+  fread(src, 2, 3, fp); // expected-warning {{'fread' will always overflow; destination buffer has size 4, but size argument is 6}}
+  fwrite(src, 2, 3, fp); // expected-warning {{'fwrite' will always read past the end of the source buffer; source buffer has size 4, but the size is 6}}
+  fgets(src, 5, fp); // expected-warning {{'fgets' size argument is too large; destination buffer has size 4, but size argument is 5}}
----------------
nickdesaulniers wrote:

What happens if someone calls `fgets` with a negative value?

Example:
```c
fgets(src, -1, fp);
```
Do we correctly diagnose that? Please add a unit test checking that.

https://github.com/llvm/llvm-project/pull/204337


More information about the cfe-commits mailing list