From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7876) id 527A8395A065; Tue, 31 May 2022 14:46:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 527A8395A065 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Nils-Christian Kempke To: gdb-cvs@sourceware.org Subject: [binutils-gdb] gdb, testsuite, fortran: fix double free in mixed-lang-stack.exp X-Act-Checkin: binutils-gdb X-Git-Author: Nils-Christian Kempke X-Git-Refname: refs/heads/master X-Git-Oldrev: 6b7b705d7c21b0d0dd9eaf5273a711e20e238ec3 X-Git-Newrev: a60ead5ded5f9fc548018637d4810cb5f313ad8c Message-Id: <20220531144619.527A8395A065@sourceware.org> Date: Tue, 31 May 2022 14:46:19 +0000 (GMT) X-BeenThere: gdb-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 31 May 2022 14:46:19 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3Da60ead5ded5f= 9fc548018637d4810cb5f313ad8c commit a60ead5ded5f9fc548018637d4810cb5f313ad8c Author: Nils-Christian Kempke Date: Tue May 31 16:43:45 2022 +0200 gdb, testsuite, fortran: fix double free in mixed-lang-stack.exp =20 While testing mixed-lang-stack I realized that valgrind actually complained about a double free in the test. =20 All done =3D=3D2503051=3D=3D =3D=3D2503051=3D=3D HEAP SUMMARY: =3D=3D2503051=3D=3D in use at exit: 0 bytes in 0 blocks =3D=3D2503051=3D=3D total heap usage: 26 allocs, 27 frees, 87,343 b= ytes allocated =3D=3D2503051=3D=3D =3D=3D2503051=3D=3D All heap blocks were freed -- no leaks are possib= le =3D=3D2503051=3D=3D =3D=3D2503051=3D=3D For lists of detected and suppressed errors, reru= n with: -s =3D=3D2503051=3D=3D ERROR SUMMARY: 1 errors from 1 contexts (suppress= ed: 0 from 0) =20 Reason for this is that in mixed-lang-stack.cpp in mixed_func_1f an object "derived_type obj" goes on the stack which is then passed-by-val= ue (so copied) to mixed_func_1g. The default copy-ctor will be called but, since derived_type contains a heap allocated string and the copy constructor is not implemented it will only be able to shallow copy the object. Right after each of the functions the object gets freed - on t= he other hand the d'tor of derived_type actually is implemented and calls free on the heap allocated string which leads to a double free. Instead of obeying the rule of 3/5 I just got rid of all that since it does not serve the test. The string is now just a const char* =3D ".." object member. Diff: --- gdb/testsuite/gdb.fortran/mixed-lang-stack.cpp | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/gdb/testsuite/gdb.fortran/mixed-lang-stack.cpp b/gdb/testsuite= /gdb.fortran/mixed-lang-stack.cpp index 39ff6c201f7..b5ae7dac0a8 100644 --- a/gdb/testsuite/gdb.fortran/mixed-lang-stack.cpp +++ b/gdb/testsuite/gdb.fortran/mixed-lang-stack.cpp @@ -26,17 +26,7 @@ class base_one class base_two { public: - base_two () - { - string =3D strdup ("Something in C++"); - } - - ~base_two () - { - free (string); - } - - char *string =3D nullptr; + const char *string =3D "Something in C++"; float val =3D 3.5; };