From nobody Sat Feb 7 15:12:26 2026 Received: from mail-pf1-f176.google.com (mail-pf1-f176.google.com [209.85.210.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 373BE35C193 for ; Fri, 30 Jan 2026 10:22:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769768560; cv=none; b=VWmEoq3diKa1zij3z9S0W9OiPTqHVx7l2fRszPE4udA1E/b8rj6HET80Ag9hV/7pQrSiZjhLkxAvBYi95K7PZB51/CUEeMBz+q9RIBJSNpmKXi2kxDwymJO7oblr9Cfq/bhZz3H2m4YN14kb6GMdsUMACDtaF8SttLT/LBYZ9f4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769768560; c=relaxed/simple; bh=7XiXSH3Z4weONLWiwlmhIUy2tc7jdLvm9inSgT/2tms=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=C3/1Cf6/M5IRbBkW54ihELGQNGmbPrr39sRRD3naMYLixFG+U/orV+ibEKfwiDBRRZg1h/jtclrYIuIMPARoxlXgLEfJ8W0DKkYAWj3JQx1UzBQd1zJiTrg+W0ZzpSo9IpI2Y+SXxc69gpV3ypWS903nAq/s/mtxV6Mvj+lTjkg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=SNOrBkP3; arc=none smtp.client-ip=209.85.210.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="SNOrBkP3" Received: by mail-pf1-f176.google.com with SMTP id d2e1a72fcca58-81f39438187so1089847b3a.2 for ; Fri, 30 Jan 2026 02:22:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769768547; x=1770373347; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=CIqzWFCLThNY86Za4t2h7S6Gindx5nUys3coyHssvyM=; b=SNOrBkP3HBM3CP43YnS7Jtj79B7y/s9qLOhXnUEvvi5Ij3eg57/jySYHLpumBn2Vrk xwMz32lEt2gDM7jooAAUaxqa+dy0PjkJX8Rn67eYyMf79zxS8fCsU7YewAU/b3Sv8tkz pqCsIuNFyfnCLkyMEQ79P326R8+7lhLRTsEVpA5p6b/UBqHsQURCp5kb/N6jNDsw7Rbv lUOfSXL/VHT0uAb+QW3fgLcpRgAplu9tXVk0E5t+4BtGdZYZnu3iXOjhvwafaKnpwpto V2fHJO2zLDvhdqZbicIbLyPO59lihYtxDNuAeH9oI+QRNrg9gPnSw9/g8xDiCzkaBuHR RCYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769768547; x=1770373347; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=CIqzWFCLThNY86Za4t2h7S6Gindx5nUys3coyHssvyM=; b=HLthgRX6YBkBV8izJhNuL27BglOEPC2IODDFX7qyuo7MPlFZFJd9HEcU77uZ+qYcI8 B0tJ5dMphFfxTHtBTwPyDI3gqfslfQLylbNVhQDIueK7r2h6ku+J/AzogqZavoSWFCXx OGTCji33h8/5AJeOgz1gDe0m22/2g3MHYkRGhxzCMtk/bBniXR9+Tl2horlEE1vJkfQT YYwT0ZqMaydlNtzz4NkMJY+wCRrV9GQRbn2Kf9jINFyVXhWmPh85SkDvBqH1F4h5gmSl v13pJLkLYlvyaj+sLuXGs6mU9PQLdLnYdHico2z59plqNzAwnEZ6m/PbQstp78LfAXuj Y6Fg== X-Forwarded-Encrypted: i=1; AJvYcCWvqnkHBzDIo8BxPbBYGD/qeO+1v/2D18uDyFVDIP0bp5TAmK4H0S0jWiFz9ddha3Ql6WTPjJfFtbyfOpg=@vger.kernel.org X-Gm-Message-State: AOJu0YxIea6UH4Sy9/aBXN6X0Ur9/W9q5YHZvvvyGgKLUn0/aZKvEx1c K5sRn/bdyrvTZtxkcnjT+Ofr3HVDvLVRhEz3jcpFg2vYNHfKgmIKfBVr X-Gm-Gg: AZuq6aL8vW8+DIX01pEbERo3yM3b0zVoLv4jzVsQHJfzpVtHHjJKut7v3hTZQaH8xBW i1wXe4X/u3JI2ogzF6kT0u/G4XwGHGURkYc4AqmwF8C6uDaXlRmrzdoFBxvejWvNhfsdxft7Dzh 2tC2tYRVsUi6jmge5oTtvxSEfAxQ96lW6Yrl8iDE6qAeYangqQI6Xm0YYmhn4IwHq3rvvW/U3rJ XrMKCLGSohzbJME7LpbL/XItZz1UIhVbtRm8bpAVzwWwy7ovv97d9rtioZH48Q92UQxcQONNk9J qjIbqBVXzLTY68hPHaQIbd96VmzyZVqCxBzu48HbfO5DdH6uVWb0blN7W2kFEimaphwlWU7Kjh1 QZibahC0bjVfoLWsTWCYkEZw0f0O0vpafBGCo3XBqkXhmWny6Bi4iQSx6TFqo3k1FCHAmJ4TSR4 AuHayShu0j74hsAj3jynZHWQ5DsNfEK0E1f/2AMvY0qFZ4AcPA2l9+JcXNbReuUe72b7J1PiYMR 3IYF9eGgBgVgn4k7WBW/A== X-Received: by 2002:a05:6a21:6450:b0:364:1337:647a with SMTP id adf61e73a8af0-392e011afc9mr1846129637.38.1769768547511; Fri, 30 Jan 2026 02:22:27 -0800 (PST) Received: from LAPTOP-UKI113GK ([119.123.175.125]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-c6427da8441sm6647206a12.9.2026.01.30.02.22.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Jan 2026 02:22:26 -0800 (PST) From: "licheng.li" To: Willy Tarreau , David Laight Cc: =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , linux-kernel@vger.kernel.org, im.lechain@gmail.com Subject: [PATCH v4 1/2] tools/nolibc: support left alignment (-) in printf Date: Fri, 30 Jan 2026 18:22:17 +0800 Message-ID: <20260130102218.1324-2-im.lechain@gmail.com> X-Mailer: git-send-email 2.51.0.windows.1 In-Reply-To: <20260130102218.1324-1-im.lechain@gmail.com> References: <20260130102218.1324-1-im.lechain@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Cheng Li Currently, __nolibc_printf() in nolibc parses the width field but always pads with spaces on the left. It ignores the '-' flag (left alignment). This patch implements support for the '-' flag to forces left alignment by padding spaces on the right. Logic behavior: - "%5d" -> " -5" (unchanged, right align) - "%-5d" -> "-5 " (new, left align) Suggested-by: David Laight Suggested-by: Willy Tarreau Signed-off-by: Cheng Li Reviewed-by: David Laight --- v4 changes: - Adopted optimization suggestions from David Laight v3 changes: - Removed pad zeros support because of bug for signed number and pointer v2 changes: - Adopted optimization suggestions from Willy Tarreau: - Incremented 'written' counter at the start of loops. - Reordered loop checks to optimize compiler register usage. - Updated commit message to explicitly mention zero-padding ('0') support. --- tools/include/nolibc/stdio.h | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h index 1f16dab2ac88..1197928a1364 100644 --- a/tools/include/nolibc/stdio.h +++ b/tools/include/nolibc/stdio.h @@ -250,7 +250,7 @@ typedef int (*__nolibc_printf_cb)(intptr_t state, const= char *buf, size_t size); static __attribute__((unused, format(printf, 4, 0))) int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const= char *fmt, va_list args) { - char escape, lpref, c; + char escape, lpref, padc, c; unsigned long long v; unsigned int written, width; size_t len, ofs, w; @@ -261,11 +261,17 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t s= tate, size_t n, const char while (1) { c =3D fmt[ofs++]; width =3D 0; + padc =3D ' '; =20 if (escape) { /* we're in an escape sequence, ofs =3D=3D 1 */ escape =3D 0; =20 + if (c =3D=3D '-') { + padc =3D c; + c =3D fmt[ofs++]; + } + /* width */ while (c >=3D '0' && c <=3D '9') { width *=3D 10; @@ -358,12 +364,15 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t s= tate, size_t n, const char if (n) { w =3D len < n ? len : n; n -=3D w; - while (width-- > w) { + if (padc =3D=3D '-' && cb(state, outstr, w) !=3D 0) + return -1; + while (width > w) { + written++; if (cb(state, " ", 1) !=3D 0) return -1; - written +=3D 1; + width--; } - if (cb(state, outstr, w) !=3D 0) + if (padc !=3D '-' && cb(state, outstr, w) !=3D 0) return -1; } =20 --=20 2.52.0 From nobody Sat Feb 7 15:12:26 2026 Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8A96A35CBA5 for ; Fri, 30 Jan 2026 10:22:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769768553; cv=none; b=DRomrspyIeo1sesLj77M1icQ8P3CDo42MQbvQ1L83CRimCNRMCX9Nef9k8yYN5K3O/0gaRVPhnUr1xOIgp6kUgK6k1HCChX/tCHapA1hos0Hkmmkk9nqqfViZm7Zxft3izSKhw39CrA/uAGDo7Lbdvhtxc2MkGd4Shh9mFLhrRY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769768553; c=relaxed/simple; bh=AcF6Fg3Ymnat8HxxAPStYCzOrYY1ptvmxT8L9V98YkA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=l6PG/tmel8cU/ZEfNU+iQXvhi+4XCUSFaVsa5UkBE25TTe+jv46PaBqS27EUbnin0fp9CuazpsTh18TLIB4VgzU4BdB/2SJwy9X6WuQ/481dMOswFn6LAODfWQ4Lma1DahzbhssiHvas0qeu84MGn7hc04sjmH4bKEe2XR4tMvs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=gf27TVbW; arc=none smtp.client-ip=209.85.210.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="gf27TVbW" Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-81f4c0e2b42so1093994b3a.1 for ; Fri, 30 Jan 2026 02:22:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769768550; x=1770373350; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=+53aZ4Nj1yaeOA5unftPISR2gWN0w2O7aStXoE4adO8=; b=gf27TVbWWMObgFoU3WQURC1bxsuLlfaNmh1HQ7lC4OINP2Lmce+/CpRln31io3akqq FLBMiTS4n03nz7fbkrsCcytpj+0BEmfqoPMgUFuoZooiAIz7K72HgmP15kpO9xYjWYsX 1fO3137q+8gcZgaX/x/K2MadNbrJ4TpKAS0FCdA4sMvaKb9d2Hu0TsP4U5GjwtGFY4Cj pmrxNFOeOTW4MlHIhPiaS+f8LhJrIsg4Nl31VRfuuLYkgyuTFWzwCxpq7Oe0Z9oezKvA PhAry4qehjfoPyNNPATOrlgGayuba5h1IMRZ2J6B++/NlRLx54OEghjRcOyMwriImxdH heAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769768550; x=1770373350; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=+53aZ4Nj1yaeOA5unftPISR2gWN0w2O7aStXoE4adO8=; b=X/gFr2pmSv5d5jo5eEu7xOH82CbhLn4KzhRUQWVYhPsqVGz6CaMXUj6fW8vF/OFCFe I8X5LPIddXCCbTtxi1CryX/S6ev3+Kq8bv6XqzXUGP4jBM1qdu9/uoErN1A+XZukn4tt 8Us1uzC7gKVPw+vbUK5t8APZFh7iKtzpq77IGy+BefqMKFEReR12gjK8Mnqg2az2zde+ B8hDNPf3i/4rPVNovraVAOlyu8EEE4z+APQYPxlQ0LcBT57x1E8p0VD1mF2uS09DF2Gf ofwULfnEF8A0YC/WF9+/DrM5vPUqWTFCrvoRnwWeLGbb69Sm+oXJeppovd1vXQNTgyuK ZYgg== X-Forwarded-Encrypted: i=1; AJvYcCWlfjeopgeUOF0QFxbg1mAvxIPve7iQCIrS9CUdHAaatfHsKz6vQ2YnnVM/dXQtuDvZn4Ps9YByqLYDSVY=@vger.kernel.org X-Gm-Message-State: AOJu0YzplJDwBDOYxysQ90+O/BAkVzLg6Ghb8eT8Ed8zaPE7vS85v4Ox xnHEnC59A6vR8NDdZvjbTf8aOsk0gWRP5pBzneV4yeRXPHasxhDJ5CUC X-Gm-Gg: AZuq6aLCD5BbfbVYeBT1Kt0YZKxt2EInYi9zpHTq2OZVd0j4qRtOjTnE/YsfJhGAMni qNWVUL0RjbM38RsvDExpRTHt7alttKQKhr3j5tztGvOHnqjuZHJHi24SJ+2DH2GnOXzLaZHUYhQ AX0UXqGTjnguvO3BUhX9JGBFrdbf5VBm46ufD2LKexGy9kigW130tnFAERASXJDjdLOn+knNbxm mFFcAGzPtAacW2aDAF9eFl6iacm0j/P0PyfEb4k8dAm7zuLrIzJv7kzzyscNJOBDtX1EDlnRDkg 0Ge6yOGWuh7jlmtf6BmZrDmXRVSbpAwKbpR2Uw1GRLs5TLjX/ZrRsKX74hRKHgyLLT3G76Ui18T RaWPAEvOaljkaYtKyEm4LZqpRzpNMIHNfoKLHRtvJ0LNfRP4ZVMPkClLEsHWFMA24FP3hYnr0xF tiSrERJhV7qpa/8XRb4pTq1LFPL/hh83KxPSJ33EBw9eiCC9QbH32Gyy6rVLstT0C0vc98dbG6P vn8DwRsI3WQCLn0Ldl99Q== X-Received: by 2002:a05:6a20:d524:b0:38b:de3d:d530 with SMTP id adf61e73a8af0-392e016dd8dmr2305227637.72.1769768549787; Fri, 30 Jan 2026 02:22:29 -0800 (PST) Received: from LAPTOP-UKI113GK ([119.123.175.125]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-c6427da8441sm6647206a12.9.2026.01.30.02.22.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Jan 2026 02:22:29 -0800 (PST) From: "licheng.li" To: Willy Tarreau , David Laight Cc: =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , linux-kernel@vger.kernel.org, im.lechain@gmail.com Subject: [PATCH v4 2/2] selftests/nolibc: add tests for printf left alignment and zero padding Date: Fri, 30 Jan 2026 18:22:18 +0800 Message-ID: <20260130102218.1324-3-im.lechain@gmail.com> X-Mailer: git-send-email 2.51.0.windows.1 In-Reply-To: <20260130102218.1324-1-im.lechain@gmail.com> References: <20260130102218.1324-1-im.lechain@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Cheng Li This patch adds validation for the recently added left-alignment ('-') flag in printf(). It ensures that Fields are correctly padded with spaces on the right when the '-' flag is used. Signed-off-by: Cheng Li --- v3 changes: - Removed test case for zero padding that cannot work properly v2 changes: - Added test cases for zero padding (%08d) --- tools/testing/selftests/nolibc/nolibc-test.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/s= elftests/nolibc/nolibc-test.c index 3c5a226dad3a..c11fcf6c5075 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -1723,6 +1723,8 @@ static int run_printf(int min, int max) CASE_TEST(truncation); EXPECT_VFPRINTF(25, "01234567890123456789", "%s= ", "0123456789012345678901234"); break; CASE_TEST(string_width); EXPECT_VFPRINTF(10, " 1", "%10s", "1");= break; CASE_TEST(number_width); EXPECT_VFPRINTF(10, " 1", "%10d", 1); b= reak; + CASE_TEST(number_left); EXPECT_VFPRINTF(10, "|-5 |", "|%-8d|", -5)= ; break; + CASE_TEST(string_align); EXPECT_VFPRINTF(10, "|foo |", "|%-8s|", "fo= o"); break; CASE_TEST(width_trunc); EXPECT_VFPRINTF(25, " ", "%2= 5d", 1); break; CASE_TEST(scanf); EXPECT_ZR(1, test_scanf()); break; CASE_TEST(strerror); EXPECT_ZR(1, test_strerror()); break; --=20 2.52.0