[SOLVED] getline throwing uninitialized value in valgrind
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
Valgrind knows the memory has been allocated within getline() and assigned to pointer buffer, but seems unaware that it has been written by getline(). Any attempt to read it produces a valgrind error, but you can write to it and once you do the program completes and valgrind says all is OK. For example, add strcpy(buffer,"some text") immediately after the call to getline() (be sure not to exceed buflen).
I am not a frequent valgrind user so cannot say whether this is to be expected or why it happens. My searches turned up other odd valgrind behaviors with getline, some version dependent, but not this exactly.
Hope it helps.
Last edited by astrogeek; 09-18-2023 at 08:17 PM.
Reason: grammar
Ok. I tried a few more things. And I dumped those 2 alloc functions I had written and replaced with strdup in my library.
Thank you both.
It works fine if I don't try to operate on the tempbuf return. No errors. But as soon as I want to run an operation on it, strlen or whatever so far seems to toss that error.
Last edited by jmgibson1981; 09-18-2023 at 09:09 PM.
It might give more information, if you compile your shared library with option `-g`.
Or for debugging you could directly link your rutins into the executable.
PS: don't do this with 'buflen':
Code:
buflen = getline(&buffer, &buflen, file);
Instead add error-checks after `fopen` and `getline`
Ok done. The main function is just me fiddling around. I normally run tests on file ops. I did replace that buflen thing with a test with error message & bail out on fail.
Could not get this off my mind as I did not really understand it on first look, and knew it, and it did not seem reasonable that valgrind would miss an initialization.
NevemTeve is on the right track. You need to test for unsuccessful call to getline(...), it returns -1 for both errors and for EOF, and it is the EOF that is causing the problem.
You are not using buflen or the returned character count, so just test for -1.
The acual cause of the valgrind error is, even though unsuccessful or EOF, a buffer will have been allocated and needs to be free()d and not further used.
So, along with the change from allocate_string_mem() to strdup() and a test of returned pointer, this should fix it up properly:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.