[nasm:nasm-2.15.xx] preproc.c: make extra sure tokens are always null-terminated

nasm-bot for H. Peter Anvin (Intel) hpa at zytor.com
Sun Jun 14 20:00:03 PDT 2020


Commit-ID:  00335e43ef59194152aa0e81e253e7ccdba29ff2
Gitweb:     http://repo.or.cz/w/nasm.git?a=commitdiff;h=00335e43ef59194152aa0e81e253e7ccdba29ff2
Author:     H. Peter Anvin (Intel) <hpa at zytor.com>
AuthorDate: Sun, 14 Jun 2020 19:49:19 -0700
Committer:  H. Peter Anvin (Intel) <hpa at zytor.com>
CommitDate: Sun, 14 Jun 2020 19:49:19 -0700

preproc.c: make extra sure tokens are always null-terminated

In tok_set_text() and tok_set_text_free(), don't trust that
the caller has given us a zero-terminated string.

Signed-off-by: H. Peter Anvin (Intel) <hpa at zytor.com>


---
 asm/preproc.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/asm/preproc.c b/asm/preproc.c
index 53136abd..663e066b 100644
--- a/asm/preproc.c
+++ b/asm/preproc.c
@@ -370,12 +370,13 @@ static Token *set_text(struct Token *t, const char *text, size_t len)
     if (t->len > INLINE_TEXT)
 	nasm_free(t->text.p.ptr);
 
-    nasm_zero(t->text.a);
+    nasm_zero(t->text);
 
-    t->len = tok_check_len(len);
+    t->len = len = tok_check_len(len);
     textp = (len > INLINE_TEXT)
 	? (t->text.p.ptr = nasm_malloc(len+1)) : t->text.a;
-    memcpy(textp, text, len+1);
+    memcpy(textp, text, len);
+    textp[len] = '\0';
     return t;
 }
 
@@ -383,18 +384,20 @@ static Token *set_text(struct Token *t, const char *text, size_t len)
  * Set the text field to the existing pre-allocated string, either
  * taking over or freeing the allocation in the process.
  */
-static Token *set_text_free(struct Token *t, char *text, unsigned int len)
+static Token *set_text_free(struct Token *t, char *text, size_t len)
 {
     if (t->len > INLINE_TEXT)
 	nasm_free(t->text.p.ptr);
 
-    nasm_zero(t->text.a);
+    nasm_zero(t->text);
 
-    t->len = tok_check_len(len);
+    t->len = len = tok_check_len(len);
     if (len > INLINE_TEXT) {
 	t->text.p.ptr = text;
+        text[len] = '\0';
     } else {
-	memcpy(t->text.a, text, len+1);
+	memcpy(t->text.a, text, len);
+        t->text.a[len] = '\0';
 	nasm_free(text);
     }
 


More information about the Nasm-commits mailing list