https://bugs.kde.org/show_bug.cgi?id=487296

            Bug ID: 487296
           Summary: --track-fds=yes and --track-fds=all report erroneous
                    information when fds 0, 1, or 2 are used as non-std
    Classification: Developer tools
           Product: valgrind
           Version: 3.24 GIT
          Platform: Other
                OS: Other
            Status: REPORTED
          Severity: minor
          Priority: NOR
         Component: memcheck
          Assignee: jsew...@acm.org
          Reporter: imb2...@columbia.edu
  Target Milestone: ---

Created attachment 169667
  --> https://bugs.kde.org/attachment.cgi?id=169667&action=edit
patch to accurately report leaks for fds 0, 1, 2

SUMMARY
If a program closes any std fd then reopens it as non-std, Memcheck's
--track-fds=yes report is inaccurate. If fds 0, 1, or 2 are in use,
--track-fds=yes and --track-fds=all treat them as std (regardless of whether
they are or not), leading to erroneous output. Leak messages are consequently
omitted with --track-fds=yes.

STEPS TO REPRODUCE
Run a program that closes one of the std fds, and reopens another file in its
place without closing it:

// test.c
#include <assert.h>
#include <stdio.h>
#include <unistd.h>

int main(void) {
        FILE *fp;
        assert(fclose(stdout) == 0);

        fp = fopen("newfile.txt", "w");
        assert(fileno(fp) == STDOUT_FILENO);

        return 0;
}


OBSERVED RESULT

$ gcc -g -Wall test.c -o test && valgrind --leak-check=yes --track-fds=yes
./test
==422217== Memcheck, a memory error detector
==422217== Copyright (C) 2002-2024, and GNU GPL'd, by Julian Seward et al.
==422217== Using Valgrind-3.24.0.GIT and LibVEX; rerun with -h for copyright
info
==422217== Command: ./test
==422217== 
==422217== 
==422217== FILE DESCRIPTORS: 3 open (3 std) at exit.
==422217== 
==422217== HEAP SUMMARY:
==422217==     in use at exit: 472 bytes in 1 blocks
==422217==   total heap usage: 1 allocs, 0 frees, 472 bytes allocated
==422217== 
==422217== LEAK SUMMARY:
==422217==    definitely lost: 0 bytes in 0 blocks
==422217==    indirectly lost: 0 bytes in 0 blocks
==422217==      possibly lost: 0 bytes in 0 blocks
==422217==    still reachable: 472 bytes in 1 blocks
==422217==         suppressed: 0 bytes in 0 blocks
==422217== Reachable blocks (those to which a pointer was found) are not shown.
==422217== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==422217== 
==422217== For lists of detected and suppressed errors, rerun with: -s
==422217== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Note that Memcheck reports 3 std fds open at exit, does not report a leak
message, and does not report any errors.


EXPECTED RESULT

Memcheck should report 2 std fds open at exit, a leak message should be
outputted for the newly opened fd, and the error summary should list 1 error.

-- 
You are receiving this mail because:
You are watching all bug changes.

Reply via email to