From 4ed23c8f8557db0ed8578a090a0c483cd993c076 Mon Sep 17 00:00:00 2001 From: "H. Peter Anvin (Intel)" Date: Sun, 14 Jun 2020 19:55:49 -0700 Subject: 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) --- asm/preproc.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'asm') 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); } -- cgit v1.2.1