From patchwork Wed May 17 20:14:02 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?UmFkaW0gS3LEjW3DocWZ?= X-Patchwork-Id: 9731847 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 5FD27600CC for ; Wed, 17 May 2017 20:15:20 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2D92B287D6 for ; Wed, 17 May 2017 20:15:20 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 21F4A287E5; Wed, 17 May 2017 20:15:20 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,HK_RANDOM_FROM, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CCC11287D6 for ; Wed, 17 May 2017 20:15:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754383AbdEQUPR (ORCPT ); Wed, 17 May 2017 16:15:17 -0400 Received: from mx1.redhat.com ([209.132.183.28]:55132 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754388AbdEQUPR (ORCPT ); Wed, 17 May 2017 16:15:17 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 898FC80B52 for ; Wed, 17 May 2017 20:15:16 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 898FC80B52 Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=rkrcmar@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 898FC80B52 Received: from potion (dhcp-1-124.brq.redhat.com [10.34.1.124]) by smtp.corp.redhat.com (Postfix) with SMTP id DEBA7177E5; Wed, 17 May 2017 20:15:14 +0000 (UTC) Received: by potion (sSMTP sendmail emulation); Wed, 17 May 2017 22:15:14 +0200 From: =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= To: kvm@vger.kernel.org Cc: Paolo Bonzini Subject: [kvm-unit-tests PATCH 3/6] libcflat: fix padding in printf Date: Wed, 17 May 2017 22:14:02 +0200 Message-Id: <20170517201405.19867-4-rkrcmar@redhat.com> In-Reply-To: <20170517201405.19867-1-rkrcmar@redhat.com> References: <20170517201405.19867-1-rkrcmar@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Wed, 17 May 2017 20:15:16 +0000 (UTC) Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP A simple test: printf(".%8p.\n", (void *)0); printf(".%-8p.\n", (void *)0); printf(".%8p.\n", (void *)1); printf(".%-8p.\n", (void *)1); printf(".%8p.\n", (void *)0x123456); printf(".%-8p.\n", (void *)0x123456); printf(".%2p.\n", (void *)0); printf(".%-2p.\n", (void *)0); printf(".%2p.\n", (void *)1); printf(".%-2p.\n", (void *)1); glibc: . (nil). .(nil) . . 0x1. .0x1 . .0x123456. .0x123456. .(nil). .(nil). .0x1. .0x1. before patch: . 0x 0. .0x 0 . . 0x 1. .0x 1 . . 0x 123456. .0x 123456 . .0x 0. .0x0 . .0x 1. .0x1 . after patch: . 0. .0 . . 0x1. .0x1 . .0x123456. .0x123456. . 0. .0 . .0x1. .0x1. (nil) would be possible with a small change, but the standard leaves it to the implementation and 0 is acceptable, IMO. Signed-off-by: Radim Krčmář --- lib/printf.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/printf.c b/lib/printf.c index e7818e1ded0e..944768f3080d 100644 --- a/lib/printf.c +++ b/lib/printf.c @@ -18,6 +18,7 @@ typedef struct pstream { typedef struct strprops { char pad; int npad; + bool alternate; } strprops_t; static void addchar(pstream_t *p, char c) @@ -94,7 +95,7 @@ void print_int(pstream_t *ps, long long n, int base, strprops_t props) void print_unsigned(pstream_t *ps, unsigned long long n, int base, strprops_t props) { - char buf[sizeof(long) * 3 + 1], *p = buf; + char buf[sizeof(long) * 3 + 3], *p = buf; int i; while (n) { @@ -104,6 +105,10 @@ void print_unsigned(pstream_t *ps, unsigned long long n, int base, if (p == buf) *p++ = '0'; + else if (props.alternate && base == 16) { + *p++ = 'x'; + *p++ = '0'; + } for (i = 0; i < (p - buf) / 2; ++i) { char tmp; @@ -225,7 +230,7 @@ int vsnprintf(char *buf, int size, const char *fmt, va_list va) } break; case 'p': - print_str(&s, "0x", props); + props.alternate = true; print_unsigned(&s, (unsigned long)va_arg(va, void *), 16, props); break; case 's':