Conditional jump or moves depends on uninitialised value(s) - strcat

521 views Asked by At

I've read all the answers about this but I can't fix it. Valgrind keeps telling me:

    ==9934== Conditional jump or move depends on uninitialised value(s)
    ==9934==    at 0x4C2D2DB: __GI_strcat (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==9934==    by 0x402036: main (in /home/doge/Tarea3/http)

Here is when my code fails:

        // buffer which gets the file
        char buf[1024];
        //socket
        struct sockaddr_in *remote;
        // socket port
        int sock;
        // ALGP
        int tmpres;
        //ip as string
        char *ip;
        //final query
        char *get;

        // host -> url
        char* host=url;

        //resource (Ex index.html)
        char *page;
        page=PAGE;

        char *ruta = NULL;


        if(url_long>0){
            ruta = (char *)malloc((url_long+12)*sizeof(char));
        }



        strcat(ruta,url);
        if(command==1){
            strcat(ruta,"GET.txt\0");

        }
        else if(command==2){
            strcat(ruta,"POST.txt\0");
        }
        else if(command==3){

            strcat(ruta,"HEAD.txt\0");
        }

        FILE *fp = fopen(ruta,"r");

        memset(buf, 0, sizeof(buf)+1);
        size_t nread=0;

URL was declared before:

    char *url=NULL;
    if(url_long>0)
    url = (char *)malloc((url_long+1)*sizeof(char));

Apparently, this is causing that my server would not be able to write in the client socket. I donĀ“t know why, but when it is running with Valgrind everything works ok. Thank you

EDIT: Ran the Valgrind with --track-origins=yes and this is what I got:

   ==11366== Conditional jump or move depends on uninitialised value(s)
   ==11366==    at 0x4C2D6EB: __GI_strcpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
   ==11366==    by 0x4020D6: main (in /home/doge/Tarea3/http)
   ==11366==  Uninitialised value was created by a heap allocation
   ==11366==    at 0x4C29F90: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
   ==11366==    by 0x401DFB: main (in /home/doge/Tarea3/http)
0

There are 0 answers