From 1c21a53e4ed03371df5d9f16359545862bb4820e Mon Sep 17 00:00:00 2001 From: "H. Peter Anvin (Intel)" Date: Fri, 9 Aug 2019 02:34:21 -0700 Subject: preproc: fix parsing of single-line macro arguments, cleanups The single-line macro argument parsing was completely broken as a comma would not be recognized as an argument separator. In the process of fixing this, make a fair bit of code cleanups. Note: reverse tokens for smacro->expansion doesn't actually make any sense anymore, might reconsider that. This checkin also removes the distinction between "magic" and plain smacros; the only difference is which specific expand method is being invoked. Finally, extend the allocating-string functions such that *all* the allocating string functions support querying the length of the string a posteori. Signed-off-by: H. Peter Anvin (Intel) --- nasmlib/alloc.c | 18 +++++++++++++----- nasmlib/alloc.h | 2 ++ nasmlib/asprintf.c | 14 ++++++++------ nasmlib/strlist.c | 2 +- 4 files changed, 24 insertions(+), 12 deletions(-) (limited to 'nasmlib') diff --git a/nasmlib/alloc.c b/nasmlib/alloc.c index ad2cff3d..4e0ff9fe 100644 --- a/nasmlib/alloc.c +++ b/nasmlib/alloc.c @@ -40,6 +40,8 @@ #include "error.h" #include "alloc.h" +size_t _nasm_last_string_size; + no_return nasm_alloc_failed(void) { /* If nasm_fatal() gets us back here, then croak hard */ @@ -90,8 +92,9 @@ void nasm_free(void *q) char *nasm_strdup(const char *s) { char *p; - size_t size = strlen(s) + 1; + const size_t size = strlen(s) + 1; + _nasm_last_string_size = size; p = nasm_malloc(size); return memcpy(p, s, size); } @@ -101,6 +104,7 @@ char *nasm_strndup(const char *s, size_t len) char *p; len = strnlen(s, len); + _nasm_last_string_size = len + 1; p = nasm_malloc(len+1); p[len] = '\0'; return memcpy(p, s, len); @@ -109,11 +113,13 @@ char *nasm_strndup(const char *s, size_t len) char *nasm_strcat(const char *one, const char *two) { char *rslt; - size_t l1 = strlen(one); - size_t l2 = strlen(two); - rslt = nasm_malloc(l1 + l2 + 1); + const size_t l1 = strlen(one); + const size_t s2 = strlen(two) + 1; + + _nasm_last_string_size = l1 + s2; + rslt = nasm_malloc(l1 + s2); memcpy(rslt, one, l1); - memcpy(rslt + l1, two, l2+1); + memcpy(rslt + l1, two, s2); return rslt; } @@ -150,6 +156,8 @@ char *nasm_strcatn(const char *str1, ...) } va_end(ap); + _nasm_last_string_size = s; + q = rslt = nasm_malloc(s); p = str1; diff --git a/nasmlib/alloc.h b/nasmlib/alloc.h index c599d213..1b896585 100644 --- a/nasmlib/alloc.h +++ b/nasmlib/alloc.h @@ -45,4 +45,6 @@ static inline void *validate_ptr(void *p) return p; } +extern size_t _nasm_last_string_size; + #endif /* NASMLIB_ALLOC_H */ diff --git a/nasmlib/asprintf.c b/nasmlib/asprintf.c index be88d491..0b8e49d8 100644 --- a/nasmlib/asprintf.c +++ b/nasmlib/asprintf.c @@ -38,16 +38,15 @@ /* * nasm_[v]asprintf() are variants of the semi-standard [v]asprintf() * functions, except that we return the pointer instead of a count. - * The size of the string (including the final NUL!) is available - * by calling nasm_aprintf_size() afterwards. + * The length of the string (with or without the final NUL) is available + * by calling nasm_last_string_{len,size}() afterwards. * * nasm_[v]axprintf() are similar, but allocates a user-defined amount * of storage before the string, and returns a pointer to the - * allocated buffer. + * allocated buffer. The size of that area is not included in the value + * returned by nasm_last_string_size(). */ -size_t _nasm_aprintf_size; - void *nasm_vaxprintf(size_t extra, const char *fmt, va_list ap) { char *strp; @@ -55,9 +54,12 @@ void *nasm_vaxprintf(size_t extra, const char *fmt, va_list ap) size_t bytes; va_copy(xap, ap); - _nasm_aprintf_size = bytes = vsnprintf(NULL, 0, fmt, xap) + 1; + bytes = vsnprintf(NULL, 0, fmt, xap) + 1; + _nasm_last_string_size = bytes; va_end(xap); + strp = nasm_malloc(extra+bytes); + memset(strp, 0, extra); vsnprintf(strp+extra, bytes, fmt, ap); return strp; } diff --git a/nasmlib/strlist.c b/nasmlib/strlist.c index a0687cce..db5a09ab 100644 --- a/nasmlib/strlist.c +++ b/nasmlib/strlist.c @@ -109,7 +109,7 @@ strlist_vprintf(struct strlist *list, const char *fmt, va_list ap) return NULL; e = nasm_vaxprintf(offsetin(*e, str), fmt, ap); - e->size = nasm_aprintf_size(); + e->size = nasm_last_string_size(); if (list->uniq) { void **dp = hash_findb(&list->hash, e->str, e->size, &hi); -- cgit v1.2.1