gpt4 book ai didi

c - 多个 valgrind 错误 : Conditional jump or move depends on uninitialised value(s)

转载 作者:行者123 更新时间:2023-12-02 00:40:42 25 4
gpt4 key购买 nike

我正在运行 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)

关于可能是什么原因的任何想法?

最佳答案

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

关于c - 多个 valgrind 错误 : Conditional jump or move depends on uninitialised value(s),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2679661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com