-
Notifications
You must be signed in to change notification settings - Fork 15.2k
Open
Labels
Description
There seems to be a regression in the unix.StdCLibraryFunctions analysis check.
Found below is code for a small example. Running clang --analyze sendto.c produces warnings such as the following:
sendto.c:20:3: warning: The 1st argument to 'sendto' is -1 but should be >= 0 [unix.StdCLibraryFunctions]
20 | sendto(sockfd, NULL, 0, 0, NULL, 0);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This happens for versions 19.1.2 and 19.1.3 but not 18.1.8. All acquired via this Github project.
It seems the analysis pass believes some_function_outside_tu can set sockfd to -1 as removing the call to it or adding if(0 > sockfd) return 1; between it and the call to sendto resolves this warning. some_function_outside_tu should not be able to modify sockfd since the implementation is in a different translation unit and sockfd is static.
Here is the code for sendto.c:
#include <arpa/inet.h>
#include <sys/socket.h>
#include <stddef.h>
static int sockfd = -1;
void some_function_outside_tu(); // declared here, but implementation
// is outside this translation unit
int main(int argc, const char* argv[]) {
(void)argc; (void)argv;
sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(0 > sockfd) {
return 1;
}
some_function_outside_tu(); // this causes issues
sendto(sockfd, NULL, 0, 0, NULL, 0);
}