From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from kwanyin.sergiodj.net (kwanyin.sergiodj.net [158.69.185.54]) by sourceware.org (Postfix) with ESMTPS id DD09F385B831 for ; Sun, 5 Apr 2020 11:56:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org DD09F385B831 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [binutils-gdb] Avoid stringop-truncation errors From: gdb-buildbot@sergiodj.net To: gdb-testers@sourceware.org Message-Id: Date: Sun, 05 Apr 2020 07:56:07 -0400 X-Spam-Status: No, score=-15.4 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, 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-testers@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-testers mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 05 Apr 2020 11:56:11 -0000 *** TEST RESULTS FOR COMMIT f67210ff1c4200ea668189d086c6b39145cd876f *** commit f67210ff1c4200ea668189d086c6b39145cd876f Author: Tom Tromey AuthorDate: Fri Mar 20 07:30:13 2020 -0600 Commit: Tom Tromey CommitDate: Fri Mar 20 08:31:17 2020 -0600 Avoid stringop-truncation errors I configured with -fsanitize=address and built gdb. linux-tdep.c and ada-tasks.c failed to build due to some stringop-truncation errors, e.g.: In function char* strncpy(char*, const char*, size_t), inlined from int linux_fill_prpsinfo(elf_internal_linux_prpsinfo*) at ../../binutils-gdb/gdb/linux-tdep.c:1742:11, inlined from char* linux_make_corefile_notes(gdbarch*, bfd*, int*) at ../../binutils-gdb/gdb/linux-tdep.c:1878:27: /usr/include/bits/string_fortified.h:106:34: error: char* __builtin_strncpy(char*, const char*, long unsigned int) specified bound 81 equals destination size [-Werror=stringop-truncation] This patch fixes the problem by using "sizeof - 1" in the call to strndup, as recommended in the GCC manual. This doesn't make a difference here because the next line, in all cases, sets the final element to '\0' anyway. gdb/ChangeLog 2020-03-20 Tom Tromey * ada-tasks.c (read_atcb): Use smaller length in strncpy call. * linux-tdep.c (linux_fill_prpsinfo): Use smaller length in strncpy call. diff --git a/gdb/ChangeLog b/gdb/ChangeLog index f1b007b1ba..583ec9c81e 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,9 @@ +2020-03-20 Tom Tromey + + * ada-tasks.c (read_atcb): Use smaller length in strncpy call. + * linux-tdep.c (linux_fill_prpsinfo): Use smaller length in + strncpy call. + 2020-03-20 Tom Tromey * symmisc.c (maintenance_print_one_line_table): Use ui_out. diff --git a/gdb/ada-tasks.c b/gdb/ada-tasks.c index 0a81c3c692..589d5e84e0 100644 --- a/gdb/ada-tasks.c +++ b/gdb/ada-tasks.c @@ -679,7 +679,8 @@ read_atcb (CORE_ADDR task_id, struct ada_task_info *task_info) task_name = p + 2; /* Copy the task name. */ - strncpy (task_info->name, task_name, sizeof (task_info->name)); + strncpy (task_info->name, task_name, + sizeof (task_info->name) - 1); task_info->name[sizeof (task_info->name) - 1] = 0; } else diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c index b6374ce399..e50946ce37 100644 --- a/gdb/linux-tdep.c +++ b/gdb/linux-tdep.c @@ -1729,7 +1729,7 @@ linux_fill_prpsinfo (struct elf_internal_linux_prpsinfo *p) /* Copying the program name. Only the basename matters. */ basename = lbasename (fname.get ()); - strncpy (p->pr_fname, basename, sizeof (p->pr_fname)); + strncpy (p->pr_fname, basename, sizeof (p->pr_fname) - 1); p->pr_fname[sizeof (p->pr_fname) - 1] = '\0'; infargs = get_inferior_args (); @@ -1739,7 +1739,7 @@ linux_fill_prpsinfo (struct elf_internal_linux_prpsinfo *p) if (infargs != NULL) psargs = psargs + " " + infargs; - strncpy (p->pr_psargs, psargs.c_str (), sizeof (p->pr_psargs)); + strncpy (p->pr_psargs, psargs.c_str (), sizeof (p->pr_psargs) - 1); p->pr_psargs[sizeof (p->pr_psargs) - 1] = '\0'; xsnprintf (filename, sizeof (filename), "/proc/%d/stat", (int) pid);