C Valgrind有条件的跳跃或移动

时间:2016-05-29 10:20:54

标签: c valgrind

正如标题所示,我对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小时(减去睡觉)我只是看不出我的错误。

提前谢谢你们!

编辑: 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)  

这不是一个完整的日志,如果你想要完整的日志我会发布它

2 个答案:

答案 0 :(得分:0)

您使用的存储指针。例如

char* passlocation = NULL;

说,创建一个指向无处的指针。因此,当您尝试使用此指针时,未经初始化的错误。

您需要做的是为要存储在指针位置的数据分配内存。例如

char* passlocation;
passlocation = (char *)malloc( 50 * sizeof(char) );

然后你就可以对它们进行有意义的操作(假设你的数据将超过50个字符。完成后不要忘记释放数据。

答案 1 :(得分:0)

好的,已经发现了这个bug!

感谢所有试图提供帮助的人,非常感谢!!!

问题,虽然valgrind只在我的函数(也就是登录)中标记它,但是是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 = 0; //HERE WAS THE PROBLEM, uninitialised value!
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;
}

每个人都度过一个美好的夜晚!