-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix clang uninitialized variable for struct curltime
#10993
Conversation
I'm counting 71 places in the code that use And I can't see how it is wrong. Can you? If this fixes the warning, then it smells like a bad warning... |
Isn't it the diff --git a/lib/socketpair.c b/lib/socketpair.c
index b94c9843e..67ce8da8d 100644
--- a/lib/socketpair.c
+++ b/lib/socketpair.c
@@ -123,11 +123,11 @@ int Curl_socketpair(int domain, int type, int protocol,
(void)Curl_poll(pfd, 1, 1000); /* one second */
socks[1] = accept(listener, NULL, NULL);
if(socks[1] == CURL_SOCKET_BAD)
goto error;
else {
- struct curltime check;
+ struct curltime check = {0, 0};
struct curltime start = Curl_now();
char *p = (char *)✓
size_t s = sizeof(check);
/* write data to the socket */ |
I think it legitimate, the question is whether it's worth fixing. The issue is that the Here is a small example: #include <time.h>
#include <string.h>
struct curltime {
time_t tv_sec; /* seconds */
int tv_usec; /* microseconds */
};
struct curltime Curl_now()
{
struct curltime now;
now.tv_sec = time(NULL);
now.tv_usec = 0;
return now;
}
int main(int argc, char** argv)
{
struct curltime check = {};
struct curltime start;
start = Curl_now();
if(memcmp(&start, &check, sizeof(check)))
return 1;
return 0;
} Msan will report unintialized variable in this case. The fix I propose above is actually not great.. it relies on an optimization trick that converts int main(int argc, char** argv)
{
struct curltime check;
memset(&check,0,sizeof(check));
struct curltime start;
memset(&start,0,sizeof(start));
start = Curl_now();
if(memcmp(&start, &check, sizeof(check)))
return 1;
return 0;
} |
Also, MSAN is complaining about |
Another option that might work:
|
|
I figure maybe a nicer approach would be to switch to a plain random number to completely avoid the struct align thing. |
... instead of using the curl time struct, since it would use a few uninitialized bytes and the sanitizers would complain. This is a neater approach I think. Reported-by: Boris Kuschel Fixes #10993
... instead of using the curl time struct, since it would use a few uninitialized bytes and the sanitizers would complain. This is a neater approach I think. Reported-by: Boris Kuschel Fixes curl#10993 Closes curl#11015
When compiling with clang-15 memory sanitizer:
Maybe the special handling of returning a struct from a function uses the uninitialized reference to
curltime
as initializing it to zeros (by{}
) prior to Curl_now() avoids the uninitialized value error.