[clang] [Clang][Sema] Add fortify warnings for send and sendto (PR #228194)
Venkatesh Srinivasan via cfe-commits
cfe-commits at lists.llvm.org
Fri Oct 9 13:08:40 PDT 2026
================
@@ -339,6 +344,49 @@ void call_recv_runtime(int fd, int n) {
recvfrom(fd, buf, n, 0, (struct sockaddr *)0, 0);
}
+void call_send(int fd) {
+ char buf[10];
+ send(fd, buf, 0, 0);
+ send(fd, buf, 10, 0);
+ send(fd, buf, 11, 0); // expected-warning {{'send' will always read past the end of the source buffer; source buffer has size 10, but the size is 11}}
+ send(fd, buf, -1, 0); // expected-warning {{'send' will always read past the end of the source buffer; source buffer has size 10, but the size is 18446744073709551615}}
+}
+
+void call_sendto(int fd) {
+ char buf[10];
+ sendto(fd, buf, 0, 0, (const struct sockaddr *)0, 0);
+ sendto(fd, buf, 10, 0, (const struct sockaddr *)0, 0);
+ sendto(fd, buf, 11, 0, (const struct sockaddr *)0, 0); // expected-warning {{'sendto' will always read past the end of the source buffer; source buffer has size 10, but the size is 11}}
+ sendto(fd, buf, -1, 0, (const struct sockaddr *)0, 0); // expected-warning {{'sendto' will always read past the end of the source buffer; source buffer has size 10, but the size is 18446744073709551615}}
+}
+
+void call_send_subobject(int fd) {
+ struct {
+ char first[10];
+ char second[20];
+ } s;
+ send(fd, s.first, 35, 0); // expected-warning {{'send' will always read past the end of the source buffer; source buffer has size 30, but the size is 35}}
+}
+
+void call_send_runtime(int fd, int n) {
+ char buf[10];
+ send(fd, buf, n, 0);
+ sendto(fd, buf, n, 0, (const struct sockaddr *)0, 0);
+}
+
+#if !defined(__cplusplus) && !defined(USE_BUILTINS)
+typedef union {
+ const struct sockaddr *__sockaddr__;
+} __CONST_SOCKADDR_ARG __attribute__((__transparent_union__));
----------------
venk-ks wrote:
Done in f8fb369b8146: added a second member (`const struct sockaddr_in *`) to `__CONST_SOCKADDR_ARG`, tested passing both `const struct sockaddr *` and `const struct sockaddr_in *`, added comments in both `warn-fortify-source.c` and `SemaChecking.cpp` explaining glibc's `_GNU_SOURCE` `<sys/socket.h>` declaration, and dropped the double underscores on `__attribute__((transparent_union))`.
https://github.com/llvm/llvm-project/pull/228194
More information about the cfe-commits
mailing list