Valgrind error: conditional jump or move depends on uninitialised value(s) - C

Question

I am trying to write a program that receives messages and reads them. Based on what those messages are it then prints out some text. As that is the part of the code that valgrind is warning me about, I left it there for you to see. As far as I read online, there is supposed to be an initialization problem but I do not know where.

int main(int argc, char* argv[])
{
    int oprimek = 0;
    char name[] = "/cmdQueue";
    char msg[255];
    msg[256]='\0';
    char submsg1[5];
    submsg1[6]='\0';
    char submsg2[250];
    submsg2[251]='\0';
    int len=0;
    int x=0;
    struct mq_attr atr;
    atr.mq_maxmsg = 5; 
    atr.mq_msgsize = 255;
    oprimek = mq_open(name,O_RDWR|O_CREAT|O_EXCL,0660, &atr);
    if(oprimek == -1){
        perror("error");
        mq_unlink(name);
        return -1;
    }
    time_t sek;
    int size=0;
    while(1){
    mq_getattr(oprimek,&atr); 
    if(atr.mq_curmsgs>0){
        size=mq_receive(oprimek, msg, atr.mq_msgsize,0);
        if(size==0){
            printf("Length is 0.");
        }else{
            len=strlen(msg);
            msg[len]='\0';
            strncpy(submsg1,msg,5);
            submsg1[5]='\0';
            if(strcmp(msg, "datum")==0){
                time(&sek);
                printf("%s", ctime(&sek));
            }
            else if(strcmp(submsg1,"izpis")==0){
                x=0;
                while(x<len){
                    submsg2[x]=msg[5+x+1];
                    x++;
                }
                printf("%s\n",submsg2);
            }
            else if(strcmp(msg,"zakljuci")==0){
                printf("Turning off!\n");
                return 1;
            }
            else{
                printf("Unknown message: %s \n",msg);
            }
        }
    }   
    }
    mq_close(oprimek);
    mq_unlink(name);
    return 0;
}

Show source
| C   | arrays   | valgrind   2017-01-07 19:01 3 Answers

Answers to Valgrind error: conditional jump or move depends on uninitialised value(s) - C ( 3 )

  1. 2017-01-07 19:01

    The bigger problem, as I see it is with

     msg[256]='\0';
     submsg1[6]='\0';
     submsg2[251]='\0';
    

    whereas you defined the arrays to be of length 255, 5 and 250, respectively. The valid access is for index 0 to size-1.

    Here, you're accessing out of bound memory which invokes undefined behavior. Nothing is guaranteed once you hit UB.

  2. 2017-01-07 20:01

    In addition to the errors pointed out by others:

    if(atr.mq_curmsgs>0)

    the above code will cause valgrind to complain about a conditional jump on an uninitialized value since you have not assigned a value to atr.mq_curmsgs, unless it is done in the call to mq_getattr(oprimek,&atr).

  3. 2017-01-07 22:01

    I solved the issue by writing

    memset(msg,0,sizeof(msg))
    

    right under the declaration.

Leave a reply to - Valgrind error: conditional jump or move depends on uninitialised value(s) - C

◀ Go back