|
You last visited: Today at 06:58
Advertisement
Valgrind Error
Discussion on Valgrind Error within the C/C++ forum part of the Coders Den category.
05/29/2016, 16:36
|
#1
|
elite*gold: 15
Join Date: Jul 2010
Posts: 3,926
Received Thanks: 1,158
|
Valgrind Error
Wie der Titel bereits vermuten lässt, zeigt mir Valgrind einen Fehler an, welchen ich einfach nicht finde.
Here is what i wrote so far:
Code:
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;
}
base64_decode:
Code:
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)
Ich erwarte hier von keinem meinen code vollständig zu überarbeiten, ich möchte lediglich wissen warum ich den ersten fehler(und damit denke ich folgefehler) bekomme.
Mittlerweile sitze ich hier seit knappen 30stunden dran diesen fehler zu finden(-schlaf), habe auch schon verschiedene personen gefragt & es auch schon auf stackoverflow gepostet, wo mir aber keiner helfen konnte.
|
|
|
05/29/2016, 17:42
|
#2
|
elite*gold: 77
Join Date: May 2008
Posts: 5,430
Received Thanks: 5,878
|
Meine Vermutung liegt hier:
Wenn man in deinem Code einmal alle möglichen Verzweigungen und Sprünge im Kopf durchgeht, sieht man sehr schnell, dass da etwas gewaltig schief gehen kann, nämlich wenn man die Befehle in folgender Reihenfolge abarbeitet:
Code:
int aValue;
// ...
temp=aValue;
// ...
decoded[threecharC]=temp;
Da siehst du dann, dass du in decoded eine noch nicht initialisierte Variable reinhaust vom Typ int. Das heißt damit ist an einer Stelle im decoded-Array ein nicht initialisierter Wert vorhanden. Das merkt man aber erst, wenn der Wert tatsächlich für Vergleiche o.ä. benutzt wird.
Scheinbar wird in "strchr" verglichen (muss ja auch, ist also nix überraschendes  ), da aber in decoded min. ein Element nicht initialisiert ist, schmeißt er dir an genau dieser Stelle einen Fehler.
Am besten alle Variable initialisieren und dann noch einmal testen.
Kleine Anmerkung noch am Rande:
Ein paar schönere Variablennamen schaden auch nicht. :P
|
|
|
05/29/2016, 18:31
|
#3
|
elite*gold: 15
Join Date: Jul 2010
Posts: 3,926
Received Thanks: 1,158
|
Du bist mein held, dadurch sind alle(bis auf ein speicher fehler) valgrind fehler behoben! (die logischen kann man dann jetzt verbesser)
VIELEN DANK!
ich hab echt seit heute morgen um 10 über stackoverflow und ähnliches versucht die fehler zu beheben(alleine sogar schon seit gestern mittag) und niemand kam auf diese idee!
|
|
|
05/30/2016, 01:59
|
#4
|
elite*gold: 0
Join Date: Jun 2008
Posts: 451
Received Thanks: 410
|
Durch genau so einen Fehler, wirst du wahrscheinlich nie wieder einer Variable bei der Deklarierung keine Initialisierung geben  hat aber jeder schonmal durch gemacht
|
|
|
05/30/2016, 18:22
|
#5
|
elite*gold: 0
Join Date: Feb 2011
Posts: 1,206
Received Thanks: 736
|
praktisch jede vernünftige ide heutzutage gibt einem da schon sehr explizite warnungen.
|
|
|
 |
Similar Threads
|
[ENG] Error: Error 1 error C2601: 'HackMain' local function definitions are illegal
08/20/2013 - C/C++ - 1 Replies
So, as the title says I've been encountering an error
Error 1 error C2601: 'HackMain' local function definitions are illegal
I'm running on VC++ (Visual C++)
This is my code:
void HackMain()
{
for (;; )
{
HackThread();
}
|
İbot Error-Error Video- Error İmages-HELP
04/10/2012 - DarkOrbit - 11 Replies
SORRY, MY ENGLİSH VERY BAD.I USE TO GOOGLE TRANSLATE :)
Most people trying to ibot but in my computer İbot not working.
Declared out this error everywhere but I do not get answers
Here's the error Video
http://youtu.be/q0fK09v-K3c
|
API Error Code: 100 API Error Description: Invalid parameter Error Message: redirect_
04/08/2012 - elite*gold Trading - 2 Replies
API Error Code: 100
API Error Description: Invalid parameter
Error Message: redirect_uri URL is not properly formatted
Das bekomme ich wenn ich ne App installiere... ich habe schon 3 Apps richtig installiert, danach kam immer das bei anderen Apps die ich installiert habe..
was heisst das? redirect_uri URL is not properly formatted
|
All times are GMT +1. The time now is 06:58.
|
|