public inbox for dash@vger.kernel.org
 help / color / mirror / Atom feed
From: Herbert Xu <herbert@gondor.apana.org.au>
To: Harald van Dijk <harald@gigawatt.nl>
Cc: Nathan Mills <the.true.nathan.mills@gmail.com>,
	DASH Mailing List <dash@vger.kernel.org>
Subject: [PATCH] expand: Do not use memcmp on strings
Date: Sun, 14 Sep 2025 08:44:10 +0800	[thread overview]
Message-ID: <aMYP2pMS30mYUYdx@gondor.apana.org.au> (raw)
In-Reply-To: <ddd331fe-b0cb-4575-9849-90a811df32fa@gigawatt.nl>

On Sat, Sep 13, 2025 at 03:29:30PM +0100, Harald van Dijk wrote:
>
> Are you sure this is a false positive? memcmp isn't required to stop reading
> once it sees NUL and typical implementations read multiple bytes at a time.
> Reading past the NUL may crash.

You are right.  This patch should fix them up.

Thanks,

---8<---
Using memcmp past the end of a string may crash if it hits a page
boundary.  Fix this by calling strcmp/strncmp instead.

Reported-by: Nathan Mills <the.true.nathan.mills@gmail.com>
Reported-by: Harald van Dijk <harald@gigawatt.nl>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

diff --git a/src/expand.c b/src/expand.c
index 35481d6..19edc9d 100644
--- a/src/expand.c
+++ b/src/expand.c
@@ -354,8 +354,7 @@ start:
 			continue;
 		case CTLQUOTEMARK:
 			/* "$@" syntax adherence hack */
-			if (!inquotes && !memcmp(p, dolatstr + 1,
-						 DOLATSTRLEN - 1)) {
+			if (!inquotes && !strcmp(p, dolatstr + 1)) {
 				p = evalvar(p + 1, flag | EXP_QUOTED) + 1;
 				goto start;
 			}
@@ -1552,7 +1551,7 @@ expandmeta(struct strlist *str)
 
 		if (fflag)
 			goto nometa;
-		if (!strpbrk(str->text, "*?]") || !memcmp(str->text, "]", 2))
+		if (!strpbrk(str->text, "*?]") || !strcmp(str->text, "]"))
 			goto nometa;
 		savelastp = exparg.lastp;
 
@@ -1996,7 +1995,7 @@ static int pmatch(char *pattern, const char *string)
 					    chr >= c && chr <= *p)
 						found = 1;
 					p++;
-				} else if (!memcmp(mbs, q, mb))
+				} else if (!strncmp(mbs, q, mb))
 					found = 1;
 			} while ((c = *p++) != ']');
 			if (found == invert)
@@ -2011,7 +2010,7 @@ static int pmatch(char *pattern, const char *string)
 			q += mb & 0xff;
 			mb >>= 8;
 
-			if (memcmp(p - 1, q - 1, mb + 1))
+			if (strncmp(p - 1, q - 1, mb + 1))
 				return 0;
 
 			p += mb;
-- 
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

  reply	other threads:[~2025-09-14  0:44 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CALjJ9ccRuGV9fmydBvwzOua_om7QVoro-nSA-pv+9-nX1KLfGA@mail.gmail.com>
2025-09-13  9:59 ` [PATCH] parser: Skip parsing of old-style command substitution here-doc marker Herbert Xu
2025-09-13 13:18 ` [PATCH] expand: Check for NUL before parsing range expression in pmatch Herbert Xu
2025-09-13 13:34 ` I found multiple crash bugs in Dash v0.5.12-115-gb4ef25d Herbert Xu
2025-09-13 13:45 ` [PATCH] parser: Fix USTPUTC overflow for new-style command substition here-doc marker Herbert Xu
2025-09-13 14:06 ` I found multiple crash bugs in Dash v0.5.12-115-gb4ef25d Herbert Xu
2025-09-13 14:29   ` Harald van Dijk
2025-09-14  0:44     ` Herbert Xu [this message]
2025-09-14  0:51 ` Herbert Xu

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=aMYP2pMS30mYUYdx@gondor.apana.org.au \
    --to=herbert@gondor.apana.org.au \
    --cc=dash@vger.kernel.org \
    --cc=harald@gigawatt.nl \
    --cc=the.true.nathan.mills@gmail.com \
    /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