Skip to contentSkip to conversation

unicode: don't write -1 after NULL terminator

PATCH Superseded Jason A. Donenfeld 3 participants 5 messages Nov 3, 2022 – Nov 7, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 unicode: don't write -1 after NULL terminator

If the intention is to overwrite the first NULL with a -1, s[strlen(s)] is the first NULL, not s[strlen(s)+1].

---
 fs/unicode/mkutf8data.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/unicode/mkutf8data.c +1 -1
index bc1a7c8b5c8d..61800e0d3226 100644
@@ -3194,7 +3194,7 @@ static int normalize_line(struct tree *tree)
 	/* Second test: length-limited string. */
 	s = buf2;
 	/* Replace NUL with a value that will cause an error if seen. */
-	s[strlen(s) + 1] = -1;
+	s[strlen(s)] = -1;
 	t = buf3;
 	if (utf8cursor(&u8c, tree, s))
 		return -1;
Signature · 1 line
2.38.1
#1 lore raw
Quoted 2 lines · Jason A. Donenfeld

If the intention is to overwrite the first NULL with a -1, s[strlen(s)] is the first NULL, not s[strlen(s)+1].

This caught my attention. You mix NULL (void *) with NUL (\0) in the changelog & subject. That occurs rather confusing to me.

Quoted 20 lines
---
  fs/unicode/mkutf8data.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
fs/unicode/mkutf8data.c +1 -1
index bc1a7c8b5c8d..61800e0d3226 100644
@@ -3194,7 +3194,7 @@ static int normalize_line(struct tree *tree)
  	/* Second test: length-limited string. */
  	s = buf2;
  	/* Replace NUL with a value that will cause an error if seen. */
-	s[strlen(s) + 1] = -1;
+	s[strlen(s)] = -1;
  	t = buf3;
  	if (utf8cursor(&u8c, tree, s))
  		return -1;
Signature · 2 lines
js
suse labs
#2 lore raw

PATCH v2 1/1 unicode: don't write -1 after NUL terminator

If the intention is to overwrite the first NUL with a -1, s[strlen(s)] is the first NUL, not s[strlen(s)+1].

---
 fs/unicode/mkutf8data.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/unicode/mkutf8data.c +1 -1
index bc1a7c8b5c8d..61800e0d3226 100644
@@ -3194,7 +3194,7 @@ static int normalize_line(struct tree *tree)
 	/* Second test: length-limited string. */
 	s = buf2;
 	/* Replace NUL with a value that will cause an error if seen. */
-	s[strlen(s) + 1] = -1;
+	s[strlen(s)] = -1;
 	t = buf3;
 	if (utf8cursor(&u8c, tree, s))
 		return -1;
Signature · 1 line
2.38.1
#3 lore raw

Re: [PATCH v2] unicode: don't write -1 after NUL terminator

Quoted 2 lines · Jason A. Donenfeld

If the intention is to overwrite the first NUL with a -1, s[strlen(s)] is the first NUL, not s[strlen(s)+1].

Hi Jason,

This code is part of the verification of the trie that done at the end of utf8data generation. It is making sure the tree is not corrupted, by ensuring that utf8byte doesn't see something past the correct end of the string (the first NULL byte). Note it is not a bad memory access either, since we guarantee to have allocated enough space.

So I think the code is correct as is. if you apply your patch and regenerate utf8data.h_shipped, utf8byte will reach that -1 and fail the verification.

Quoted 20 lines
---
 fs/unicode/mkutf8data.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/unicode/mkutf8data.c +1 -1
index bc1a7c8b5c8d..61800e0d3226 100644
@@ -3194,7 +3194,7 @@ static int normalize_line(struct tree *tree)
 	/* Second test: length-limited string. */
 	s = buf2;
 	/* Replace NUL with a value that will cause an error if seen. */
-	s[strlen(s) + 1] = -1;
+	s[strlen(s)] = -1;
 	t = buf3;
 	if (utf8cursor(&u8c, tree, s))
 		return -1;
Signature · 1 line
Gabriel Krisman Bertazi
#4 lore raw

Re: [PATCH v2] unicode: don't write -1 after NUL terminator

Hi Gabriel,

Quoted 13 lines · Gabriel Krisman Bertazi
Quoted 2 lines · Jason A. Donenfeld

If the intention is to overwrite the first NUL with a -1, s[strlen(s)] is the first NUL, not s[strlen(s)+1].

Hi Jason,

This code is part of the verification of the trie that done at the end of utf8data generation. It is making sure the tree is not corrupted, by ensuring that utf8byte doesn't see something past the correct end of the string (the first NULL byte). Note it is not a bad memory access either, since we guarantee to have allocated enough space.

Quoted 3 lines

So I think the code is correct as is. if you apply your patch and regenerate utf8data.h_shipped, utf8byte will reach that -1 and fail the verification.

Ah, okay. "Replace NUL" would seem to be wrong/confusing comment text I suppose. Thanks for the explanation anyhow, and sorry for the noise.

Jason

Quoted 24 lines
Quoted 21 lines
---
 fs/unicode/mkutf8data.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/unicode/mkutf8data.c +1 -1
index bc1a7c8b5c8d..61800e0d3226 100644
@@ -3194,7 +3194,7 @@ static int normalize_line(struct tree *tree)
 	/* Second test: length-limited string. */
 	s = buf2;
 	/* Replace NUL with a value that will cause an error if seen. */
-	s[strlen(s) + 1] = -1;
+	s[strlen(s)] = -1;
 	t = buf3;
 	if (utf8cursor(&u8c, tree, s))
 		return -1;
Signature · 1 line
Gabriel Krisman Bertazi