【问题标题】:C Valgrind Conditional jump or moveC Valgrind 条件跳转或移动
【发布时间】:2016-09-27 06:47:29
【问题描述】:

正如标题所示,我遇到了 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)  

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

【问题讨论】:

  • 你能发布你的 valgrind 输出吗?
  • 还有这个函数:base64_decode
  • 我编辑了你要求的东西,如果还有什么遗漏请告诉我
  • http-login.c 的第 81 行是哪一行?如果if(octets[deLoop-decodeC]==1),我会猜到这个。
  • char* base64_decode(char* toDecode, int toDecode_length){ 这是第81行,所以开始执行base64_decode

标签: c valgrind


【解决方案1】:

您使用存储等指针。例如

char* passlocation = NULL;

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

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

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

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

【讨论】:

  • 这是不正确的。 char* passlocation = NULL; 实际上已初始化,并不是导致警告的命令。
  • 造成错误的不是命令,而是错误的原因。解决这个问题,错误就会消失。 (答案澄清)
  • 另外,当我为 passlocation 分配内存时,我确实会发生内存泄漏,因为我使用 strchr,它会给我一个指向我的解码指针的指针,因此我不能再释放 passlocation。
  • 这是一个单独的问题!
  • 只是为了让您知道,您所说的可能是真的,也可能不是,但是您的代码被剪断并不能解决任何问题。我仍然在完全相同的行上遇到条件跳转错误,然后我有内存泄漏。我知道在编码方面你比我更有能力,我很感激你的帮助,但这对我没有任何帮助。
【解决方案2】:

好的,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;
}

祝大家晚上愉快!

【讨论】:

    猜你喜欢
    • 2020-07-20
    • 1970-01-01
    • 2019-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-11
    相关资源
    最近更新 更多