From patchwork Fri Apr 25 21:06:33 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 884436 Delivered-To: patch@linaro.org Received: by 2002:a5d:474d:0:b0:38f:210b:807b with SMTP id o13csp4162741wrs; Fri, 25 Apr 2025 14:07:32 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVsYKTNvL6Cw9Xh08BsSDxhOsl7ev//13WK7j3zgfL78QtJUjEvsoezBu2PmsrCFs/unDcatw==@linaro.org X-Google-Smtp-Source: AGHT+IHOGng5462M1j3ttYDAyaj+LCQqYwaEv6uMVtmat5NvNHSfQWw9YvlPsIP3YRj7XlvVKjcU X-Received: by 2002:a05:6214:238a:b0:6e6:6598:84c1 with SMTP id 6a1803df08f44-6f4d1f9f9d3mr14612966d6.42.1745615252523; Fri, 25 Apr 2025 14:07:32 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1745615252; cv=pass; d=google.com; s=arc-20240605; b=Ka3R7G3eBDJdedd4pKTDItBo1O/43gUBseD9snGZWArbNrzU6x7B4lP1H8QS+qoqKl Fw0GBUUsNMLZ3OVm7cmQ0pSjNEGMThu3+od5rHxNPJPDotYdGLOXAfl3OHIjRroegDXz YY2GB792mNRpt/4aLu0xS2WMKIrmZQfKUdZxmCLPj4gGXXTiEJ5q1OPYZmrtayNpOz/m MnF/awS33z9Uy6KKWApHpw5x5sQX2fMYdJ103nw5brxCGDm0E1ywNF8uedl+JGus/6+5 LiBnZZlge6RbdQ6wpfsR5JlG13eIKmW3pjSxqMogwjA6NNtj5wzsVSZYLCFFE9MQNIWp QaUg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :dkim-filter:arc-filter:dmarc-filter:delivered-to:dkim-filter; bh=UfP5Zk/3c+64BG1DKXEGc8s2C/PdPTnXefy9MgJAUP4=; fh=y8c+zjefmHK4fStUusN64Sx8n6mpTt8O9P1WOVzrReM=; b=kbXWcsvOozWRxL8+FnuOlbJtyiRaWe37UwY3qufhexXOepFqmbuz1OMQvbKYv8ECi+ QEcCU8z9MGvSKdRsKmqXMLiIWEelB+CrxqbGVFbXdtj3EHqSJuNO8JzOIQU+qT+LEX4G UuGA37pOGLwohF75CiYXBkvor/udY5N4+PIo7sdBeT+J+SpcTaHv8xUOQGC2jJz5MhlP u0bgGt290ktF7dsDsB81clznNklPHot5GkWYzZh+TXIuNkd9tAtO10ZhJjsIfR7tWJIj QA5JIVfsfd6QbpMta6bvH1U+Mf49Dngo2qN6QNnvmsG5dhP+IoEgvqIWLjVW3uSSWsdJ 47yA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=BTRZZqwK; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="libc-alpha-bounces~patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id 6a1803df08f44-6f4c0acaf84si44317276d6.199.2025.04.25.14.07.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Apr 2025 14:07:32 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=BTRZZqwK; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="libc-alpha-bounces~patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 12BC53858406 for ; Fri, 25 Apr 2025 21:07:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 12BC53858406 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=BTRZZqwK X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x62b.google.com (mail-pl1-x62b.google.com [IPv6:2607:f8b0:4864:20::62b]) by sourceware.org (Postfix) with ESMTPS id C3D873858C60 for ; Fri, 25 Apr 2025 21:06:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C3D873858C60 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C3D873858C60 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::62b ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1745615204; cv=none; b=Tf+e+RHztJPcysG7rxG8qxluplucgxLoMLxI/qrIXO5/Cg9b4iKNazdY7CADDBeYDrT7iBPVeEiNqgy1ZUMLriEYJ6lvasvsxj5+z5N1uE9KJleROPMa151bxad+Tk+sp91ItYemxiomIRaUTRCy5u69n/5jyjg4Y9k8OFt8pIg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1745615204; c=relaxed/simple; bh=y/frrD+Ps6G8oHrBpmz89Pjs56uoGz7vM3YpGMPVOEU=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=lhs1vJYOqkTQjxt5rhxH6KRN/CAgZjY1/E6k03n554UF/LwpZ4YVzFWXcHHlo+V0ZtVAUxVHJxmzhPs0tSOnQv8FEp1MLNPfzWFQLS/yJ8JIhefHXzkyzpUApZEHK0DSUe1hfAz9dQEPLe6wlbmpsLt2UojeZue/D7XnFfDXCbM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C3D873858C60 Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-2241053582dso43322585ad.1 for ; Fri, 25 Apr 2025 14:06:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1745615202; x=1746220002; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=UfP5Zk/3c+64BG1DKXEGc8s2C/PdPTnXefy9MgJAUP4=; b=BTRZZqwK7JxUaMdbG81tic0NCjDClqpgTWW+1cb8nZYOhaBYNjn+6g5Y67eqU1X4PP gcaN6a5VIkEL0lkVWW8/X5wy5tKmtOsWZpf2WVsaqYoIXEqWoHJFbFOKU+lE1hYLzhwo hrkQ44Y+KiTGctMzYGrQ6OkGSXbG2igH1KOVOfrQCKKBOPgUP2iQ824tdY3HaL+ZMN1s f43q5NTf28nSq4BHTCzUhmcc7k7S50v2UJdMA0RslsDyJuK0gGnJMlLA5+U+veIlCYt6 4/YPL0n58RXu5KjZEi4KBhSqzI9qHwtfRlFa52aydTHZQCDj6/eimHojfN6vXqvMnMD8 IF+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745615202; x=1746220002; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=UfP5Zk/3c+64BG1DKXEGc8s2C/PdPTnXefy9MgJAUP4=; b=mUKKl4dwagJNw0161+879Q+MlewLJNa92xt6KovXImSBuFRj1RyvxyfdBy4SgTn6QK zDeR7zwf9BWjQPI91fkKd+ujYI/rRaFk+Mkj8hrsptYxoBokyiiNF7eKTOf8NN0L6wxe q8iN02petpRYLLN+SS2h51nY8463bf3TsrlyUUCopwGl0JC4dvTAcgk/5X3MStrXzVfz TorJP3T3Xm4zCa2rBBo4cczfToXjLBfBcBD0BUgFLCKnN0eUy9odifcZMZAJ9RCb1a2t EP3UeBKZM7nAYsJ09YnNeZDfqQuhPYqmQcU+QZqBG1eKDiPKAv6zuMuZoVZ6yAksYFhs AWdw== X-Gm-Message-State: AOJu0YzoxME6QY+ZiwouBEwr1cac76O04BJFUoESFcRir56RJ/TJVGsU mN1jI7lhBHA0WjDhaywra3i9vDeQSjgbq6DonLHURymmGFb+0y5VpXyKwISVAGwxGNHte7yGw7T R X-Gm-Gg: ASbGncumcV52J/ieDVCB9Mtasa9/6eiJKs5yRGqiuapv4D5guALWiyQQzfJx8d1l6nM oA6Tu0I4QxQp1DlUPbcOP2L21JORBsKUMuKepBtbpzKTYP0CjM+2OLSDF6nj+ehSIjGfJpQOBiN INmo5Prois6IMQNXO/D/UgewHep3zPzCmuG1c/NcOd+Ce1ln2DKa+yLrzYsFyaflxHAUp/tw04o hfpfTTsgPX2tJONS41+126jYiGqVLVMCZw+xd7JlGtrzMU+t/4Rv+vZokl30WcMMEJDEcaiiznM fmKBQ7VhzaFx9t7nOJ/ya+dqhMJ7qnZyn5/JYgGp5p4djrwTqraxzfgWB/aQc+3W X-Received: by 2002:a17:902:e841:b0:223:f9a4:3fb6 with SMTP id d9443c01a7336-22dc69f83dfmr15730955ad.11.1745615202504; Fri, 25 Apr 2025 14:06:42 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c0:9bf1:37fb:44e3:5707:516b]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-22db50e7613sm37144875ad.143.2025.04.25.14.06.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Apr 2025 14:06:42 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Joseph Myers Subject: [PATCH] stdio: Remove UB on printf_fp Date: Fri, 25 Apr 2025 18:06:33 -0300 Message-ID: <20250425210638.2620532-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patch=linaro.org@sourceware.org The __printf_fp_buffer_1 issues count_leading_zeros with 0 argument, which might leads to call __builtin_ctz depending of the ABI. Replace with stdbit.h function instead. Checked on x86_64-linux-gnu and i686-linux-gnu. Reviewed-by: Paul Eggert --- stdio-common/printf_fp.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/stdio-common/printf_fp.c b/stdio-common/printf_fp.c index 5b46ddcaec..0039e1b20b 100644 --- a/stdio-common/printf_fp.c +++ b/stdio-common/printf_fp.c @@ -29,7 +29,6 @@ #include #include #include -#include #include #include #include @@ -40,6 +39,7 @@ #include #include #include +#include #include #include #include @@ -386,7 +386,7 @@ __printf_fp_buffer_1 (struct __printf_buffer *buf, locale_t loc, { int cnt; MPN_ASSIGN (p.scale, p.tmp); - count_leading_zeros (cnt, p.scale[p.scalesize - 1]); + cnt = stdc_leading_zeros (p.scale[p.scalesize - 1]); scaleexpo = (p.scalesize - 2) * BITS_PER_MP_LIMB - cnt - 1; exp10 |= 1 << explog; } @@ -408,7 +408,7 @@ __printf_fp_buffer_1 (struct __printf_buffer *buf, locale_t loc, ; /* Determine number of bits the scaling factor is misplaced. */ - count_leading_zeros (cnt_h, p.scale[p.scalesize - 1]); + cnt_h = stdc_leading_zeros (p.scale[p.scalesize - 1]); if (cnt_h == 0) { @@ -426,17 +426,17 @@ __printf_fp_buffer_1 (struct __printf_buffer *buf, locale_t loc, { if (p.scale[i] != 0) { - count_trailing_zeros (cnt_l, p.scale[i]); + cnt_l = stdc_trailing_zeros (p.scale[i]); if (p.frac[i] != 0) { int cnt_l2; - count_trailing_zeros (cnt_l2, p.frac[i]); + cnt_l2 = stdc_trailing_zeros (p.frac[i]); if (cnt_l2 < cnt_l) cnt_l = cnt_l2; } } else - count_trailing_zeros (cnt_l, p.frac[i]); + cnt_l = stdc_trailing_zeros (p.frac[i]); /* Now shift the numbers to their optimal position. */ if (i == 0 && BITS_PER_MP_LIMB - cnt_h > cnt_l) @@ -528,7 +528,7 @@ __printf_fp_buffer_1 (struct __printf_buffer *buf, locale_t loc, if (cy == 0) --p.tmpsize; - count_leading_zeros (cnt_h, p.tmp[p.tmpsize - 1]); + cnt_h = stdc_leading_zeros (p.tmp[p.tmpsize - 1]); incr = (p.tmpsize - p.fracsize) * BITS_PER_MP_LIMB + BITS_PER_MP_LIMB - 1 - cnt_h; @@ -584,7 +584,7 @@ __printf_fp_buffer_1 (struct __printf_buffer *buf, locale_t loc, } else { - count_trailing_zeros (cnt_l, p.tmp[i]); + cnt_l = stdc_trailing_zeros (p.tmp[i]); /* Now shift the numbers to their optimal position. */ if (i == 0 && BITS_PER_MP_LIMB - 1 - cnt_h > cnt_l) @@ -630,7 +630,7 @@ __printf_fp_buffer_1 (struct __printf_buffer *buf, locale_t loc, p.tmpsize = p.fracsize; assert (cy == 0 || p.tmp[p.tmpsize - 1] < 20); - count_trailing_zeros (cnt_l, p.tmp[0]); + cnt_l = stdc_trailing_zeros (p.tmp[0]); if (cnt_l < MIN (4, p.exponent)) { cy = __mpn_lshift (p.frac, p.tmp, p.tmpsize,