[nasm:nasm-2.15.xx] preproc.c: make extra sure we always have a null-terminated token

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


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

preproc.c: make extra sure we always have a null-terminated token

tok_set_text() and tok_set_text_free() take a length argument, which
could at least theoretically mean that we don't have a null-terminated
string. Directly enforce a null-terminated string in all cases.

In the future this means that it is legal to intentionally use these
functions to tokenize a substring.

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


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

diff --git a/asm/preproc.c b/asm/preproc.c
index 663e066b..fd06ba89 100644
--- a/asm/preproc.c
+++ b/asm/preproc.c
@@ -365,18 +365,24 @@ static size_t tok_strlen(const char *str)
  */
 static Token *set_text(struct Token *t, const char *text, size_t len)
 {
-    char *textp;
-
     if (t->len > INLINE_TEXT)
 	nasm_free(t->text.p.ptr);
 
     nasm_zero(t->text);
 
     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);
-    textp[len] = '\0';
+    if (len > INLINE_TEXT) {
+        char *textp;
+
+        t->text.p.ptr = textp = nasm_malloc(len+1);
+        memcpy(textp, text, len);
+        textp[len] = '\0';
+    } else {
+        /* Null-terminated due to nasm_zero() above */
+        t->len = len;
+	memcpy(t->text.a, text, len);
+    }
+
     return t;
 }
 
@@ -396,8 +402,8 @@ static Token *set_text_free(struct Token *t, char *text, size_t len)
 	t->text.p.ptr = text;
         text[len] = '\0';
     } else {
+        /* Null-terminated due to nasm_zero() above */
 	memcpy(t->text.a, text, len);
-        t->text.a[len] = '\0';
 	nasm_free(text);
     }
 


More information about the Nasm-commits mailing list