public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Andreas Schwab <schwab@suse.de>
To: libc-alpha@sourceware.org
Subject: [PATCH] power8: fix read past array bounds in strncasecmp (bug 34683)
Date: Tue, 29 Sep 2026 15:58:52 +0200	[thread overview]
Message-ID: <mvmbj9g6twj.fsf@suse.de> (raw)

Move the load of the characters to the top of the loops, so that they
occur after the bounds check for the strncasecmp implementation.  Amend
the page test for strncasecmp to also test with non-null-terminated arrays
that trigger this bug.
---
 string/test-strncasecmp.c                     | 16 ++++++++++++++++
 sysdeps/powerpc/powerpc64/power8/strcasecmp.S | 14 ++++++--------
 2 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/string/test-strncasecmp.c b/string/test-strncasecmp.c
index 6b00113e66..3fced4202b 100644
--- a/string/test-strncasecmp.c
+++ b/string/test-strncasecmp.c
@@ -199,6 +199,22 @@ do_page_tests (void)
 			  exp_result);
 	}
     }
+
+  /* Like strncmp, the arrays passed to strncasecmp are not required to be
+     null-terminated.  Check that the implementations do not read beyond
+     the end of the arrays.  */
+
+  s1[maxoffset - 1] = 'a';
+  s2[maxoffset - 1] = 'a';
+  for (size_t off1 = 0; off1 < maxoffset; off1++)
+    {
+      for (size_t off2 = 0; off2 < maxoffset; off2++)
+	{
+	  size_t off_max = off1 < off2 ? off2 : off1;
+	  FOR_EACH_IMPL (impl, 0)
+	    check_result (impl, s1 + off1, s2 + off2, maxoffset - off_max, 0);
+	}
+    }
 }
 
 static void
diff --git a/sysdeps/powerpc/powerpc64/power8/strcasecmp.S b/sysdeps/powerpc/powerpc64/power8/strcasecmp.S
index 3c45e9ea50..22db297855 100644
--- a/sysdeps/powerpc/powerpc64/power8/strcasecmp.S
+++ b/sysdeps/powerpc/powerpc64/power8/strcasecmp.S
@@ -355,8 +355,6 @@ L(bytebybyte):
 	offset and string descriptors are only updated in the end
 	of loop unrolling. */
 	ld	rLOC, LOCALE_CTYPE_TOLOWER(rLOC)
-	lbz	rCHAR1, 0(rSTR1)	/* Load char from s1 */
-	lbz	rCHAR2, 0(rSTR2)	/* Load char from s2 */
 #ifdef USE_AS_STRNCASECMP
 	rldicl	rTMP, r5, 62, 2
 	cmpdi	cr7, rTMP, 0
@@ -364,6 +362,8 @@ L(bytebybyte):
 	mtctr	rTMP
 #endif
 L(loop):
+	lbz	rCHAR1, 0(rSTR1)	/* Load char from s1 */
+	lbz	rCHAR2, 0(rSTR2)	/* Load char from s2 */
 	cmpdi	rCHAR1, 0		/* *s1 == '\0' ? */
 	sldi	rADDR1, rCHAR1, 2	/* Calculate address for tolower(*s1) */
 	sldi	rADDR2, rCHAR2, 2	/* Calculate address for tolower(*s2) */
@@ -404,13 +404,11 @@ L(loop):
 	lwzx	rLWR2, rLOC, rADDR2
 	cmpw	cr1, rLWR1, rLWR2
 	crorc	4*cr1+eq,eq,4*cr1+eq
-	beq     cr1, L(done)
-	lbz	rCHAR1, 0(rSTR1)	/* Load char from s1 */
-	lbz	rCHAR2, 0(rSTR2)	/* Load char from s2 */
 #ifdef USE_AS_STRNCASECMP
+	beq     cr1, L(done)
 	bdnz	L(loop)
 #else
-	b	L(loop)
+	bne     cr1, L(loop)
 #endif
 #ifdef USE_AS_STRNCASECMP
 L(lessthan4):
@@ -419,6 +417,8 @@ L(lessthan4):
 	beq	cr7, L(retnull)
 	mtctr	r5
 L(loop1):
+	lbz	rCHAR1, 0(rSTR1)
+	lbz	rCHAR2, 0(rSTR2)
 	cmpdi	rCHAR1, 0
 	sldi	rADDR1, rCHAR1, 2
 	sldi	rADDR2, rCHAR2, 2
@@ -429,8 +429,6 @@ L(loop1):
 	beq	cr1, L(done)
 	addi	rSTR1, rSTR1, 1
 	addi	rSTR2, rSTR2, 1
-	lbz	rCHAR1, 0(rSTR1)
-	lbz	rCHAR2, 0(rSTR2)
 	bdnz	L(loop1)
 #endif
 L(done):
-- 
2.55.0


-- 
Andreas Schwab, SUSE Labs, schwab@suse.de
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."

             reply	other threads:[~2026-09-29 13:59 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-29 13:58 Andreas Schwab [this message]
2026-09-29 14:05 ` Adhemerval Zanella Netto
2026-09-29 14:24   ` Andreas Schwab
2026-09-30 18:22     ` Sachin Monga

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=mvmbj9g6twj.fsf@suse.de \
    --to=schwab@suse.de \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).