C Valgrind 条件跳转或移动

标签 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 小时(减去) sleep ),我只是不明白我的错误是什么。

先谢谢大家了!

编辑: base64_解码:

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)  

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

最佳答案

您使用的指针就像存储一样。例如

char* passlocation = NULL;

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

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

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

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

关于C Valgrind 条件跳转或移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37508714/

相关文章:

c - 如何理解C99标准语法

c - 在 C 中释放二维数组(检查指针是否为 NULL)

c - (windows) DevC++ GCC 编译器挂起或打印无限字符

c - 如何在 linux(gcc) 中将 int 转换为 char/string,反之亦然?

c - 在 Debian 上使用 gcc 4.7.2-5 无效释放/删除,而在 Ubuntu/Linaro 上使用 gcc 4.8.1 一切正常

c++ - 全局分配的内存会发生什么?

c - 由于 RDRAND,OpenSSL 在 Valgrind 下生成核心转储

c - valgrind : Conditional jump or move depends on uninitialised value using strlen() strncat()

c++ - c/c++中指针的内存开销

c 遍历一个数组