多个 valgrind 错误:条件跳转或移动取决于未初始化的值

发布于 2024-08-30 00:31:02 字数 3192 浏览 5 评论 0原文

我正在运行 Valgrind,并且收到以下错误(这不是唯一的错误):

==21743== Conditional jump or move depends on uninitialised value(s)
==21743==    at 0x4A06509: index (mc_replace_strmem.c:164)
==21743==    by 0x33B7CBB3CD: gaih_inet (in /lib64/libc-2.5.so)
==21743==    by 0x33B7CBD629: getaddrinfo (in /lib64/libc-2.5.so)
==21743==    by 0x401A5F: tunnelURL (proxy.c:336)
==21743==    by 0x40142A: client_thread (proxy.c:194)
==21743==    by 0x33B8806616: start_thread (in /lib64/libpthread-2.5.so)
==21743==    by 0x33B7CD3C2C: clone (in /lib64/libc-2.5.so)

我的 tunnelURL() 函数如下所示(C 代码):

char * tunnelURL(char *url) {
 char * a = strstr(url, "//");
 a += 2;
 char * path = strstr(a, "/");

 char host[256];
 strncpy (host, a, strlen(a)-strlen(path));

 /*
  * The following is courtesy of Beej's Guide
  */
 int status;
 int proxySocketFD;
 struct addrinfo hints;
 struct addrinfo *servinfo; // will point to the results

 memset(&hints, 0, sizeof(hints)); // make sure the struct is empty
 hints.ai_family = AF_INET; // don't care IPv4 or IPv6
 hints.ai_socktype = SOCK_STREAM; // TCP stream sockets
 hints.ai_flags = AI_PASSIVE; // fill in my IP for me

 if ((status = getaddrinfo(host, "80", &hints, &servinfo)) != 0) {
  perror("getaddrinfo() fail");
  exit(1);
 }

 // create socket
 if ((proxySocketFD = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol)) == -1) {
  perror("proxy socket() fail");
  exit(1);
 }

 // connect
 if (connect(proxySocketFD, servinfo->ai_addr, servinfo->ai_addrlen) != 0) {
  printf("connect() fail");
  exit(1);
 }

 // construct request
 char request[strlen(path) + strlen(host) + 26];
 sprintf(request, "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n", path, host);
 printf("%s", request);

 // send request
 send(proxySocketFD, request, strlen(request), 0);

 // receive response
 int i = 0;
 int amntRecvd = 0;
 char *pageContentBuffer = (char*) malloc(4096 * sizeof(char));
 while ((amntRecvd = recv(proxySocketFD, pageContentBuffer + i, 4096, 0)) > 0) {
  i += amntRecvd;
  realloc(pageContentBuffer, i * 4096 * sizeof(char));
 }

 // close proxy socket
 close(proxySocketFD);

 // deallocate memory
 freeaddrinfo(servinfo);

 return pageContentBuffer;

}

第 336 行对应于 if 语句与 getaddrinfo() 函数调用。我不太确定我还没有初始化什么。我传入“应该”的字符串已经设置。我把它打印出来就好了。我还收到与同一行代码相对应的另一个错误:

==21743== Use of uninitialised value of size 8
==21743==    at 0x33B7D05816: __nscd_cache_search (in /lib64/libc-2.5.so)
==21743==    by 0x33B7D0438B: nscd_gethst_r (in /lib64/libc-2.5.so)
==21743==    by 0x33B7D04B26: __nscd_gethostbyname2_r (in /lib64/libc-2.5.so)
==21743==    by 0x33B7CE9F5E: gethostbyname2_r@@GLIBC_2.2.5 (in /lib64/libc-2.5.so)
==21743==    by 0x33B7CBC522: gaih_inet (in /lib64/libc-2.5.so)
==21743==    by 0x33B7CBD629: getaddrinfo (in /lib64/libc-2.5.so)
==21743==    by 0x401A5F: tunnelURL (proxy.c:336)
==21743==    by 0x40142A: client_thread (proxy.c:194)
==21743==    by 0x33B8806616: start_thread (in /lib64/libpthread-2.5.so)
==21743==    by 0x33B7CD3C2C: clone (in /lib64/libc-2.5.so)

关于可能导致此问题的任何想法?

I'm running Valgrind and I'm getting the following error (this is not the only one):

==21743== Conditional jump or move depends on uninitialised value(s)
==21743==    at 0x4A06509: index (mc_replace_strmem.c:164)
==21743==    by 0x33B7CBB3CD: gaih_inet (in /lib64/libc-2.5.so)
==21743==    by 0x33B7CBD629: getaddrinfo (in /lib64/libc-2.5.so)
==21743==    by 0x401A5F: tunnelURL (proxy.c:336)
==21743==    by 0x40142A: client_thread (proxy.c:194)
==21743==    by 0x33B8806616: start_thread (in /lib64/libpthread-2.5.so)
==21743==    by 0x33B7CD3C2C: clone (in /lib64/libc-2.5.so)

My tunnelURL() function looks like this (C code):

