[PATCH] Protect strcat from accessing an unaligend long pointer
Alexey Lapshin
alexey.lapshin@espressif.com
Wed Feb 12 03:14:47 GMT 2025
- related to Bug libc/32679
---
newlib/libc/string/strcat.c | 21 ++++++++++++---------
1 file changed, 12 insertions(+), 9 deletions(-)
diff --git a/newlib/libc/string/strcat.c b/newlib/libc/string/strcat.c
index 47c53a5d2..e213e2d91 100644
--- a/newlib/libc/string/strcat.c
+++ b/newlib/libc/string/strcat.c
@@ -54,15 +54,18 @@ strcat (char *__restrict s1,
while (UNALIGNED_X(s1) && *s1)
s1++;
- /* Skip over the aligned data in s1 as quickly as possible. */
- unsigned long *aligned_s1 = (unsigned long *)s1;
- while (!DETECT_NULL(*aligned_s1))
- aligned_s1++;
- s1 = (char *)aligned_s1;
-
- /* Find string terminator. */
- while (*s1)
- s1++;
+ if (*s1)
+ {
+ /* Skip over the aligned data in s1 as quickly as possible. */
+ unsigned long *aligned_s1 = (unsigned long *)s1;
+ while (!DETECT_NULL(*aligned_s1))
+ aligned_s1++;
+ s1 = (char *)aligned_s1;
+
+ /* Find string terminator. */
+ while (*s1)
+ s1++;
+ }
/* s1 now points to the its trailing null character, we can
just use strcpy to do the work for us now.
--
2.43.0
More information about the Newlib
mailing list