https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67430
Bug ID: 67430 Summary: reallocate lhs with overloaded assignment operators causes memory error and wrong size Product: gcc Version: 6.0 Status: UNCONFIRMED Severity: major Priority: P3 Component: fortran Assignee: unassigned at gcc dot gnu.org Reporter: alipasha.celeris at gmail dot com Target Milestone: --- Created attachment 36284 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=36284&action=edit the test file with line numbers matching the Valgrind output. The following code produces a memory error in Valgrind (an invalid read) and does not reallocate the LHS to the correct size. MODULE A_TEST_M TYPE :: A_TYPE INTEGER :: INT ! CONTAINS ! GENERIC :: ASSIGNMENT (=) => ASGN_A PROCEDURE, PRIVATE :: ASGN_A END TYPE CONTAINS ELEMENTAL SUBROUTINE ASGN_A (A, B) CLASS (A_TYPE), INTENT (INOUT) :: A CLASS (A_TYPE), INTENT (IN) :: B ! A%INT = 45 END SUBROUTINE END MODULE A_TEST_M PROGRAM ASGN_REALLOC_TEST USE A_TEST_M TYPE (A_TYPE), ALLOCATABLE :: A(:) ! ALLOCATE (A(4)) A(1:2)%INT = 7 A(3:4)%INT = 13 ! A = A(1:2) ! PRINT *, 'SIZE(A)', SIZE(A), 'A:', A END PROGRAM The results look like this (valgrind output): ==30070== Memcheck, a memory error detector ==30070== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al. ==30070== Using Valgrind-3.11.0.SVN and LibVEX; rerun with -h for copyright info ==30070== Command: ./a.out ==30070== ==30070== Invalid read of size 8 ==30070== at 0x402F148: _intel_fast_memcpy (vg_replace_strmem.c:946) ==30070== by 0x400CA1: MAIN__ (asgn_realloc_test.f:29) ==30070== by 0x400DF3: main (asgn_realloc_test.f:22) ==30070== Address 0x50f37c8 is 0 bytes after a block of size 8 alloc'd ==30070== at 0x402BB20: malloc (vg_replace_malloc.c:299) ==30070== by 0x400C13: MAIN__ (asgn_realloc_test.f:29) ==30070== by 0x400DF3: main (asgn_realloc_test.f:22) ==30070== SIZE(A) 4 A: 45 45 0 0 "A" should have size of 2 after assignment (being reallocated). It seems that the overloaded operator is being used (it is rigged to assign 45). The intrinsic assignment (when = is not overloaded) works fine.