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.