From patchwork Tue Jun 9 14:38:41 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 39522 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 BF5FC388B00A; Tue, 9 Jun 2020 14:39:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BF5FC388B00A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1591713559; bh=7D4vFpn5yCecM0EWMrrY1Y6nLnHCRfgvM/xYi33Y+1k=; h=References:In-Reply-To:Date:Subject:To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=cE1S2w2S0XRs49IOSHqFs4qXecbuYpKFFIfapV+aRq8YLR8M3jUpt96A58oMTk6Pf WMQV+YHrXcBRi4NnOgs4K9mPuXlrGQdZhbXTMxeHtptNdm5orirhWTZWnpQPfQvXdK x32qF0I0vr45y6h/g0sM9lQgj5NmZHGVh3PeeuDI= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-il1-x142.google.com (mail-il1-x142.google.com [IPv6:2607:f8b0:4864:20::142]) by sourceware.org (Postfix) with ESMTPS id DAE13388A80E for ; Tue, 9 Jun 2020 14:39:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org DAE13388A80E Received: by mail-il1-x142.google.com with SMTP id g3so20475806ilq.10 for ; Tue, 09 Jun 2020 07:39:17 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=7D4vFpn5yCecM0EWMrrY1Y6nLnHCRfgvM/xYi33Y+1k=; b=VPJdXUmhiqD6KcwPvMhaSlLaQSf1sYArvsg7y7b2x6z2kzgVy2bevrcag8aJk9M9Kr t45JuBVss9SE8344zHGrceQN5PodRSYYqRfGVjout0ldfZTmCFvx2Yr/C2vW+IiFHJJY 6K4fND7CC+quzphZK5QGF1PcSxcTZwrwUMTw+YzPGrNaoPV4rL8Ftk4Yxdz8zMDqpITk 7QvD2UMMeDuoPXZ9XVoEpi6vi2UP9VejhdS63P1hDh3XwKJTb8m2ZpEqRjk3P75C8L+W TpZ/pt3jL1g74iLwMzImTwfQTDkWa8ozfsqe22M3r6ZjQjbO4T1HF8/tz9FxQAVu8Wy0 MYIA== X-Gm-Message-State: AOAM533IxLHvS2mRSvAbZfwDiaWU9K0E/f84/NnZeCMCkVbBl0KbFDbv zCOHammqkTZCe9MVEzyp0thVrGS6ChPZJjA78i4= X-Google-Smtp-Source: ABdhPJzXJfMZjTnHW6oxY2yjoz64di3P/tL2kwBBeFnYJAx2R0cpxn2drYWaA13AJmz09ZKdvudHsbAyHzIXVH+sJ+M= X-Received: by 2002:a92:244c:: with SMTP id k73mr6602473ilk.292.1591713557265; Tue, 09 Jun 2020 07:39:17 -0700 (PDT) MIME-Version: 1.0 References: <20200605150324.460300-1-hjl.tools@gmail.com> In-Reply-To: Date: Tue, 9 Jun 2020 07:38:41 -0700 Message-ID: Subject: V2 [PATCH] Add "%d" support to _dl_debug_vdprintf To: Adhemerval Zanella X-Spam-Status: No, score=-9.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, 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: 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: "H.J. Lu via Libc-alpha" From: "H.J. Lu" Reply-To: "H.J. Lu" Cc: GNU C Library Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" On Tue, Jun 9, 2020 at 7:18 AM Adhemerval Zanella wrote: > > > > On 05/06/2020 12:03, H.J. Lu via Libc-alpha wrote: > > "%d" will be used to print out signed value. > > LGTM with some smalls nits below. > > > --- > > elf/dl-misc.c | 31 +++++++++++++++++++++++++++++-- > > 1 file changed, 29 insertions(+), 2 deletions(-) > > > > diff --git a/elf/dl-misc.c b/elf/dl-misc.c > > index ab70481fda..c82c8ae6fa 100644 > > --- a/elf/dl-misc.c > > +++ b/elf/dl-misc.c > > @@ -167,6 +167,7 @@ _dl_debug_vdprintf (int fd, int tag_p, const char *fmt, va_list arg) > > switch (*fmt) > > { > > /* Integer formatting. */ > > + case 'd': > > case 'u': > > case 'x': > > { > > Ok. > > > @@ -179,11 +180,34 @@ _dl_debug_vdprintf (int fd, int tag_p, const char *fmt, va_list arg) > > #else > > unsigned long int num = va_arg (arg, unsigned int); > > #endif > > + bool negative = false; > > + if (*fmt == 'd') > > + { > > +#if LONG_MAX != INT_MAX > > + if (long_mod) > > + { > > + if ((long) num < 0) > > Full type specify on cast. Fixed. > > + negative = true; > > + } > > + else > > + { > > + if ((int) num < 0)> + { > > + num = (unsigned int) num; > > + negative = true; > > + } > > + } > > +#else > > + if ((int) num < 0) > > + negative = true; > > +#endif > > + } > > + > > /* We use alloca() to allocate the buffer with the most > > pessimistic guess for the size. Using alloca() allows > > having more than one integer formatting in a call. */ > > - char *buf = (char *) alloca (3 * sizeof (unsigned long int)); > > - char *endp = &buf[3 * sizeof (unsigned long int)]; > > + char *buf = (char *) alloca (1 + 3 * sizeof (unsigned long int)); > > + char *endp = &buf[1 + 3 * sizeof (unsigned long int)]; > > I think we can remove alloca here and use INT_STRLEN_BOUND (since the > string is not '\0' bounded due the writev usage). Something like: > > char buf[INT_STRLEN_BOUND (unsigned long int)]; > char *endp = &buf[INT_STRLEN_BOUND (unsigned long int)]; Fixed. > > char *cp = _itoa (num, endp, *fmt == 'x' ? 16 : 10, 0); > > > > /* Pad to the width the user specified. */ > > @@ -191,6 +215,9 @@ _dl_debug_vdprintf (int fd, int tag_p, const char *fmt, va_list arg) > > while (endp - cp < width) > > *--cp = fill; > > > > + if (negative) > > + *--cp = '-'; > > + > > iov[niov].iov_base = cp; > > iov[niov].iov_len = endp - cp; > > ++niov; > > > > Ok. Here is the updated patch I am checking in. Thanks. From 6174817fdd86eab9b0ae396dbf5cc9b99ab06bfb Mon Sep 17 00:00:00 2001 From: "H.J. Lu" Date: Fri, 5 Jun 2020 07:59:25 -0700 Subject: [PATCH] Add "%d" support to _dl_debug_vdprintf "%d" will be used to print out signed value and replace alloca with INT_STRLEN_BOUND. --- elf/dl-misc.c | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/elf/dl-misc.c b/elf/dl-misc.c index ab70481fda..a88d6370bb 100644 --- a/elf/dl-misc.c +++ b/elf/dl-misc.c @@ -31,6 +31,7 @@ #include #include #include +#include #include <_itoa.h> #include #include @@ -167,6 +168,7 @@ _dl_debug_vdprintf (int fd, int tag_p, const char *fmt, va_list arg) switch (*fmt) { /* Integer formatting. */ + case 'd': case 'u': case 'x': { @@ -179,11 +181,31 @@ _dl_debug_vdprintf (int fd, int tag_p, const char *fmt, va_list arg) #else unsigned long int num = va_arg (arg, unsigned int); #endif - /* We use alloca() to allocate the buffer with the most - pessimistic guess for the size. Using alloca() allows - having more than one integer formatting in a call. */ - char *buf = (char *) alloca (3 * sizeof (unsigned long int)); - char *endp = &buf[3 * sizeof (unsigned long int)]; + bool negative = false; + if (*fmt == 'd') + { +#if LONG_MAX != INT_MAX + if (long_mod) + { + if ((long int) num < 0) + negative = true; + } + else + { + if ((int) num < 0) + { + num = (unsigned int) num; + negative = true; + } + } +#else + if ((int) num < 0) + negative = true; +#endif + } + + char buf[INT_STRLEN_BOUND (long int)]; + char *endp = &buf[INT_STRLEN_BOUND (long int)]; char *cp = _itoa (num, endp, *fmt == 'x' ? 16 : 10, 0); /* Pad to the width the user specified. */ @@ -191,6 +213,9 @@ _dl_debug_vdprintf (int fd, int tag_p, const char *fmt, va_list arg) while (endp - cp < width) *--cp = fill; + if (negative) + *--cp = '-'; + iov[niov].iov_base = cp; iov[niov].iov_len = endp - cp; ++niov; -- 2.26.2