Bug 424656

Summary: Uninitialised value was created by a heap allocation
Product: [Developer tools] valgrind Reporter: Rick Stanley <rstanley>
Component: memcheckAssignee: Julian Seward <jseward>
Status: RESOLVED NOT A BUG    
Severity: normal CC: philippe.waroquiers
Priority: NOR    
Version First Reported In: unspecified   
Target Milestone: ---   
Platform: Debian testing   
OS: Linux   
Latest Commit: Version Fixed/Implemented In:
Sentry Crash Report:

Description Rick Stanley 2020-07-25 14:57:51 UTC
SUMMARY
Possible incorrect errors using the source below.  Occurs with gcc-8, gcc-9, gcc-10 on Debian Testing, "valgrind-3.16.1"

STEPS TO REPRODUCE
1. Source:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define DIM 32

int main(void)
{
   char *p = NULL;

   p = malloc(DIM);

   if(p == NULL)
   {
      printf("Allocation error.\n");
      exit(1);
   }

   strcpy(p, "This is a test.");

   for(int x = 0 ; x < DIM; ++x)
   {
      printf("%02x ",  p[x]);
   }
   putchar('\n');

   free(p);

   return 0;
}

2.  "gcc -std=c18 -Wall -Wextra -Wpedantic -g -I . -o ptest ptest.c"

3. "valgrind -s --track-origins=yes ./ptest"

OBSERVED RESULT

==11539== Use of uninitialised value of size 8
==11539==    at 0x48B4E5A: _itoa_word (_itoa.c:180)
==11539==    by 0x48CE753: __vfprintf_internal (vfprintf-internal.c:1687)
==11539==    by 0x48BAD6A: printf (printf.c:33)
==11539==    by 0x10920D: main (ptest.c:33)
==11539==  Uninitialised value was created by a heap allocation
==11539==    at 0x483877F: malloc (vg_replace_malloc.c:307)
==11539==    by 0x10919E: main (ptest.c:21)
==11539== 
...


EXPECTED RESULT


SOFTWARE/OS VERSIONS
Windows: 
macOS: 
Linux/KDE Plasma: 
(available in About System)
KDE Plasma Version: 
KDE Frameworks Version: 
Qt Version: 

ADDITIONAL INFORMATION
Comment 1 Philippe Waroquiers 2020-07-25 15:11:05 UTC
That looks like a real bug that valgrind detects.

The malloc allocates 32 bytes, the strcpy initialises 16 bytes
but the printf loop prints the 32 bytes, so effectively prints data nopt initialised.
Comment 2 Rick Stanley 2020-07-25 17:20:10 UTC
Is there a way to suppress a valgrind warning in the code for specific
lines and/or functions by setting/un-setting a variable or using
comment lines?

If not then there should be.

In the real code I am writing, I want to simply display in hex the
values of each byte of an allocation irregardless of all the bytes
being initialized or not.

Thank you!


On Sat, 2020-07-25 at 15:11 +0000, Philippe Waroquiers wrote:
> https://bugs.kde.org/show_bug.cgi?id=424656
> 
> Philippe Waroquiers <philippe.waroquiers@skynet.be> changed:
> 
>            What    |Removed                     |Added
> -------------------------------------------------------------------
> ---------
>                  CC|                            
> |philippe.waroquiers@skynet.
>                    |                            |be
> 
> --- Comment #1 from Philippe Waroquiers <
> philippe.waroquiers@skynet.be> ---
> That looks like a real bug that valgrind detects.
> 
> The malloc allocates 32 bytes, the strcpy initialises 16 bytes
> but the printf loop prints the 32 bytes, so effectively prints data
> nopt
> initialised.
>
Comment 3 Philippe Waroquiers 2020-07-25 18:27:55 UTC
Yes, you can suppress errors.

See user manual for more info:
https://www.valgrind.org/docs/manual/manual-core.html#manual-core.suppress

More generally it is hiighly recommended to read or at least scan the user manual.