getline throwing uninitialized value in valgrind
by jmgibson1981 from LinuxQuestions.org on (#6EW2Y)
Relavant code.
Code:#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <jmgeneral.h>
#define TESTFILE "testfile.txt"
int line_num_chars(char * str);
int main()
{
FILE * file = fopen(TESTFILE, "r");
int max_chars = 0;
int len = 0;
char * buffer = NULL;
bool keepgoing = true;
while (keepgoing) {
char * tempbuf = getline_mem_alloc(file);
// allocate second memory. trying to make valgrind happy
buffer = allocate_string_mem(tempbuf);
// clear temp buffer asap to limit memory usage
free(tempbuf);
tempbuf = NULL;
len = strlen(buffer);
if (len > max_chars) {
max_chars = len;
}
free(buffer);
buffer = NULL;
if (feof(file)) {
keepgoing = false;
}
}
fclose(file);
file = NULL;
printf("total top = %d\n", max_chars);
return(0);
}Code:char * getline_mem_alloc(FILE * file)
{
assert(file);
// declare & initialize
size_t buflen = 0;
char * buffer = NULL;
buflen = getline(&buffer,
&buflen,
file);
// return buffer
return(buffer);
}Code:char * allocate_string_mem(const char * str)
{
assert(str);
int chars = 0;
char * retstr = (char*)malloc(strlen(str) * sizeof(char) + 1);
for (int i = 0; i <= strlen(str); i++) {
retstr[i] = '\0';
chars++;
}
strncpy(retstr,
str,
chars);
assert(retstr);
return(retstr);
}Output
Code:valgrind -s --leak-check=full --track-origins=yes ./a.out
==48843== Memcheck, a memory error detector
==48843== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==48843== Using Valgrind-3.19.0 and LibVEX; rerun with -h for copyright info
==48843== Command: ./a.out
==48843==
==48843== Conditional jump or move depends on uninitialised value(s)
==48843== at 0x48467E5: __strlen_sse2 (vg_replace_strmem.c:496)
==48843== by 0x48543B2: allocate_string_mem (in /usr/local/lib/libjmgeneral.so)
==48843== by 0x109205: main (in /home/jason/Documents/a.out)
==48843== Uninitialised value was created by a heap allocation
==48843== at 0x48407B4: malloc (vg_replace_malloc.c:381)
==48843== by 0x48DFBAE: getdelim (iogetdelim.c:62)
==48843== by 0x4853C79: getline_mem_alloc (in /usr/local/lib/libjmgeneral.so)
==48843== by 0x1091F5: main (in /home/jason/Documents/a.out)
==48843==
==48843== Conditional jump or move depends on uninitialised value(s)
==48843== at 0x4846DF3: __strncpy_sse2_unaligned (vg_replace_strmem.c:602)
==48843== by 0x48543E7: allocate_string_mem (in /usr/local/lib/libjmgeneral.so)
==48843== by 0x109205: main (in /home/jason/Documents/a.out)
==48843== Uninitialised value was created by a heap allocation
==48843== at 0x48407B4: malloc (vg_replace_malloc.c:381)
==48843== by 0x48DFBAE: getdelim (iogetdelim.c:62)
==48843== by 0x4853C79: getline_mem_alloc (in /usr/local/lib/libjmgeneral.so)
==48843== by 0x1091F5: main (in /home/jason/Documents/a.out)
==48843==
total top = 29
==48843==
==48843== HEAP SUMMARY:
==48843== in use at exit: 0 bytes in 0 blocks
==48843== total heap usage: 13 allocs, 13 frees, 6,274 bytes allocated
==48843==
==48843== All heap blocks were freed -- no leaks are possible
==48843==
==48843== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
==48843==
==48843== 1 errors in context 1 of 2:
==48843== Conditional jump or move depends on uninitialised value(s)
==48843== at 0x4846DF3: __strncpy_sse2_unaligned (vg_replace_strmem.c:602)
==48843== by 0x48543E7: allocate_string_mem (in /usr/local/lib/libjmgeneral.so)
==48843== by 0x109205: main (in /home/jason/Documents/a.out)
==48843== Uninitialised value was created by a heap allocation
==48843== at 0x48407B4: malloc (vg_replace_malloc.c:381)
==48843== by 0x48DFBAE: getdelim (iogetdelim.c:62)
==48843== by 0x4853C79: getline_mem_alloc (in /usr/local/lib/libjmgeneral.so)
==48843== by 0x1091F5: main (in /home/jason/Documents/a.out)
==48843==
==48843==
==48843== 1 errors in context 2 of 2:
==48843== Conditional jump or move depends on uninitialised value(s)
==48843== at 0x48467E5: __strlen_sse2 (vg_replace_strmem.c:496)
==48843== by 0x48543B2: allocate_string_mem (in /usr/local/lib/libjmgeneral.so)
==48843== by 0x109205: main (in /home/jason/Documents/a.out)
==48843== Uninitialised value was created by a heap allocation
==48843== at 0x48407B4: malloc (vg_replace_malloc.c:381)
==48843== by 0x48DFBAE: getdelim (iogetdelim.c:62)
==48843== by 0x4853C79: getline_mem_alloc (in /usr/local/lib/libjmgeneral.so)
==48843== by 0x1091F5: main (in /home/jason/Documents/a.out)
==48843==
==48843== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)I've tried many variations thinking it was something with my functions. I can do this via fgets no problem no errors. but something about my getline here. I've googled, seems to be an occasional problem but haven't found a solution that works yet.
Code:#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <jmgeneral.h>
#define TESTFILE "testfile.txt"
int line_num_chars(char * str);
int main()
{
FILE * file = fopen(TESTFILE, "r");
int max_chars = 0;
int len = 0;
char * buffer = NULL;
bool keepgoing = true;
while (keepgoing) {
char * tempbuf = getline_mem_alloc(file);
// allocate second memory. trying to make valgrind happy
buffer = allocate_string_mem(tempbuf);
// clear temp buffer asap to limit memory usage
free(tempbuf);
tempbuf = NULL;
len = strlen(buffer);
if (len > max_chars) {
max_chars = len;
}
free(buffer);
buffer = NULL;
if (feof(file)) {
keepgoing = false;
}
}
fclose(file);
file = NULL;
printf("total top = %d\n", max_chars);
return(0);
}Code:char * getline_mem_alloc(FILE * file)
{
assert(file);
// declare & initialize
size_t buflen = 0;
char * buffer = NULL;
buflen = getline(&buffer,
&buflen,
file);
// return buffer
return(buffer);
}Code:char * allocate_string_mem(const char * str)
{
assert(str);
int chars = 0;
char * retstr = (char*)malloc(strlen(str) * sizeof(char) + 1);
for (int i = 0; i <= strlen(str); i++) {
retstr[i] = '\0';
chars++;
}
strncpy(retstr,
str,
chars);
assert(retstr);
return(retstr);
}Output
Code:valgrind -s --leak-check=full --track-origins=yes ./a.out
==48843== Memcheck, a memory error detector
==48843== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==48843== Using Valgrind-3.19.0 and LibVEX; rerun with -h for copyright info
==48843== Command: ./a.out
==48843==
==48843== Conditional jump or move depends on uninitialised value(s)
==48843== at 0x48467E5: __strlen_sse2 (vg_replace_strmem.c:496)
==48843== by 0x48543B2: allocate_string_mem (in /usr/local/lib/libjmgeneral.so)
==48843== by 0x109205: main (in /home/jason/Documents/a.out)
==48843== Uninitialised value was created by a heap allocation
==48843== at 0x48407B4: malloc (vg_replace_malloc.c:381)
==48843== by 0x48DFBAE: getdelim (iogetdelim.c:62)
==48843== by 0x4853C79: getline_mem_alloc (in /usr/local/lib/libjmgeneral.so)
==48843== by 0x1091F5: main (in /home/jason/Documents/a.out)
==48843==
==48843== Conditional jump or move depends on uninitialised value(s)
==48843== at 0x4846DF3: __strncpy_sse2_unaligned (vg_replace_strmem.c:602)
==48843== by 0x48543E7: allocate_string_mem (in /usr/local/lib/libjmgeneral.so)
==48843== by 0x109205: main (in /home/jason/Documents/a.out)
==48843== Uninitialised value was created by a heap allocation
==48843== at 0x48407B4: malloc (vg_replace_malloc.c:381)
==48843== by 0x48DFBAE: getdelim (iogetdelim.c:62)
==48843== by 0x4853C79: getline_mem_alloc (in /usr/local/lib/libjmgeneral.so)
==48843== by 0x1091F5: main (in /home/jason/Documents/a.out)
==48843==
total top = 29
==48843==
==48843== HEAP SUMMARY:
==48843== in use at exit: 0 bytes in 0 blocks
==48843== total heap usage: 13 allocs, 13 frees, 6,274 bytes allocated
==48843==
==48843== All heap blocks were freed -- no leaks are possible
==48843==
==48843== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
==48843==
==48843== 1 errors in context 1 of 2:
==48843== Conditional jump or move depends on uninitialised value(s)
==48843== at 0x4846DF3: __strncpy_sse2_unaligned (vg_replace_strmem.c:602)
==48843== by 0x48543E7: allocate_string_mem (in /usr/local/lib/libjmgeneral.so)
==48843== by 0x109205: main (in /home/jason/Documents/a.out)
==48843== Uninitialised value was created by a heap allocation
==48843== at 0x48407B4: malloc (vg_replace_malloc.c:381)
==48843== by 0x48DFBAE: getdelim (iogetdelim.c:62)
==48843== by 0x4853C79: getline_mem_alloc (in /usr/local/lib/libjmgeneral.so)
==48843== by 0x1091F5: main (in /home/jason/Documents/a.out)
==48843==
==48843==
==48843== 1 errors in context 2 of 2:
==48843== Conditional jump or move depends on uninitialised value(s)
==48843== at 0x48467E5: __strlen_sse2 (vg_replace_strmem.c:496)
==48843== by 0x48543B2: allocate_string_mem (in /usr/local/lib/libjmgeneral.so)
==48843== by 0x109205: main (in /home/jason/Documents/a.out)
==48843== Uninitialised value was created by a heap allocation
==48843== at 0x48407B4: malloc (vg_replace_malloc.c:381)
==48843== by 0x48DFBAE: getdelim (iogetdelim.c:62)
==48843== by 0x4853C79: getline_mem_alloc (in /usr/local/lib/libjmgeneral.so)
==48843== by 0x1091F5: main (in /home/jason/Documents/a.out)
==48843==
==48843== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)I've tried many variations thinking it was something with my functions. I can do this via fgets no problem no errors. but something about my getline here. I've googled, seems to be an occasional problem but haven't found a solution that works yet.