From patchwork Sat Dec 4 21:15:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Mark Wielaard X-Patchwork-Id: 48503 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A944C3858432 for ; Sat, 4 Dec 2021 21:15:21 +0000 (GMT) X-Original-To: elfutils-devel@sourceware.org Delivered-To: elfutils-devel@sourceware.org Received: from gnu.wildebeest.org (gnu.wildebeest.org [45.83.234.184]) by sourceware.org (Postfix) with ESMTPS id BBC8E3858C60 for ; Sat, 4 Dec 2021 21:15:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org BBC8E3858C60 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=klomp.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=klomp.org Received: from reform (deer0x0b.wildebeest.org [172.31.17.141]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by gnu.wildebeest.org (Postfix) with ESMTPSA id 3D6873000F0A; Sat, 4 Dec 2021 22:15:14 +0100 (CET) Received: by reform (Postfix, from userid 1000) id E6D202E8219C; Sat, 4 Dec 2021 22:15:13 +0100 (CET) From: Mark Wielaard To: elfutils-devel@sourceware.org Subject: [PATCH] readelf: Workaround stringop-truncation error Date: Sat, 4 Dec 2021 22:15:04 +0100 Message-Id: <20211204211504.276502-1-mark@klomp.org> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 X-Spam-Status: No, score=-10.0 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.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: elfutils-devel@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Elfutils-devel mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , Cc: Mark Wielaard Errors-To: elfutils-devel-bounces+patchwork=sourceware.org@sourceware.org Sender: "Elfutils-devel" In function ‘strncpy’, inlined from ‘print_ehdr’ at readelf.c:1175:4: error: ‘__builtin_strncpy’ specified bound 512 equals destination size [-Werror=stringop-truncation] strncpy doesn't terminate the copied string if there is not enough room. We compensate later by explicitly adding a zero terminator at buf[sizeof (buf) - 1]. Normally gcc does see this, but with -fsanitize=address there is too much (checking) code in between. But it is actually better to not let strncpy do too much work, so substract one from the size. Signed-off-by: Mark Wielaard --- src/ChangeLog | 4 ++++ src/readelf.c | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/ChangeLog b/src/ChangeLog index 05b2522d..263e9faa 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,7 @@ +2021-12-04 Mark Wielaard + + * readelf.c (print_ehdr): Pass sizeof (buf) - 1 to strncpy. + 2021-10-20 John M Mellor-Crummey * readelf.c (print_debug_line_section): Try to read diff --git a/src/readelf.c b/src/readelf.c index c10038e3..93fb5989 100644 --- a/src/readelf.c +++ b/src/readelf.c @@ -1172,7 +1172,7 @@ print_ehdr (Ebl *ebl, GElf_Ehdr *ehdr) (uint32_t) shdr->sh_link); else { - strncpy (buf, _(" ([0] not available)"), sizeof (buf)); + strncpy (buf, _(" ([0] not available)"), sizeof (buf) - 1); buf[sizeof (buf) - 1] = '\0'; }