[PATCH] Protect strcat from accessing an unaligend long pointer

Alexey Lapshin <[email protected]>
Newsgroups gmane.comp.lib.newlib
Message-ID <[email protected]>
- 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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.