From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id DDDCF383942B for ; Fri, 1 Apr 2022 20:15:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org DDDCF383942B Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-494-Z778DYxEN465NEzbKOJy5g-1; Fri, 01 Apr 2022 16:15:37 -0400 X-MC-Unique: Z778DYxEN465NEzbKOJy5g-1 Received: by mail-qt1-f197.google.com with SMTP id f3-20020ac84983000000b002e22396acfbso2705457qtq.18 for ; Fri, 01 Apr 2022 13:15:37 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=lXjaXpDRssPQW0iqW44+pIFnwYigIfM7OwfUBjV5rA0=; b=ASPSA11P5+1J9wJhwh92a0tCJxTPzOLGg9yb4Wuy6F0x/6Y1PllzyRIdMAoR674c5k Ez/uhp+RqgwjtSx/g5LCtzdCTVHOjz9IH8mBJMyaLRf2WAXc7mTwsUNUZzjTBQP5AQOx bldDUxwaT0QFcAZJKePPY6k8vNMp9odAcvhXMcMqIrFSeLe8FPWZNPYMvmvXF1Eq2az5 B65CL2oyHnyqtPnoYn4vE6VbqZ0unUKLDuHPVE8kf+hXB3sin/iySzkFoRglwZ/wAtnS tFSqwj6cA9DXDjCiabsR6pPIepiV+RyZrpzkHcCEMqrhIOYk5EJo8G56RJNw3aMTLEp8 f1Dw== X-Gm-Message-State: AOAM531ze6dclpW8/AGEryxyHXQSMVx3ObrbxaHfIFE04MVFjY2vXbQ3 CDO/j+kMlhmmlsIbynxV3u9txXkI1BXfJ3DTA76Skl+GsXRr9/oZ4wtGk9UzyZk4tgt0y8LHxj0 wZ+WqsjgN1TbUiS40QQ== X-Received: by 2002:a05:6214:2a8d:b0:443:7f75:2aaf with SMTP id jr13-20020a0562142a8d00b004437f752aafmr9508409qvb.19.1648844136917; Fri, 01 Apr 2022 13:15:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwX4wogc9m85atGfK4SfZHD6QpciSudAX8dlVgsd7eNfEEsGXM7g4driVFc1rZij8ip1EHv1g== X-Received: by 2002:a05:6214:2a8d:b0:443:7f75:2aaf with SMTP id jr13-20020a0562142a8d00b004437f752aafmr9508389qvb.19.1648844136597; Fri, 01 Apr 2022 13:15:36 -0700 (PDT) Received: from redhat.com ([2601:184:4780:4310::5f2c]) by smtp.gmail.com with ESMTPSA id r64-20020a37a843000000b0067b0cf40b18sm1861987qke.69.2022.04.01.13.15.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Apr 2022 13:15:35 -0700 (PDT) Date: Fri, 1 Apr 2022 16:15:34 -0400 From: Marek Polacek To: Frolov Daniil Cc: gcc-patches@gcc.gnu.org Subject: Re: -Wformat-overflow handling for %b and %B directives in C2X standard Message-ID: References: MIME-Version: 1.0 In-Reply-To: User-Agent: Mutt/2.1.5 (2021-12-30) X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-13.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 01 Apr 2022 20:15:40 -0000 On Sat, Apr 02, 2022 at 12:19:47AM +0500, Frolov Daniil via Gcc-patches wrote: > Hello, I've noticed that -Wformat-overflow doesn't handle %b and %B > directives in the sprintf function. I've added a relevant issue in bugzilla > (bug #105129). > I attach a patch with a possible solution to the letter. Thanks for the patch. Support for C2X %b, %B formats is relatively new (Oct 2021) so it looks like gimple-ssa-sprintf.cc hasn't caught up. This is not a regression, so should probably wait till GCC 13. Anyway... > From 2051344e9500651f6e94c44cbc7820715382b957 Mon Sep 17 00:00:00 2001 > From: Frolov Daniil > Date: Fri, 1 Apr 2022 00:47:03 +0500 > Subject: [PATCH] Support %b, %B for -Wformat-overflow (sprintf, snprintf) > > testsuite: add tests to check -Wformat-overflow on %b. > Wformat-overflow1.c is compiled using -std=c2x so warning has to > be throwed > > Wformat-overflow2.c doesn't throw warnings cause c2x std isn't > used > > gcc/ChangeLog: > > * gimple-ssa-sprintf.cc > (check_std_c2x): New function > (fmtresult::type_max_digits): add base == 2 handling > (tree_digits): add handle for base == 2 > (format_integer): now handle %b and %B using base = 2 > (parse_directive): add cases to handle %b and %B directives > (compute_format_length): add handling for base = 2 The descriptions should start with a capital letter and end with a period, like "Handle base == 2." > gcc/testsuite/ChangeLog: > > * gcc.dg/Wformat-overflow1.c: New test. (using -std=c2x) > * gcc.dg/Wformat-overflow2.c: New test. (-std=c11 no warning) You can just say "New test." > --- > gcc/gimple-ssa-sprintf.cc | 42 ++++++++++++++++++++---- > gcc/testsuite/gcc.dg/Wformat-overflow1.c | 28 ++++++++++++++++ > gcc/testsuite/gcc.dg/Wformat-overflow2.c | 16 +++++++++ > 3 files changed, 79 insertions(+), 7 deletions(-) > create mode 100644 gcc/testsuite/gcc.dg/Wformat-overflow1.c > create mode 100644 gcc/testsuite/gcc.dg/Wformat-overflow2.c > > diff --git a/gcc/gimple-ssa-sprintf.cc b/gcc/gimple-ssa-sprintf.cc > index c93f12f90b5..7f68c2b6e51 100644 > --- a/gcc/gimple-ssa-sprintf.cc > +++ b/gcc/gimple-ssa-sprintf.cc > @@ -107,6 +107,15 @@ namespace { > > static int warn_level; > > +/* b_overflow_flag depends on the current standart when using gcc */ "standard" /* Comments should be formatted like this. */ > +static bool b_overflow_flag; > + > +/* check is current standart version equals C2X*/ > +static bool check_std_c2x () > +{ > + return !strcmp (lang_hooks.name, "GNU C2X"); > +} Is this really needed? ISTM that this new checking shouldn't depend on -std=c2x. If not using C2X, you only get a warning if -Wpedantic. So I think you should remove b_overflow_flag. > /* The minimum, maximum, likely, and unlikely maximum number of bytes > of output either a formatting function or an individual directive > can result in. */ > @@ -535,6 +544,8 @@ fmtresult::type_max_digits (tree type, int base) > unsigned prec = TYPE_PRECISION (type); > switch (base) > { > + case 2: > + return prec; > case 8: > return (prec + 2) / 3; > case 10: > @@ -857,11 +868,11 @@ tree_digits (tree x, int base, HOST_WIDE_INT prec, bool plus, bool prefix) > > /* Adjust a non-zero value for the base prefix, either hexadecimal, > or, unless precision has resulted in a leading zero, also octal. */ > - if (prefix && absval && (base == 16 || prec <= ndigs)) > + if (prefix && absval && (base == 2 || base == 16 || prec <= ndigs)) > { > if (base == 8) > res += 1; > - else if (base == 16) > + else if (base == 16 || base == 2) /*0x...(0X...) and 0b...(0B...)*/ > res += 2; > } > > @@ -1229,6 +1240,10 @@ format_integer (const directive &dir, tree arg, pointer_query &ptr_qry) > case 'u': > base = 10; > break; > + case 'b': > + case 'B': > + base = 2; > + break; > case 'o': > base = 8; > break; > @@ -1351,10 +1366,10 @@ format_integer (const directive &dir, tree arg, pointer_query &ptr_qry) > > /* Bump up the counters if WIDTH is greater than LEN. */ > res.adjust_for_width_or_precision (dir.width, dirtype, base, > - (sign | maybebase) + (base == 16)); > + (sign | maybebase) + (base == 2 || base == 16)); > /* Bump up the counters again if PRECision is greater still. */ > res.adjust_for_width_or_precision (dir.prec, dirtype, base, > - (sign | maybebase) + (base == 16)); > + (sign | maybebase) + (base == 2 || base == 16)); > > return res; > } > @@ -1503,7 +1518,7 @@ format_integer (const directive &dir, tree arg, pointer_query &ptr_qry) > if (res.range.min == 1) > res.range.likely += base == 8 ? 1 : 2; > else if (res.range.min == 2 > - && base == 16 > + && (base == 16 || base == 2) > && (dir.width[0] == 2 || dir.prec[0] == 2)) > ++res.range.likely; > } > @@ -1511,9 +1526,9 @@ format_integer (const directive &dir, tree arg, pointer_query &ptr_qry) > > res.range.unlikely = res.range.max; > res.adjust_for_width_or_precision (dir.width, dirtype, base, > - (sign | maybebase) + (base == 16)); > + (sign | maybebase) + (base == 2 || base == 16)); > res.adjust_for_width_or_precision (dir.prec, dirtype, base, > - (sign | maybebase) + (base == 16)); > + (sign | maybebase) + (base == 2 || base == 16)); > > return res; > } > @@ -3680,6 +3695,8 @@ parse_directive (call_info &info, > ++pf; > break; > } > + > + Drop this spurious change. > switch (target_to_host (*pf)) > { > @@ -3713,6 +3730,14 @@ parse_directive (call_info &info, > case 'X': > dir.fmtfunc = format_integer; > break; > + > + case 'b': > + case 'B': > + if (b_overflow_flag) { > + dir.fmtfunc = format_integer; > + break; > + } > + return 0; > > case 'p': > /* The %p output is implementation-defined. It's possible > @@ -4038,6 +4063,9 @@ compute_format_length (call_info &info, format_result *res, > > bool success = true; > > + /* Check for GNU C2X standart */ > + b_overflow_flag = check_std_c2x (); > + > for (const char *pf = info.fmtstr; ; ++dirno) > { > directive dir (&info, dirno); > diff --git a/gcc/testsuite/gcc.dg/Wformat-overflow1.c b/gcc/testsuite/gcc.dg/Wformat-overflow1.c > new file mode 100644 > index 00000000000..cf9766fae14 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/Wformat-overflow1.c > @@ -0,0 +1,28 @@ > +/* > + { dg-do compile } > + { dg-options "-Wformat-overflow -std=c2x" } > +*/ > + > +extern int sprintf (char* restrict, const char* restrict, ...); > + > +void test_warn () { > + > + int n = __INT_MAX__; > + char dst [5] = {0}; > + sprintf (dst, "%b", n); /* { dg-warning "-Wformat-overflow" } */ > + > + sprintf (dst, "%#b", n); /* { dg-warning "-Wformat-overflow" } */ > + > +} > + > +void test_no_warn () { > + > + char dst [5] = {0}; > + int n = 8; > + sprintf (dst, "%b", n); > + > + char another_dst [34] = {0}; > + n = __INT_MAX__; > + sprintf (another_dst, "%#b", n); > + > +} > diff --git a/gcc/testsuite/gcc.dg/Wformat-overflow2.c b/gcc/testsuite/gcc.dg/Wformat-overflow2.c > new file mode 100644 > index 00000000000..c6b1d9062a6 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/Wformat-overflow2.c > @@ -0,0 +1,16 @@ > +/* > + { dg-do compile } > + { dg-options "-Wformat-overflow -std=c11" } > +*/ > + > +extern int sprintf (char* restrict, const char* restrict, ...); > + > +void test_no_warn () { > + > + /*There is no reason to throw warning if std < c2x*/ > + > + char dst [5] = {0}; > + int n = 32; > + sprintf (dst, "%b", n); > + > +} > -- > 2.25.1 > Marek