coreutils/backport-pinky-fix-string-size-calculation.patch
h30032433 80b999f39c sync patches from community
(cherry picked from commit 0c9adb86cb84d4f086e9660601e48e84d9a2dc48)
2024-09-12 09:24:29 +08:00

67 lines
1.9 KiB
Diff

From 3e0d7787e67d4f732298d99eee772fc2631ddfb8 Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Sat, 11 Nov 2023 00:17:11 -0800
Subject: [PATCH] pinky: fix string size calculation
* src/pinky.c (count_ampersands): Simplify and return idx_t.
(create_fullname): Compute proper destination string size,
basically, by adding (ulen - 1) * ampersands rather than ulen *
(ampersands - 1). Problem found on CHERI-64.
Reference:https://github.com/coreutils/coreutils/commit/3e0d7787e67d4f732298d99eee772fc2631ddfb8
Conflict:Adapt to context. Adapt to INT_MULTIPLY_WRAPV.
---
src/pinky.c | 21 +++++++++------------
1 file changed, 9 insertions(+), 12 deletions(-)
diff --git a/src/pinky.c b/src/pinky.c
index 6fea949..501a5b7 100644
--- a/src/pinky.c
+++ b/src/pinky.c
@@ -81,15 +81,12 @@ static struct option const longopts[] =
/* Count and return the number of ampersands in STR. */
-static size_t _GL_ATTRIBUTE_PURE
+static idx_t _GL_ATTRIBUTE_PURE
count_ampersands (char const *str)
{
- size_t count = 0;
- do
- {
- if (*str == '&')
- count++;
- } while (*str++);
+ idx_t count = 0;
+ for (; *str; str++)
+ count += *str == '&';
return count;
}
@@ -102,16 +99,16 @@ count_ampersands (char const *str)
static char *
create_fullname (char const *gecos_name, char const *user_name)
{
- size_t rsize = strlen (gecos_name) + 1;
+ idx_t rsize = strlen (gecos_name) + 1;
char *result;
char *r;
- size_t ampersands = count_ampersands (gecos_name);
+ idx_t ampersands = count_ampersands (gecos_name);
if (ampersands != 0)
{
- size_t ulen = strlen (user_name);
- size_t product;
- if (INT_MULTIPLY_WRAPV (ulen, ampersands - 1, &product)
+ idx_t ulen = strlen (user_name);
+ ptrdiff_t product;
+ if (INT_MULTIPLY_WRAPV (ulen - 1, ampersands, &product)
|| INT_ADD_WRAPV (rsize, product, &rsize))
xalloc_die ();
}
--
2.33.0