Please find below a stripped down testcase and a session log from valgrind.

The code reads input from stdin, compares what was read against a couple of
choices (exactly one in this case) and repeats reading until a single 'x' was
entered. The leak is related to the length of the input buffer within the main
program, hitting [enter] N times before giving an 'x' results in N leaked
blocks of strlen(input). Also involved is the SELECT CASE: if it is replaced by
an equivalent IF-statement, no memory is leaked.

Reproducible with gfortran-4.1.1 and gfortran-4.2 20060914 (experimental).

$> cat test.f90
MODULE stringutils
CHARACTER(*), PRIVATE, PARAMETER :: lowercase = 'abcdefghijklmnopqrstuvwxyz' 
CHARACTER(*), PRIVATE, PARAMETER :: uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 
CONTAINS
  FUNCTION tolower(instr) RESULT (outstr)
    CHARACTER(len=*), INTENT(in) :: instr
    CHARACTER(len=len(instr))    :: outstr
    INTEGER :: i, k
    outstr = instr
    DO i = 1, len_trim(outstr)
      k = index(uppercase, outstr(i:i))
      if (k /= 0) outstr(i:i) = lowercase(k:k)
    END DO
  END FUNCTION
END MODULE

MODULE configuration
CONTAINS
  LOGICAL FUNCTION validate_value(input, value)
    USE stringutils
    CHARACTER(len=*), INTENT(in) :: input
    INTEGER, INTENT(out)         :: value
    validate_value = .TRUE.
    SELECT CASE (tolower(input))
      CASE ("x");  value = 1
      CASE DEFAULT
        validate_value = .FALSE.
    END SELECT
  END FUNCTION
END MODULE

PROGRAM test
  USE configuration
  USE stringutils
  INTEGER             :: value
  CHARACTER(len=256)  :: input
  DO 
    WRITE (*, FMT="(A)", ADVANCE = "NO") "$> "
    READ (*, FMT="(A)") input
    IF (validate_value(input, value)) EXIT
  END DO
END PROGRAM

$> gfortran-4.2 -g -Wall test.f90
$> valgrind --tool=memcheck --leak-check=full ./a.out
==9829== Memcheck, a memory error detector.
==9829== Copyright (C) 2002-2006, and GNU GPL'd, by Julian Seward et al.
==9829== Using LibVEX rev 1606, a library for dynamic binary translation.
==9829== Copyright (C) 2004-2006, and GNU GPL'd, by OpenWorks LLP.
==9829== Using valgrind-3.2.0, a dynamic binary instrumentation framework.
==9829== Copyright (C) 2000-2006, and GNU GPL'd, by Julian Seward et al.
==9829== For more details, rerun with: -v
==9829==
%>
%>
%>
%>
%>
%> x
==9829==
==9829== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 3 from 1)
==9829== malloc/free: in use at exit: 1,536 bytes in 6 blocks.
==9829== malloc/free: 24 allocs, 18 frees, 55,080 bytes allocated.
==9829== For counts of detected errors, rerun with: -v
==9829== searching for pointers to 6 not-freed blocks.
==9829== checked 78,156 bytes.
==9829==
==9829== 1,536 bytes in 6 blocks are definitely lost in loss record 1 of 1
==9829==    at 0x4021396: malloc (vg_replace_malloc.c:149)
==9829==    by 0x40304EC: _gfortrani_get_mem (memory.c:53)
==9829==    by 0x80488FA: __configuration__validate_value (dump.f90:29)
==9829==    by 0x8048A9A: MAIN__ (dump.f90:47)
==9829==    by 0x8048AD6: main (fmain.c:18)
==9829==
==9829== LEAK SUMMARY:
==9829==    definitely lost: 1,536 bytes in 6 blocks.
==9829==      possibly lost: 0 bytes in 0 blocks.
==9829==    still reachable: 0 bytes in 0 blocks.
==9829==         suppressed: 0 bytes in 0 blocks.
==9829== Reachable blocks (those to which a pointer was found) are not shown.
==9829== To see them, rerun with: --show-reachable=yes


-- 
           Summary: memory leak in libgfortran
           Product: gcc
           Version: 4.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libfortran
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: franke dot daniel at gmail dot com
  GCC host triplet: i686-pc-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=29101

Reply via email to