From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 103496 invoked by alias); 16 Aug 2017 20:55:29 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 103479 invoked by uid 89); 16 Aug 2017 20:55:28 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-10.9 required=5.0 tests=BAYES_00,GIT_PATCH_2,GIT_PATCH_3,KAM_LAZY_DOMAIN_SECURITY,RP_MATCHES_RCVD,SPF_HELO_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 16 Aug 2017 20:55:27 +0000 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C8BAD788A8; Wed, 16 Aug 2017 20:55:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com C8BAD788A8 Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=dmalcolm@redhat.com Received: from ovpn-117-90.phx2.redhat.com (ovpn-117-90.phx2.redhat.com [10.3.117.90]) by smtp.corp.redhat.com (Postfix) with ESMTP id 720556016F; Wed, 16 Aug 2017 20:55:25 +0000 (UTC) Message-ID: <1502916924.3741.21.camel@redhat.com> Subject: Re: [PATCH] use strnlen in pretty printer for "%.*s" (PR 81859) From: David Malcolm To: Martin Sebor , Gcc Patch List Date: Wed, 16 Aug 2017 22:38:00 -0000 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Mime-Version: 1.0 Content-Transfer-Encoding: 7bit X-IsSubscribed: yes X-SW-Source: 2017-08/txt/msg01024.txt.bz2 On Wed, 2017-08-16 at 10:12 -0600, Martin Sebor wrote: > PR c/81859 - [8 Regression] valgrind error from > warn_about_normalization > > gcc/ChangeLog: > > PR c/81859 > * pretty-print.c (pp_format): Use strnlen in %.*s to avoid > reading > past the end of an array. > (test_pp_format): Add test cases. > > Index: gcc/pretty-print.c > =================================================================== > --- gcc/pretty-print.c (revision 251100) > +++ gcc/pretty-print.c (working copy) > @@ -668,15 +668,11 @@ pp_format (pretty_printer *pp, text_info *text) > > s = va_arg (*text->args_ptr, const char *); > > - /* Negative precision is treated as if it were > omitted. */ > - if (n < 0) > - n = INT_MAX; > + /* Append the lesser of precision and strlen (s) > characters > + from the array (which need not be a nul-terminated > string). > + Negative precision is treated as if it were > omitted. */ > + size_t len = n < 0 ? strlen (s) : strnlen (s, n); > > - /* Append the lesser of precision and strlen (s) > characters. */ > - size_t len = strlen (s); > - if ((unsigned) n < len) > - len = n; > - > pp_append_text (pp, s, s + len); > } > break; > @@ -1438,6 +1434,13 @@ test_pp_format () > ASSERT_PP_FORMAT_2 ("A 12345678", "%c %x", 'A', 0x12345678); > ASSERT_PP_FORMAT_2 ("hello world 12345678", "%s %x", "hello > world", > 0x12345678); > + > + /* Not nul-terminated. */ > + char arr[5] = { '1', '2', '3', '4', '5' }; > + ASSERT_PP_FORMAT_2 ("123", "%.*s", 3, arr); > + ASSERT_PP_FORMAT_2 ("1234", "%.*s", -1, "1234"); > + ASSERT_PP_FORMAT_2 ("12345", "%.*s", 7, "12345"); > + The other examples in this selftest append a trailing argument with a known bit pattern (0x12345678), to ensure that we're consuming arguments correctly. Please can you do the same for these tests. Thanks Dave