From patchwork Mon Feb 12 17:09:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 199937 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp51303dyb; Mon, 12 Feb 2024 09:10:12 -0800 (PST) X-Google-Smtp-Source: AGHT+IHGt9fgQeYKGwO8GIaecUf46A6eUL73DDFtAm0NHm0b5uvbMVXNxk6SThosoJESA+wsBPPr X-Received: by 2002:a37:c447:0:b0:783:8046:7b73 with SMTP id h7-20020a37c447000000b0078380467b73mr8671806qkm.20.1707757811818; Mon, 12 Feb 2024 09:10:11 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707757811; cv=pass; d=google.com; s=arc-20160816; b=XnT3OdIXAlmuXMNxmGfpKjF7EbCVQdygheWYIlW1BShJ8JRAfyyWnc8GR4YXDGxStU N2+JzWQC3/47tiQrdz1CM8jHncnVhAMVENYF8POVEUerOLCfWgL0w5JVPlMswX/bhJWa OOL7mxcksBflSF7Wg35v2iY6fZM95HIc/Rg2qgJd1PhsrRr7qJe/RXtRuP3Ed15xwgSQ 9vTuDEzbpNMUlrmj72wGIq+CMkgJzabA7KsggmoZeWMXrN66TMEuHhSpYx3n21IMhWND hpICDYqZ/LC9iJn5zvaQreCGt95fgMOqZFE/icOcoB+S7t3vSJakKRiYpprkFdvzixwT bCXg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:reply-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-disposition :mime-version:message-id:subject:cc:to:from:date:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=9qFWzzXPVE5TKAzrNrOJ4P6h+7jTc7Ye4b05g5vMUlc=; fh=pf8+aJa8ann/Gum4ZTZRI6g4hawT5UcoLxlaUya/H0U=; b=Bc1qQBNy2UKOzqxK+pPBHDxkwnmcMvSZt9jAaNWPfdF6j5UDSU/uXiYAQ4qgUshcTQ XxJvSM5Tg1nv3aPknN69vo9aXwy+PjtCqFMZuGxiqQcHFSCuWeDbXHtYqitjX1gO9wpM NoaUDlqQp4gZF3u8EDPS8MihAqYcnc0L+DxQy3jD/Qy/MJ9kuVooxXvCLD+eqZB5Dquf UR6L94VH/5QvAeUEq8QLBsqgnzuqPR4vDIp+TWO7Vgzpztw8vgv8CtW+mgJjIg1LipFn +RwhdwdhPRMyTzihBgSessmYbrgVXUjKoIBfJ2GxovFEwA9a8BMWBNruHBYEee1CsN3p NKlw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=fL57DjSk; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Forwarded-Encrypted: i=2; AJvYcCWdIltG8F/oTFF2d4IE54dzfWyp5YHG5jRA4ebSV2suiwBh5TK0MSitZwYi70g2MYkp8vNOK93isPffCVKNJSe/n5jMSg== Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id x18-20020a05620a14b200b007840499694csi6171381qkj.206.2024.02.12.09.10.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Feb 2024 09:10:11 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=fL57DjSk; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7B061385840F for ; Mon, 12 Feb 2024 17:10:11 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 58D763858D38 for ; Mon, 12 Feb 2024 17:09:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 58D763858D38 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 58D763858D38 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707757765; cv=none; b=McuaORypIFa7wMS2eEamGSuF5P4dgR3z30r14SHjw1IULU/P3qzzSbGJIS7gRnr+5SetbVQr4993S+eOCP43jwqx4eaquHHVEpCBl2B3mYl9BM1C8XXbDNTkemB4BnotLgFzBMdJCdDI7rZ2q0Mjvmq+D1Z9y4EVUpbP44PfKvo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707757765; c=relaxed/simple; bh=crcp68319weMJ+W99Nqc7oXH7AppBbX0SGUGak1eXws=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=Xmx0IUZ7vnAndchTrdOyE1PNlL0MM/NsTPj10PItDTh6/syjovjRvFsY1Yo8YMFxY0ln928x5FAIaS5VJt468xHCxEEV61eWhiOsVvTwWjA8T4QILuRg/m/vV7UMFC35GHPWbCs+uTe0+iAr99U8aSZ1iEDGywZZSl30K4CCp+g= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707757763; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=9qFWzzXPVE5TKAzrNrOJ4P6h+7jTc7Ye4b05g5vMUlc=; b=fL57DjSkcaOCYWvOdoaOxopaSm2KCtnge6wlFiJhJ97AEMN+/wVFqfhhkAlKzS5yB2k+UZ FXa9Mfc++7/TOWdsMa5za9GuzYEBCY3b/vXxYgyiljvhVnyS0MFviMA4qaQX7RaFjEP1XL 4FK9NZuW+4iIFb9CHF8pCG3EcaHYeEA= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-160-ZPkkVa5yMjeqBOjqqmB4tQ-1; Mon, 12 Feb 2024 12:09:19 -0500 X-MC-Unique: ZPkkVa5yMjeqBOjqqmB4tQ-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 63C5C28116A1; Mon, 12 Feb 2024 17:09:19 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.8]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 268E62022AAA; Mon, 12 Feb 2024 17:09:19 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 41CH9GRq2671447 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Mon, 12 Feb 2024 18:09:16 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 41CH9FhT2671446; Mon, 12 Feb 2024 18:09:15 +0100 Date: Mon, 12 Feb 2024 18:09:15 +0100 From: Jakub Jelinek To: Richard Biener , Jeff Law Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] libiberty: Fix up libiberty_vprintf_buffer_size Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-4.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790713855627265086 X-GMAIL-MSGID: 1790713855627265086 Hi! When writing the HOST_SIZE_T_PRINT_UNSIGNED incremental patch, my first bootstrap failed on i686-linux. That is because I've also had @@ -1344,8 +1344,10 @@ adjust_field_rtx_def (type_p t, options_ } subfields = create_field (subfields, t, - xasprintf (".fld[%lu].%s", - (unsigned long) aindex, + xasprintf (".fld[" + HOST_SIZE_T_PRINT_UNSIGNED + "].%s", + (fmt_size_t) aindex, subname)); subfields->opt = nodot; if (t == note_union_tp) hunk in gengtype.cc. While sprintf obviously can print in this case %llu with fmt_size_t being unsigned long long (that is another bug I'll fix incrementally), seems libiberty_vprintf_buffer_size can't deal with that, it ignores h, hh, l, ll and L modifiers and unconditionally, estimates 30 chars as upper bounds for integers (that is fine) and then uses (void) va_arg (ap, int); to skip over the argument regardless if it was %d, %ld, %lld, %hd, %hhd etc. Now, on x86_64 that happens to work fine probably for all of those, on ia32 for everything but %lld, because it then skips just one half of the long long argument; now as there is %s after it, it will try to compute strlen not from the pointer argument corresponding to %s, but from the most significant half of the previous long long argument. So, the following patch attempts not to completely ignore the modifiers, but figure out from them whether to va_arg an int (used for h and hh as well), or long, or long long, or size_t, or ptrdiff_t - added support for z and t there, plus for Windows I64. And also %Lf etc. for long double. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2024-02-12 Jakub Jelinek * vprintf-support.c (libiberty_vprintf_buffer_size): Handle properly l, ll, z, t or on _WIN32 I64 modifiers for diouxX and L modifier for fFgGeE. Jakub --- libiberty/vprintf-support.c.jj 2024-01-03 12:07:48.464085595 +0100 +++ libiberty/vprintf-support.c 2024-02-12 09:29:35.265651819 +0100 @@ -56,6 +56,7 @@ libiberty_vprintf_buffer_size (const cha { if (*p++ == '%') { + int prec = 0; while (strchr ("-+ #0", *p)) ++p; if (*p == '*') @@ -76,8 +77,43 @@ libiberty_vprintf_buffer_size (const cha else total_width += strtoul (p, (char **) &p, 10); } - while (strchr ("hlL", *p)) - ++p; + do + { + switch (*p) + { + case 'h': + ++p; + continue; + case 'l': + case 'L': + ++prec; + ++p; + continue; + case 'z': + prec = 3; + ++p; + continue; + case 't': + prec = 4; + ++p; + continue; +#ifdef _WIN32 + case 'I': + if (p[1] == '6' && p[2] == '4') + { + prec = 2; + p += 3; + continue; + } + break; +#endif + default: + break; + } + break; + } + while (1); + /* Should be big enough for any format specifier except %s and floats. */ total_width += 30; switch (*p) @@ -88,6 +124,15 @@ libiberty_vprintf_buffer_size (const cha case 'u': case 'x': case 'X': + switch (prec) + { + case 0: (void) va_arg (ap, int); break; + case 1: (void) va_arg (ap, long int); break; + case 2: (void) va_arg (ap, long long int); break; + case 3: (void) va_arg (ap, size_t); break; + case 4: (void) va_arg (ap, ptrdiff_t); break; + } + break; case 'c': (void) va_arg (ap, int); break; @@ -96,10 +141,18 @@ libiberty_vprintf_buffer_size (const cha case 'E': case 'g': case 'G': - (void) va_arg (ap, double); - /* Since an ieee double can have an exponent of 307, we'll - make the buffer wide enough to cover the gross case. */ - total_width += 307; + if (!prec) + { + (void) va_arg (ap, double); + /* Since an ieee double can have an exponent of 308, we'll + make the buffer wide enough to cover the gross case. */ + total_width += 308; + } + else + { + (void) va_arg (ap, long double); + total_width += 4932; + } break; case 's': total_width += strlen (va_arg (ap, char *));