From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2205) id C69B438F8606; Mon, 23 May 2022 12:50:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C69B438F8606 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Tom de Vries To: gdb-cvs@sourceware.org Subject: [binutils-gdb] [gdbsupport] Fix UB in print-utils.cc:int_string X-Act-Checkin: binutils-gdb X-Git-Author: Tom de Vries X-Git-Refname: refs/heads/master X-Git-Oldrev: 5a3cf18c2ed9593f194ea22f50ea5651532f6cfc X-Git-Newrev: 735dfe028c93f20026339f3eeeadacac82fe7963 Message-Id: <20220523125017.C69B438F8606@sourceware.org> Date: Mon, 23 May 2022 12:50:17 +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: Mon, 23 May 2022 12:50:17 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D735dfe028c93= f20026339f3eeeadacac82fe7963 commit 735dfe028c93f20026339f3eeeadacac82fe7963 Author: Tom de Vries Date: Mon May 23 14:50:02 2022 +0200 [gdbsupport] Fix UB in print-utils.cc:int_string =20 When building gdb with -fsanitize=3Dundefined, I run into: ... (gdb) PASS: gdb.ada/access_to_packed_array.exp: set logging enabled on maint print symbols^M print-utils.cc:281:29:runtime error: negation of -9223372036854775808 c= annot \ be represented in type 'long int'; cast to an unsigned type to negate= this \ value to itself (gdb) FAIL: gdb.ada/access_to_packed_array.exp: maint print symbols ... =20 By running in a debug session, we find that this happens during printin= g of: ... typedef system.storage_elements.storage_offset: \ range -9223372036854775808 .. 9223372036854775807; ... Possibly, an ada test-case could be created that exercises this in isol= ation. =20 The problem is here in int_string, where we negate a val with type LONG= EST: ... return decimal2str ("-", -val, width); ... =20 Fix this by, as recommend, using "-(ULONGEST)val" instead. =20 Tested on x86_64-linux. Diff: --- gdbsupport/print-utils.cc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/gdbsupport/print-utils.cc b/gdbsupport/print-utils.cc index 73ff1afda30..7bbb6deea74 100644 --- a/gdbsupport/print-utils.cc +++ b/gdbsupport/print-utils.cc @@ -278,7 +278,11 @@ int_string (LONGEST val, int radix, int is_signed, int= width, case 10: { if (is_signed && val < 0) - return decimal2str ("-", -val, width); + /* Cast to unsigned before negating, to prevent runtime error: + negation of -9223372036854775808 cannot be represented in type + 'long int'; cast to an unsigned type to negate this value to + itself. */ + return decimal2str ("-", -(ULONGEST)val, width); else return decimal2str ("", val, width); }