From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from rock.gnat.com (rock.gnat.com [205.232.38.15]) by sourceware.org (Postfix) with ESMTP id C14A2395041D for ; Mon, 3 May 2021 19:32:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org C14A2395041D Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tromey@adacore.com Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id A247D11758C; Mon, 3 May 2021 15:32:08 -0400 (EDT) X-Virus-Scanned: Debian amavisd-new at gnat.com Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id X-IpIFTFm56G; Mon, 3 May 2021 15:32:08 -0400 (EDT) Received: from murgatroyd.Home (97-122-70-176.hlrn.qwest.net [97.122.70.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by rock.gnat.com (Postfix) with ESMTPSA id 5E957117163; Mon, 3 May 2021 15:32:08 -0400 (EDT) From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH 3/3] Fix ubsan build Date: Mon, 3 May 2021 13:32:06 -0600 Message-Id: <20210503193206.4008066-4-tromey@adacore.com> X-Mailer: git-send-email 2.26.3 In-Reply-To: <20210503193206.4008066-1-tromey@adacore.com> References: <20210503193206.4008066-1-tromey@adacore.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 03 May 2021 19:32:10 -0000 I tried a build using the undefined behavior sanitizer, and gcc gave this error: In file included from /usr/include/string.h:495, from ../gnulib/import/string.h:41, from ../../binutils-gdb/gdb/../gdbsupport/common-defs.h:95, from ../../binutils-gdb/gdb/nat/linux-osdata.c:20: In function ‘char* strncpy(char*, const char*, size_t)’, inlined from ‘void time_from_time_t(char*, int, TIME_T)’ at ../../binutils-gdb/gdb/nat/linux-osdata.c:923:15, inlined from ‘void time_from_time_t(char*, int, TIME_T)’ at ../../binutils-gdb/gdb/nat/linux-osdata.c:911:1, inlined from ‘void linux_xfer_osdata_sem(buffer*)’ at ../../binutils-gdb/gdb/nat/linux-osdata.c:1082:22: /usr/include/bits/string_fortified.h:106:34: error: ‘char* __builtin_strncpy(char*, const char*, long unsigned int)’ specified bound 32 equals destination size [-Werror=stringop-truncation] This patch fixes the problem by subtracting one from the length parameter to strncpy. I changed a couple of other similar functions -- gcc does not warn about these, but I didn't see any substantial difference between the different cases, and I think these are just latent warnings, to be triggered in the future by a change to inlining heuristics. gdb/ChangeLog 2021-05-03 Tom Tromey * nat/linux-osdata.c (user_from_uid, time_from_time_t) (group_from_gid): Subtract one from strncpy length. --- gdb/ChangeLog | 5 +++++ gdb/nat/linux-osdata.c | 6 +++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/gdb/nat/linux-osdata.c b/gdb/nat/linux-osdata.c index 7034dd82376..12f66d3c981 100644 --- a/gdb/nat/linux-osdata.c +++ b/gdb/nat/linux-osdata.c @@ -212,7 +212,7 @@ user_from_uid (char *user, int maxlen, uid_t uid) if (pwentry) { - strncpy (user, pwentry->pw_name, maxlen); + strncpy (user, pwentry->pw_name, maxlen - 1); /* Ensure that the user name is null-terminated. */ user[maxlen - 1] = '\0'; } @@ -920,7 +920,7 @@ time_from_time_t (char *time, int maxlen, TIME_T seconds) characters long. */ char buf[30]; const char *time_str = ctime_r (&t, buf); - strncpy (time, time_str, maxlen); + strncpy (time, time_str, maxlen - 1); time[maxlen - 1] = '\0'; } } @@ -935,7 +935,7 @@ group_from_gid (char *group, int maxlen, gid_t gid) if (grentry) { - strncpy (group, grentry->gr_name, maxlen); + strncpy (group, grentry->gr_name, maxlen - 1); /* Ensure that the group name is null-terminated. */ group[maxlen - 1] = '\0'; } -- 2.26.3