我跑Valgrind的,我收到以下錯誤.. 之前我做了一個備份我固定的,但現在我不記得如何。物通過的malloc產生錯誤,但在代碼Unitialised值(S)的valgrind
>Insert password for admin: ==5720== Conditional jump or move depends on uninitialised value(s)
==5720== at 0x40299EB: strcmp (mc_replace_strmem.c:538)
==5720== by 0x80496C6: adm_log_request (commands_man.c:169)
==5720== by 0x80521CA: main (mmboxman.c:9)
==5720== Uninitialised value was created by a heap allocation
==5720== at 0x4028876: malloc (vg_replace_malloc.c:236)
==5720== by 0x8049683: adm_log_request (commands_man.c:165)
==5720== by 0x80521CA: main (mmboxman.c:9)
==5720==
這是函數 線commands_man我無法找到該錯誤:165是,如果後(大小> 0)
int adm_log_request(void){
FILE *password;
char *pwdin, *frompwd = NULL;
int primo = 0/*indica se è un primo avvio*/, tentativi = 2, p, size;
if(!(password = fopen(F_PWD_ADM, "rb"))){
primo = 1;
printf("First server boot\n>Insert password for admin: ");
}
else{
primo = 0;
printf(">Insert password for admin: ");
}
p = get_hid_pass(&pwdin);
if(p < 0)
return -1;
switch(primo){
case 0:
if(!(password = fopen(F_PWD_ADM, "r")))
return -1;
fread(&size, sizeof(int), 1, password);
if(size > 0){
frompwd = (char*)malloc(size + 1);
fread(frompwd,sizeof(frompwd),1,password);
}else return 0;
while(tentativi > 0){
if(strcmp(pwdin, frompwd) != 0){
printf("\nIncorrect password\n%d attempts left\n>Insert password for admin: ", tentativi);
tentativi--;
}
else return 1;
p = get_hid_pass(&pwdin);
if(p < 0)
return -1;
}
fclose(password);
break;
case 1: //primo avvio del server
if(!(password = fopen(F_PWD_ADM, "w")))
return -1;
size = strlen(pwdin) + 1;
fwrite(&size, sizeof(int), 1, password);
fwrite(pwdin, sizeof(pwdin), 1, password);
fclose(password);
break;
}
if(tentativi == 0)
return -1;
return 1;
}
莫非有人幫我解決他們?謝謝你的問題
我發現問題..它在 size = strlen(pwdin)+ 1; +1錯了!謝謝!! =) – roccocullo 2012-02-09 15:46:53
@roccocullo:這取決於你是否想寫空終止符字節或不。並且請注意,'fwrite'調用仍然需要被賦予正確的長度(大小)而不是sizeof結果。 – 2012-02-09 15:49:15