как заголовок подсказывает, что у меня проблема с Valgrind, где я получаю некоторые ошибки, которые переменные не инициализируются. Вот что я писал до сих пор:C Valgrind Условный переход или перемещение
int login(char* input, int input_length){
//input = base64encoded user:pass
//decode data
//find username
//find pass
//hash pass
SHA1_CTX context;
uint8_t digest[20];
char* passlocation = NULL;
char* decoded = NULL;
char* username = NULL;
char* pass = NULL;
int temp = 0;
int login_status = -1;
int i = 0;
decoded = NULL;
if(input != NULL) {
decoded = base64_decode(input, input_length);
}
if(decoded == NULL){
return -1;
}
passlocation = strchr(decoded, ':'); //First Uninitalised error
if(passlocation) {
temp = strlen(input) - strlen(passlocation);
}
if(temp == 0 || temp == (input_length-1)){
return -1;
}
username = calloc(temp+1, sizeof(char));
strncpy(username, decoded, temp); //Second Uninitalised error
pass = calloc((input_length - temp), sizeof(char)); //Third Uninitalised error
strcpy(pass, (passlocation+1)); //inavlid read of size 1
if(username != NULL && pass != NULL){
printf("Username: %s\n", username); //Fourth Uninitalised error
printf("Password: %s\n", pass); //Invalid read of size 1
}
SHA1_Init(&context);
SHA1_Update(&context, (uint8_t *) pass, strlen(pass)); //invalid read of size 1
SHA1_Final(&context, digest);
login_status = identify_user(username, temp,(char*) digest);
clean_free(username);
clean_free(pass);
clean_free(decoded);
printf("%d\n",login_status);
return login_status;
}
Я не ожидаю какой-либо из вас, ребята, чтобы исправить все свои ошибки сразу, я бы хотел, чтобы понять, почему я получаю первые неинициализированная ошибка от valgrind, так как я пытался исправить ее за 30 часов (минус сон), и я просто не вижу, что моя ошибка.
Спасибо, ребята, заранее!
Edit: base64_decode:
char* base64_decode(char* toDecode, int toDecode_length){
static char encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '+', '/'};
char* i=toDecode;
char* decoded = calloc(256,sizeof(char));
int octets[24];
int s=6;
int sc=0;
int c=0;
int n=0;
int threechars=0;
int threecharC=0;
int decodeC;
int deLoop;
int expo=1;
int aValue;
char temp;
while(c<(toDecode_length)){ //länge des toDecode
n=0;
if(toDecode[c]!='='){
while(toDecode[c]!=encoding_table[n]){ //base64 char Wert ermitteln
n++;
}
for(sc=1;sc<7;sc++){ //base64 char Wert in binär
octets[s-sc]=n%2;
n=n/2;
}
for(sc=0;sc<6;sc++){ //Ausgabe des Binärwertes in Konsole (Debug)
//printf("%d",octets[s-6+sc]);
}
}else{
for(sc=1;sc<7;sc++){ //bei base64 wert '=' mit 0 füllen
octets[s-sc]=0;
}
}
s=s+6;
i++;
threechars++;
if(threechars==4){ //ermitteln des ascii wertes und schreiben in decoded
for(deLoop=8;deLoop<=24;deLoop=deLoop+8){
for(decodeC=1;decodeC<=8;decodeC++){
if(octets[deLoop-decodeC]==1){
aValue=aValue+expo;
}
expo=expo*2;
}
temp=aValue;
decoded[threecharC]=temp;
expo=1;
aValue=0;
threecharC++;
}
threechars=0;
s=6;
}
c++;
}
//printf("return value %d",n);
return decoded;
}
Valgrind-log(via Command-line not Eclipse Plug-in)
==4383== Conditional jump or move depends on uninitialised value(s)
==4383== at 0x4C2DB9A: __GI_strchr (in /usr/lib/valgrind/vgpreload_memcheck- amd64-linux.so)
==4383== by 0x401889: login (http-login.c:174)
==4383== by 0x4036DD: main_loop (http-server.c:140)
==4383== by 0x403902: main (http-server.c:214)
==4383== Uninitialised value was created by a stack allocation
==4383== at 0x401568: base64_decode (http-login.c:81)
==4383==
==4383== Conditional jump or move depends on uninitialised value(s)
==4383== at 0x4C2DBA0: __GI_strchr (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4383== by 0x401889: login (http-login.c:174)
==4383== by 0x4036DD: main_loop (http-server.c:140)
==4383== by 0x403902: main (http-server.c:214)
==4383== Uninitialised value was created by a stack allocation
==4383== at 0x401568: base64_decode (http-login.c:81)
==4383==
==4383== Conditional jump or move depends on uninitialised value(s)
==4383== at 0x4C2E78E: __strncpy_sse2_unaligned (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4383== by 0x401929: login (http-login.c:182)
==4383== by 0x4036DD: main_loop (http-server.c:140)
==4383== by 0x403902: main (http-server.c:214)
==4383== Uninitialised value was created by a stack allocation
==4383== at 0x401568: base64_decode (http-login.c:81)
==4383==
==4383== Invalid write of size 1
==4383== at 0x4C2E1F3: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4383== by 0x40196C: login (http-login.c:185)
==4383== by 0x4036DD: main_loop (http-server.c:140)
==4383== by 0x403902: main (http-server.c:214)
==4383== Address 0x51fcf88 is 0 bytes after a block of size 8 alloc'd
==4383== at 0x4C2CC70: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4383== by 0x401948: login (http-login.c:183)
==4383== by 0x4036DD: main_loop (http-server.c:140)
==4383== by 0x403902: main (http-server.c:214)
Это не полный журнал, если вы хотите полный журнал я выложу его
Можете ли вы опубликовать свой выход valgrind? –
И эта функция: base64_decode – Mat
Я отредактировал то, что вы просили, если что-то еще отсутствует, пожалуйста, дайте мне знать –