diff options
author | H. Peter Anvin (Intel) <hpa@zytor.com> | 2020-06-14 19:55:49 -0700 |
---|---|---|
committer | H. Peter Anvin (Intel) <hpa@zytor.com> | 2020-06-14 19:55:49 -0700 |
commit | 4ed23c8f8557db0ed8578a090a0c483cd993c076 (patch) | |
tree | 7a37edd999db8c4bd64374d3a81123bb0df10372 | |
parent | 00335e43ef59194152aa0e81e253e7ccdba29ff2 (diff) | |
download | nasm-4ed23c8f8557db0ed8578a090a0c483cd993c076.tar.gz |
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@zytor.com>
-rw-r--r-- | asm/preproc.c | 20 |
1 files 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); } |