==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);
}
答案 0 :(得分:3)
此
r=fread(buf,1,255,fp);
sscanf(buf,"id:%s",....
应该是:
r=fread(buf,1,255,fp);
buf[r]=0;
sscanf(buf,"id:%s",....
答案 1 :(得分:0)
在声明时初始化buf或在fread之前将其设置为0。