From patchwork Mon Jan 30 09:53:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Schwab X-Patchwork-Id: 63909 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 E0EA93858C78 for ; Mon, 30 Jan 2023 09:53:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E0EA93858C78 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1675072427; bh=gGCTnpNi5Tav5ZSYpAeblO0RcJ/OmntFhip3O3gCmQ8=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=xpW/vmm7FEvkviiZnta/CTlYIDT9HMjwGwf6nM0ClGBSNJXlL5IHWgT9CVaxjRFDY 914PMbDvEfd0XZw3ml8mqc5/4vTwBA8RQMmw8HKknE3z8Axwx8YIEvHBQ9Ny4JwNg5 ep3YxlVQ/BXUjUy4AWZhRZpW4X8cPFh1r3v7ZfaM= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by sourceware.org (Postfix) with ESMTPS id 50DB13858C50 for ; Mon, 30 Jan 2023 09:53:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 50DB13858C50 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 68A201FE0D for ; Mon, 30 Jan 2023 09:53:25 +0000 (UTC) Received: from hawking.suse.de (unknown [10.168.4.11]) by relay2.suse.de (Postfix) with ESMTP id 639792C141 for ; Mon, 30 Jan 2023 09:53:25 +0000 (UTC) Received: by hawking.suse.de (Postfix, from userid 17005) id 44663442F40; Mon, 30 Jan 2023 10:53:25 +0100 (CET) To: libc-alpha@sourceware.org Subject: [PATCH] Account for octal marker in %#o format X-Yow: Is this going to involve RAW human ecstasy? Date: Mon, 30 Jan 2023 10:53:25 +0100 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux) MIME-Version: 1.0 X-Spam-Status: No, score=-9.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Andreas Schwab via Libc-alpha From: Andreas Schwab Reply-To: Andreas Schwab Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" --- stdio-common/Makefile | 1 + stdio-common/tst-printf-oct.c | 50 +++++++++++++++++++++++++++++ stdio-common/vfprintf-process-arg.c | 6 ++++ 3 files changed, 57 insertions(+) create mode 100644 stdio-common/tst-printf-oct.c Tested-by: Carlos O'Donell Reviewed-by: Carlos O'Donell diff --git a/stdio-common/Makefile b/stdio-common/Makefile index 8150453944..c787450e5c 100644 --- a/stdio-common/Makefile +++ b/stdio-common/Makefile @@ -207,6 +207,7 @@ tests := \ tst-printf-bz25691 \ tst-printf-fp-free \ tst-printf-fp-leak \ + tst-printf-oct \ tst-printf-round \ tst-printfsz \ tst-put-error \ diff --git a/stdio-common/tst-printf-oct.c b/stdio-common/tst-printf-oct.c new file mode 100644 index 0000000000..d1c88d5641 --- /dev/null +++ b/stdio-common/tst-printf-oct.c @@ -0,0 +1,50 @@ +/* Test printf width padding with octal format + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include + +static int +do_test (void) +{ + char buf[80]; + + sprintf (buf, "%-7o", 123); + TEST_COMPARE_STRING (buf, "173 "); + + sprintf (buf, "%#-7o", 123); + TEST_COMPARE_STRING (buf, "0173 "); + + sprintf (buf, "%7o", 123); + TEST_COMPARE_STRING (buf, " 173"); + + sprintf (buf, "%#7o", 123); + TEST_COMPARE_STRING (buf, " 0173"); + + sprintf (buf, "%07o", 123); + TEST_COMPARE_STRING (buf, "0000173"); + + sprintf (buf, "%#07o", 123); + TEST_COMPARE_STRING (buf, "0000173"); + + return 0; +} + +#include diff --git a/stdio-common/vfprintf-process-arg.c b/stdio-common/vfprintf-process-arg.c index cd3eaf5c0c..279a0efa9d 100644 --- a/stdio-common/vfprintf-process-arg.c +++ b/stdio-common/vfprintf-process-arg.c @@ -196,6 +196,9 @@ LABEL (unsigned_number): /* Unsigned number of base BASE. */ /* Account for 0X, 0x, 0B or 0b hex or binary marker. */ width -= 2; + if (octal_marker) + --width; + if (is_negative || showsign || space) --width; @@ -257,6 +260,9 @@ LABEL (unsigned_number): /* Unsigned number of base BASE. */ width -= 2; } + if (octal_marker) + --width; + width -= number_length + prec; Xprintf_buffer_pad (buf, L_('0'), prec);