Bug 885610 - valgrind does not report errors
Summary: valgrind does not report errors
Keywords:
Status: CLOSED NOTABUG
Alias: None
Product: Fedora
Classification: Fedora
Component: valgrind
Version: rawhide
Hardware: Unspecified
OS: Unspecified
unspecified
unspecified
Target Milestone: ---
Assignee: Jakub Jelinek
QA Contact: Fedora Extras Quality Assurance
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2012-12-10 08:36 UTC by Jan Synacek
Modified: 2012-12-10 09:56 UTC (History)
4 users (show)

Fixed In Version:
Clone Of:
Environment:
Last Closed: 2012-12-10 08:49:33 UTC
Type: Bug
Embargoed:


Attachments (Terms of Use)
Reproducer (347 bytes, text/plain)
2012-12-10 08:36 UTC, Jan Synacek
no flags Details

Description Jan Synacek 2012-12-10 08:36:49 UTC
Created attachment 660629 [details]
Reproducer

Description of problem:
When I'm strncpy-ing a string that is longer than allocated memory to the destination buffer, no valgrind errors are shown. However, if I declare a pointer that points to the destination, errors are shown as expected.

Version-Release number of selected component (if applicable):

valgrind-3.8.1-5.fc19.x86_64
gcc-4.7.2-9.fc19.x86_64
glibc-2.16.90-36.fc19.x86_64


How reproducible:
Always


Steps to Reproduce:
1. compile the attachment
2. run through valgrind
3.

  
Actual results:
No errors shown.


Expected results:
Errors shown like when the pointer assignment is uncommented.


Additional info:

Comment 1 Jakub Jelinek 2012-12-10 08:49:33 UTC
You are misunderstanding what valgrind is actually protecting.
For this testcase you need either -D_FORTIFY_SOURCE=2, then GCC will report it already at compile time:
warning: call to __builtin___strncpy_chk will always overflow destination buffer [enabled by default]
or Address Sanitizer (will be in GCC 4.8 and later).
valgrind doesn't instrument automatic variable boundaries on the stack in any way, the reason why it warns with the extra line uncommented isn't that it would detect access beyond end of the array, but that the bytes are there are never stored to and thus considered uninitialized.

Comment 2 Jan Synacek 2012-12-10 08:51:42 UTC
Hmm, didn't know that. Sorry for the noise.

Comment 3 Mark Wielaard 2012-12-10 09:32:47 UTC
(In reply to comment #2)
> Hmm, didn't know that. Sorry for the noise.

No worries, it is a common mistake, I made it myself a couple of times.
Just think of valgrind (memcheck) as a read-before-write checker. It will detect whenever you use some bits that were never written to before. It can also detect some cases of "bad memory" access, like when writing to never allocated memory, but not as accurately. Your example is one where valgrind cannot know for sure, because the stack is already allocated (it isn't "bad memory" as far as memcheck is concerned).

Comment 4 Jakub Jelinek 2012-12-10 09:56:16 UTC
It also instruments malloc, so for heap objects the detection of accesses before or after the allocated region are reported, or use-after-free too.


Note You need to log in before you can comment on or make changes to this bug.