Thread: Valgrind: Conditional jump or move depends on uninitialised value(s)

  1. #1
    Registered User
    Join Date
    Jan 2019
    Posts
    2

    Valgrind: Conditional jump or move depends on uninitialised value(s)

    Hi

    When I check my c code with valgrind --track-origins=yes I get this error.

    Code:
    ==4649== Conditional jump or move depends on uninitialised value(s)
    ==4649==    at 0x4C37928: strcspn (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==4649==    by 0x4EDAB94: strtok_r (strtok_r.c:64)
    ==4649==    by 0x108D36: readDirectory (ps.c:132)
    ==4649==    by 0x108A9F: main (ps.c:37)
    ==4649==  Uninitialised value was created by a stack allocation
    ==4649==    at 0x108ACF: readDirectory (ps.c:52)
    ==4649== 
    ==4649== Conditional jump or move depends on uninitialised value(s)
    ==4649==    at 0x4EDAB9B: strtok_r (strtok_r.c:65)
    ==4649==    by 0x108D36: readDirectory (ps.c:132)
    ==4649==    by 0x108A9F: main (ps.c:37)
    ==4649==  Uninitialised value was created by a stack allocation
    ==4649==    at 0x108ACF: readDirectory (ps.c:52)
    ==4649== 
    ==4649== Conditional jump or move depends on uninitialised value(s)
    ==4649==    at 0x4C32D08: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==4649==    by 0x4E994D2: vfprintf (vfprintf.c:1643)
    ==4649==    by 0x4EA0F25: printf (printf.c:33)
    ==4649==    by 0x108DDF: printOutput (ps.c:148)
    ==4649==    by 0x108D5F: readDirectory (ps.c:134)
    ==4649==    by 0x108A9F: main (ps.c:37)
    ==4649==  Uninitialised value was created by a stack allocation
    ==4649==    at 0x108ACF: readDirectory (ps.c:52)
    This is a snippet from my Code.

    Code:
    void readDirectory(char pid[]){
    
      char path[50];
      char buffer[128];
    
      char name[128];
      static char *nameOutput;
      char *saveName = name;
    
      char vmrss[128];
      static char *vmrssOutput;
      char *saveVmrss = vmrss;
    
      while(fgets(buffer, sizeof(buffer), fptr)){
                if(strstr(buffer, "Name:")){
    
                  strcpy(name, buffer);
                  nameOutput = strtok_r(name, ":", &saveName);
                  nameOutput = strtok_r(NULL, ":", &saveName);
     
    
    
                  for(unsigned int i = strcspn (nameOutput, "\n"); i < strlen(nameOutput); i++){
                    nameOutput[i] = 0;
                  }
    
                } else if(strstr(buffer, "Uid:") && strstr(buffer, sUserID)){
                    isUse = true;
    
    
                    }else if(strstr(buffer, "VmRSS:")){
                          strncpy(vmrss, buffer, strlen(buffer) - 3);
                          vmrssOutput = strtok_r(vmrss, ":", &saveVmrss);
                          vmrssOutput = strtok_r(NULL, ":", &saveVmrss);
    
                          printOutput(isUse, pid, nameOutput, vmrssOutput);
                        }
    I think the problem is, that i didn't initialized the variable char *saveVmrss = vmrss;
    How can I solve this?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,082
    For starters, you need to format your code better, e.g.,
    Code:
    void readDirectory(char pid[]) {
        char path[50];
        char buffer[128];
    
        char name[128];
        static char *nameOutput;
        char *saveName = name;
    
        char vmrss[128];
        static char *vmrssOutput;
        char *saveVmrss = vmrss;
    
        while (fgets(buffer, sizeof(buffer), fptr)) {
            if (strstr(buffer, "Name:")) {
                strcpy(name, buffer);
                nameOutput = strtok_r(name, ":", &saveName);
                nameOutput = strtok_r(NULL, ":", &saveName);
    
                for (unsigned int i = strcspn(nameOutput, "\n"); i < strlen(nameOutput); i++) {
                    nameOutput[i] = 0;
                }
            } else if (strstr(buffer, "Uid:") && strstr(buffer, sUserID)) {
                isUse = true;
            } else if (strstr(buffer, "VmRSS:")) {
                strncpy(vmrss, buffer, strlen(buffer) - 3);
                vmrssOutput = strtok_r(vmrss, ":", &saveVmrss);
                vmrssOutput = strtok_r(NULL, ":", &saveVmrss);
    
                printOutput(isUse, pid, nameOutput, vmrssOutput);
            }
    I note that you seem to have cut off the code before the end of the function, so it is hard to make recommendations as it is not known what comes next in the function.

    Anyway, it looks like your use of the strtok_r state pointers is wrong. You should have written:
    Code:
    char name[128];
    char *nameOutput;
    char *saveName;
    
    char vmrss[128];
    char *vmrssOutput;
    char *saveVmrss;
    I removed the use of static because there is no evidence that you need static local variables here: what nameOutput points to will not be overwritten just because it is not static; it will be overwritten because the content of name is overwritten, as nameOutput will point to somewhere in name due to the strtok_r call.

    You don't need that for loop with the strcspn call: you can just use the result of strcspn to overwrite the character at that index with a null character. Also, use '\0', not 0, for null characters.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,422
    > strncpy(vmrss, buffer, strlen(buffer) - 3);
    Another issue might be that strncpy does not automatically append a \0 on the end of whatever it copies.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jan 2019
    Posts
    2
    Quote Originally Posted by Salem View Post
    > strncpy(vmrss, buffer, strlen(buffer) - 3);
    Another issue might be that strncpy does not automatically append a \0 on the end of whatever it copies.
    Thank you very much. You were rigth, the problem was the strncpy(vmrss, buffer, strlen(buffer) - 3).

    When I didn't define char*nameOutput as static, the gcc throws a warning. Therefore I define it as static.
    Now I haven't any errors from gcc and valgrind.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 11-01-2016, 07:13 AM
  2. Why does x = 0 if it's uninitialised
    By jim_0 in forum C++ Programming
    Replies: 5
    Last Post: 11-29-2013, 03:03 PM
  3. Conditional jump depends on unitialized value Valgrind
    By Ivan Novák in forum C++ Programming
    Replies: 5
    Last Post: 01-11-2013, 06:55 PM
  4. Replies: 5
    Last Post: 01-15-2011, 12:31 PM
  5. Replies: 3
    Last Post: 05-23-2010, 07:32 AM

Tags for this Thread