title: C Valgrind Conditional jump or move
body: <p>as the title suggests i have a problem with Valgrind where i get some errors that variables are uninitialised.
  Here is what i wrote so far:</p>
  
  <pre><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 &amp;&amp; pass != NULL){
      printf("Username: %s\n", username); //Fourth Uninitalised error
      printf("Password: %s\n", pass); //Invalid read of size 1
  }
  
  
  SHA1_Init(&amp;context);
  SHA1_Update(&amp;context, (uint8_t *) pass, strlen(pass)); //invalid read of size 1
  SHA1_Final(&amp;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;
  </code></pre>
  
  <p>}</p>
  
  <p>I don't expect any of you guys to fix all my mistakes at once, i'd just like to understand why i get the first uninitialised error from valgrind since i have been trying to fix it for a mere 30hours(minus sleep) and i just don't see what my mistake is.</p>
  
  <p>Thank you guys in advance!</p>
  
  <p>Edit:
  base64_decode:</p>
  
  <pre><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&lt;(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&lt;7;sc++){                                                //base64 char Wert in binär
              octets[s-sc]=n%2;
              n=n/2;
  
          }
          for(sc=0;sc&lt;6;sc++){                                                //Ausgabe des Binärwertes in Konsole (Debug)
              //printf("%d",octets[s-6+sc]);
          }
      }else{
          for(sc=1;sc&lt;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&lt;=24;deLoop=deLoop+8){
              for(decodeC=1;decodeC&lt;=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;
  </code></pre>
  
  <p>}</p>
  
  <pre><code>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)  
  </code></pre>
  
  <p>This is not a full log, if you want the full log i'll post it</p>