gpt4 book ai didi

c - 来自 getaddrinfo() 的 4 错误的无效读取大小

转载 作者:太空宇宙 更新时间:2023-11-04 03:45:11 24 4
gpt4 key购买 nike

在我的程序上运行 Valgrind 会生成大小为 4 的无效读取错误。我试图找到它,但似乎找不到。

#define _GNU_SOURCE
#include <stdio.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
#include <sys/types.h>
#include <sys/socket.h>

void download_file (void){
int ret;
char *service = "http";
char *site = "www.sony.com";
char *page = "/";
char *msg;
struct addrinfo *res;
struct addrinfo hints;
const char * format = "GET %s HTTP/1.1\r\nHost: %s\r\nUser-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0\r\n\r\n";
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;

asprintf(&msg, format, page, site);

if((ret = getaddrinfo(site, service, &hints, &res)) != 0){
fprintf(stderr, "download_file: getaddrinfo error: %s\n", gai_strerror(ret));
freeaddrinfo(res);
free(msg);
return;
}
freeaddrinfo(res);
free(msg);
return;
}
int main (void){
download_file();
exit(0);
}

这是 Valgrind 的输出:

==3215== Memcheck, a memory error detector
==3215== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==3215== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==3215== Command: ./a.out
==3215==
==3215== Invalid read of size 4
==3215== at 0x401718C: strlen (strlen.S:53)
==3215== by 0x4012F1F: _dl_open (dl-open.c:672)
==3215== by 0x495982F: do_dlopen (dl-libc.c:86)
==3215== by 0x400F0BF: _dl_catch_error (dl-error.c:178)
==3215== by 0x4959917: dlerror_run (dl-libc.c:47)
==3215== by 0x495997F: __libc_dlopen_mode (dl-libc.c:160)
==3215== by 0x4935447: __nss_lookup_function (nsswitch.c:434)
==3215== by 0x493569B: __nss_lookup (nsswitch.c:229)
==3215== by 0x493DB93: getservbyname_r@@GLIBC_2.4 (getXXbyYY_r.c:203)
==3215== by 0x48FDBA3: gaih_inet_serv (getaddrinfo.c:164)
==3215== by 0x48FE917: gaih_inet (getaddrinfo.c:332)
==3215== by 0x490145F: getaddrinfo (getaddrinfo.c:2438)
==3215== Address 0x4988e64 is 44 bytes inside a block of size 46 alloc'd
==3215== at 0x4835978: malloc (vg_replace_malloc.c:263)
==3215== by 0x400EE03: _dl_signal_error (dl-error.c:90)
==3215== by 0x400903F: _dl_map_object (dl-load.c:2556)
==3215== by 0x4013157: dl_open_worker (dl-open.c:226)
==3215== by 0x400F0BF: _dl_catch_error (dl-error.c:178)
==3215== by 0x4012C9B: _dl_open (dl-open.c:633)
==3215== by 0x495982F: do_dlopen (dl-libc.c:86)
==3215== by 0x400F0BF: _dl_catch_error (dl-error.c:178)
==3215== by 0x4959917: dlerror_run (dl-libc.c:47)
==3215== by 0x495997F: __libc_dlopen_mode (dl-libc.c:160)
==3215== by 0x4935447: __nss_lookup_function (nsswitch.c:434)
==3215== by 0x493569B: __nss_lookup (nsswitch.c:229)
==3215==
==3215==
==3215== HEAP SUMMARY:
==3215== in use at exit: 0 bytes in 0 blocks
==3215== total heap usage: 67 allocs, 67 frees, 6,461 bytes allocated
==3215==
==3215== All heap blocks were freed -- no leaks are possible
==3215==
==3215== For counts of detected and suppressed errors, rerun with: -v
==3215== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 25 from 11)

有人可以帮我理解为什么会出现这个错误吗?或者如何纠正?在我的搜索中,我只发现与 gethostbyaddr() 相关的错误。

看起来无效读取错误来自 getaddrinfo(),但我无法在我的系统上找到文件 getaddrinfo.c 以尝试更正它。

最佳答案

这看起来像是一个与此处讨论的问题类似的问题:http://git.661346.n2.nabble.com/PATCH-valgrind-ignore-SSE-based-strlen-invalid-reads-td6175816.html

优化的 strlen() 在搜索 null 终止符 char 时通常会读取完整的单词。这可以通过实现以安全的方式完成,即使它在技术上违反了分配边界。

您可能需要较新版本的 valgrind(当前为 3.9.0),或者需要针对您正在使用的运行时配置或修补 valgrind。您可能希望为此配置抑制:http://valgrind.org/docs/manual/manual-core.html#manual-core.suppress

关于c - 来自 getaddrinfo() 的 4 错误的无效读取大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25222131/

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