[clang] [Clang][Sema] Add fortify warnings for fread, fwrite, and fgets (PR #204337)
Radovan Božić via cfe-commits
cfe-commits at lists.llvm.org
Mon Sep 21 05:19:37 PDT 2026
================
@@ -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}}
----------------
bozicrHT 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.
Unfortunately, I missed that case. I’ll add support for negative size arguments to `fgets`.
https://github.com/llvm/llvm-project/pull/204337
More information about the cfe-commits
mailing list