From bba4427b5dfba7f9cdab8fa2cac1399fceda3058 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Mon, 5 Aug 2024 01:30:44 -0700 Subject: [PATCH] Fix unlikely pointer overflow in abspath * src/function.c (abspath): len is now ptrdiff_t, to avoid GCC warning about comparing signed to unsigned. It really is a pointer difference, after all. Rejigger comparision to avoid undefined behavior if dest + len is an invalid pointer. --- src/function.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/function.c b/src/function.c index 133e33a3..b4c38052 100644 --- a/src/function.c +++ b/src/function.c @@ -2119,7 +2119,7 @@ abspath (const char *name, char *apath) for (start = end = name; *start != '\0'; start = end) { - size_t len; + ptrdiff_t len; /* Skip sequence of multiple path-separators. */ while (ISDIRSEP (*start)) @@ -2147,7 +2147,7 @@ abspath (const char *name, char *apath) if (! ISDIRSEP (dest[-1])) *dest++ = '/'; - if (dest + len >= apath_limit) + if (apath_limit - dest <= len) return NULL; dest = mempcpy (dest, start, len); -- 2.47.3