char * tunnelURL(char *url) {
 char * a = strstr(url, "//");
 a += 2;
 char * path = strstr(a, "/");

 char host[256];
 strncpy (host, a, strlen(a)-strlen(path));

 /*
  * The following is courtesy of Beej's Guide
  */
 int status;
 int proxySocketFD;
 struct addrinfo hints;
 struct addrinfo *servinfo; // will point to the results

 memset(&hints, 0, sizeof(hints)); // make sure the struct is empty
 hints.ai_family = AF_INET; // don't care IPv4 or IPv6
 hints.ai_socktype = SOCK_STREAM; // TCP stream sockets
 hints.ai_flags = AI_PASSIVE; // fill in my IP for me

 if ((status = getaddrinfo(host, "80", &hints, &servinfo)) != 0) {
  perror("getaddrinfo() fail");
  exit(1);
 }

 // create socket
 if ((proxySocketFD = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol)) == -1) {
  perror("proxy socket() fail");
  exit(1);
 }

 // connect
 if (connect(proxySocketFD, servinfo->ai_addr, servinfo->ai_addrlen) != 0) {
  printf("connect() fail");
  exit(1);
 }

 // construct request
 char request[strlen(path) + strlen(host) + 26];
 sprintf(request, "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n", path, host);
 printf("%s", request);

 // send request
 send(proxySocketFD, request, strlen(request), 0);

 // receive response
 int i = 0;
 int amntRecvd = 0;
 char *pageContentBuffer = (char*) malloc(4096 * sizeof(char));
 while ((amntRecvd = recv(proxySocketFD, pageContentBuffer + i, 4096, 0)) > 0) {
  i += amntRecvd;
  realloc(pageContentBuffer, i * 4096 * sizeof(char));
 }

 // close proxy socket
 close(proxySocketFD);

 // deallocate memory
 freeaddrinfo(servinfo);

 return pageContentBuffer;

}

Line 336 corresponds to the if statement with the getaddrinfo() function call. I'm not really sure what I haven't initialized. The string I'm passing in "should" be already set. I'm printing it out just fine. I also get another error corresponding to the same line of code:

==21743== Use of uninitialised value of size 8
==21743==    at 0x33B7D05816: __nscd_cache_search (in /lib64/libc-2.5.so)
==21743==    by 0x33B7D0438B: nscd_gethst_r (in /lib64/libc-2.5.so)
==21743==    by 0x33B7D04B26: __nscd_gethostbyname2_r (in /lib64/libc-2.5.so)
==21743==    by 0x33B7CE9F5E: gethostbyname2_r@@GLIBC_2.2.5 (in /lib64/libc-2.5.so)
==21743==    by 0x33B7CBC522: gaih_inet (in /lib64/libc-2.5.so)
==21743==    by 0x33B7CBD629: getaddrinfo (in /lib64/libc-2.5.so)
==21743==    by 0x401A5F: tunnelURL (proxy.c:336)
==21743==    by 0x40142A: client_thread (proxy.c:194)
==21743==    by 0x33B8806616: start_thread (in /lib64/libpthread-2.5.so)
==21743==    by 0x33B7CD3C2C: clone (in /lib64/libc-2.5.so)

Any ideas as to what might becausing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

み格子的夏天 2024-09-06 00:31:02

您没有正确使用realloc()。 realloc() 可能会移动分配的块,并返回该块的新地址 - 因此您需要将该返回值分配给 pageContentBuffer(如果它不是) NULL)。

You're not using realloc() correctly. realloc() may move the allocated block, and it returns the new address of the block - so you need to assign that return value to pageContentBuffer (iff it's not NULL).

绿光 2024-09-06 00:31:02
 // receive response
 int i = 0;
 int amntRecvd = 0;
 char *pageContentBuffer = (char*) malloc(4096 * sizeof(char));
 while ((amntRecvd = recv(proxySocketFD, pageContentBuffer + i, 4096, 0)) > 0) {
  i += amntRecvd;
  realloc(pageContentBuffer, i * 4096 * sizeof(char));
 }

realloc(pageContentBuffer, i * 4096 * sizeof(char)) 看起来不正确。如果您首先收到 4096 字节,则接下来将分配 4096*4096 字节,接下来分配 2*4096*4096 字节,依此类推。
也许你的意思是加法?

 // receive response
 int i = 0;
 int amntRecvd = 0;
 char *pageContentBuffer = (char*) malloc(4096 * sizeof(char));
 while ((amntRecvd = recv(proxySocketFD, pageContentBuffer + i, 4096, 0)) > 0) {
  i += amntRecvd;
  realloc(pageContentBuffer, i * 4096 * sizeof(char));
 }

realloc(pageContentBuffer, i * 4096 * sizeof(char)) does not look right. If you received 4096 bytes first, you will be allocating 4096*4096 bytes next, 2*4096*4096 bytes next and so on.
Perhaps you meant addition?

清欢 2024-09-06 00:31:02

您的问题中显示的第一个条件跳转错误是由于您错误地使用了 strncpy 造成的。它不会终止复制的字符串,您必须随后手动执行此操作。那么最好使用 memcpy。

char host[256];
int n = strlen(a)-strlen(path);
memcpy (host, a, n);
host[n] = '\0';

The first conditional jump error displayed in your question is due to you using strncpy incorrectly. It won't terminate the copied string, you have to do that manually afterwards. Better then to use memcpy.

char host[256];
int n = strlen(a)-strlen(path);
memcpy (host, a, n);
host[n] = '\0';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文