gpt4 book ai didi

c - valgrind 重新分配错误 : Conditional jump or move depends on uninitialised value(s)

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

==17209== Conditional jump or move depends on uninitialised value(s)
==17209== at 0x402E7C5: __GI___rawmemchr (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==17209== by 0x40CE921: _IO_str_init_static_internal (strops.c:45)
==17209== by 0x40B0B76: __isoc99_vsscanf (isoc99_vsscanf.c:42)
==17209== by 0x8048647: main (lala.c:23)
==17209== Uninitialised value was created by a stack allocation
==17209== at 0x8048659: gatherInfoSalt (lala.c:28)
==17209==

.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <string.h>
#include <unistd.h>
#include <errno.h>


struct sysinfo_s {
char *salt_id;
};


void gatherInfoSalt(char ** );

int main (int argc, char *argv[]) {
struct sysinfo_s si;

si.salt_id = malloc(1);
*si.salt_id = 0;

gatherInfoSalt(&si.salt_id);

free(si.salt_id);
}

void gatherInfoSalt(char ** salt_id) {
FILE * fp;
char buf[256];
int r;


if (!(fp = fopen("/etc/salt/minion.d/id.conf","r"))) {
perror("fopen");
return;
}

r=fread(buf,1,255,fp);

//adding *buf=0 here removes the error (but removes functionality)

if (r) {
*salt_id=realloc(*salt_id,r-2);
**salt_id=0;
sscanf(buf,"id:%s",*salt_id);
//sscanf(buf,"id:%s",buf1);
}

printf("saltid: %s\n",*salt_id);

fclose(fp);

}

最佳答案

这个

r=fread(buf,1,255,fp);
sscanf(buf,"id:%s",....

应该是:

  r=fread(buf,1,255,fp);
buf[r]=0;
sscanf(buf,"id:%s",....

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

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