Thread: Valgrind complaining of unitialized variable with strstr

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    6

    Valgrind complaining of unitialized variable with strstr

    This is my first post on this forum so I apologize if I do something wrong.

    As explained in my title, my code produces the desired result, but valgrind complains of using an uninitialized variable when running. Here's the code; I simplified it, changing some functionality and removing error handling for the sake of brevity and clarity, but still producing the same error:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define INDEX "/tmp/index.html"
    #define DURL "http://bay12games.com/dwarves/"
    char *getDownloadURL (char *haystack) { 
       char *a = strstr(haystack,"\">Linux</a>");
       size_t len = a - haystack;
       char intermediary[len+1];
       memcpy(intermediary,haystack,len);
       a = strrchr(intermediary,'\"');
       len = strlen(a + 1) + 1; //Re-define.
       char *stringToReturn = malloc(strlen (DURL) + len);
       memcpy(stringToReturn,DURL,strlen(DURL)+1);
       strcat(stringToReturn,a+1);
    
       return stringToReturn;
    }
    
    int main() {
       FILE *f = fopen(INDEX, "r");
       fseek(f,0,SEEK_END);
       long len = ftell(f);
       fseek(f,0,SEEK_SET);
       char *page = malloc(len+1);
       fread(page,1,len,f);
       fclose(f);
       page[len] = 0;
       char *URL = getDownloadURL(page);
       printf("%s",URL);
       free(URL);
       free(page);
       return 0;
    
    }
    The full Valgrind error is:

    [spoiler]
    Code:
    =115998== Memcheck, a memory error detector
    ==115998== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
    ==115998== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
    ==115998== Command: ./debug -s
    ==115998== 
    fread success.
    ==115998== Conditional jump or move depends on uninitialised value(s)
    ==115998==    at 0x483B7C8: rindex (vg_replace_strmem.c:202)
    ==115998==    by 0x10932E: getDownloadURL (debug.c:23)
    ==115998==    by 0x1094C4: main (debug.c:51)
    ==115998==  Uninitialised value was created by a stack allocation
    ==115998==    at 0x109276: getDownloadURL (debug.c:7)
    ==115998== 
    ==115998== Conditional jump or move depends on uninitialised value(s)
    ==115998==    at 0x109338: getDownloadURL (debug.c:24)
    ==115998==    by 0x1094C4: main (debug.c:51)
    ==115998==  Uninitialised value was created by a stack allocation
    ==115998==    at 0x109276: getDownloadURL (debug.c:7)
    ==115998== 
    ==115998== Use of uninitialised value of size 8
    ==115998==    at 0x483BC82: strlen (vg_replace_strmem.c:461)
    ==115998==    by 0x109370: getDownloadURL (debug.c:28)
    ==115998==    by 0x1094C4: main (debug.c:51)
    ==115998==  Uninitialised value was created by a stack allocation
    ==115998==    at 0x109276: getDownloadURL (debug.c:7)
    ==115998== 
    ==115998== Use of uninitialised value of size 8
    ==115998==    at 0x483BC94: strlen (vg_replace_strmem.c:461)
    ==115998==    by 0x109370: getDownloadURL (debug.c:28)
    ==115998==    by 0x1094C4: main (debug.c:51)
    ==115998==  Uninitialised value was created by a stack allocation
    ==115998==    at 0x109276: getDownloadURL (debug.c:7)
    ==115998== 
    ==115998== Conditional jump or move depends on uninitialised value(s)
    ==115998==    at 0x483BC98: strlen (vg_replace_strmem.c:461)
    ==115998==    by 0x109370: getDownloadURL (debug.c:28)
    ==115998==    by 0x1094C4: main (debug.c:51)
    ==115998==  Uninitialised value was created by a stack allocation
    ==115998==    at 0x109276: getDownloadURL (debug.c:7)
    ==115998== 
    ==115998== Use of uninitialised value of size 8
    ==115998==    at 0x483B979: strcat (vg_replace_strmem.c:310)
    ==115998==    by 0x1093BB: getDownloadURL (debug.c:31)
    ==115998==    by 0x1094C4: main (debug.c:51)
    ==115998==  Uninitialised value was created by a stack allocation
    ==115998==    at 0x109276: getDownloadURL (debug.c:7)
    ==115998== 
    ==115998== Conditional jump or move depends on uninitialised value(s)
    ==115998==    at 0x483B997: strcat (vg_replace_strmem.c:310)
    ==115998==    by 0x1093BB: getDownloadURL (debug.c:31)
    ==115998==    by 0x1094C4: main (debug.c:51)
    ==115998==  Uninitialised value was created by a stack allocation
    ==115998==    at 0x109276: getDownloadURL (debug.c:7)
    ==115998== 
    ==115998== Conditional jump or move depends on uninitialised value(s)
    ==115998==    at 0x483B9B5: is_overlap (vg_replace_strmem.c:131)
    ==115998==    by 0x483B9B5: strcat (vg_replace_strmem.c:310)
    ==115998==    by 0x1093BB: getDownloadURL (debug.c:31)
    ==115998==    by 0x1094C4: main (debug.c:51)
    ==115998==  Uninitialised value was created by a stack allocation
    ==115998==    at 0x109276: getDownloadURL (debug.c:7)
    ==115998== 
    ==115998== Conditional jump or move depends on uninitialised value(s)
    ==115998==    at 0x483B9BA: is_overlap (vg_replace_strmem.c:140)
    ==115998==    by 0x483B9BA: is_overlap (vg_replace_strmem.c:127)
    ==115998==    by 0x483B9BA: strcat (vg_replace_strmem.c:310)
    ==115998==    by 0x1093BB: getDownloadURL (debug.c:31)
    ==115998==    by 0x1094C4: main (debug.c:51)
    ==115998==  Uninitialised value was created by a stack allocation
    ==115998==    at 0x109276: getDownloadURL (debug.c:7)
    ==115998== 
    ==115998== Conditional jump or move depends on uninitialised value(s)
    ==115998==    at 0x483B9BC: is_overlap (vg_replace_strmem.c:143)
    ==115998==    by 0x483B9BC: is_overlap (vg_replace_strmem.c:127)
    ==115998==    by 0x483B9BC: strcat (vg_replace_strmem.c:310)
    ==115998==    by 0x1093BB: getDownloadURL (debug.c:31)
    ==115998==    by 0x1094C4: main (debug.c:51)
    ==115998==  Uninitialised value was created by a stack allocation
    ==115998==    at 0x109276: getDownloadURL (debug.c:7)
    ==115998== 
    ==115998== Conditional jump or move depends on uninitialised value(s)
    ==115998==    at 0x483B9CB: strcat (vg_replace_strmem.c:310)
    ==115998==    by 0x1093BB: getDownloadURL (debug.c:31)
    ==115998==    by 0x1094C4: main (debug.c:51)
    ==115998==  Uninitialised value was created by a stack allocation
    ==115998==    at 0x109276: getDownloadURL (debug.c:7)
    ==115998== 
    http://bay12games.com/dwarves/df_44_12_linux.tar.bz2==115998== 
    ==115998== HEAP SUMMARY:
    ==115998==     in use at exit: 0 bytes in 0 blocks
    ==115998==   total heap usage: 5 allocs, 5 frees, 440,956 bytes allocated
    ==115998== 
    ==115998== All heap blocks were freed -- no leaks are possible
    ==115998== 
    ==115998== For lists of detected and suppressed errors, rerun with: -s
    [/spoiler]

    Line 7, which it seems to complain about, is:

    Code:
    char *a = strstr(haystack,"\">Linux</a>");
    Any ideas? I'm a C noob, so I'm stumped.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,537
    Code:
       char intermediary[len+1];
       memcpy(intermediary,haystack,len);
       a = strrchr(intermediary,'\"');
    memcpy doesn't put \0 characters on the ends of strings.
    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.

  3. #3
    Registered User
    Join Date
    Oct 2019
    Posts
    6
    Quote Originally Posted by Salem View Post
    Code:
       char intermediary[len+1];
       memcpy(intermediary,haystack,len);
       a = strrchr(intermediary,'\"');
    memcpy doesn't put \0 characters on the ends of strings.
    My god, of course. Thank you so much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 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
  2. vector::size() unitialized at creation
    By VirtualAce in forum C++ Programming
    Replies: 9
    Last Post: 03-08-2008, 01:48 AM
  3. Checking unitialized class pointer
    By cunnus88 in forum C++ Programming
    Replies: 7
    Last Post: 11-16-2005, 05:36 PM
  4. Mingw not complaining
    By arjunajay in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:45 AM
  5. Yes- I'm complaining about DirectX
    By CodeMonkey in forum Game Programming
    Replies: 4
    Last Post: 07-01-2003, 12:30 PM

Tags for this Thread