From 18ce6e98590635d9127dbe32464988d524f2a6ea Mon Sep 17 00:00:00 2001 From: "Steffen (Daode) Nurpmeso" Date: Fri, 16 Sep 2016 13:57:52 +0200 Subject: [PATCH] n_string_resize(): change semantics --- nailfuns.h | 7 ++++--- strings.c | 12 ++++++------ 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/nailfuns.h b/nailfuns.h index f56c26cf..46e14c27 100644 --- a/nailfuns.h +++ b/nailfuns.h @@ -2033,7 +2033,7 @@ FL struct str * n_str_add_buf(struct str *self, char const *buf, uiz_t buflen /* Truncate to size, which must be LE current length */ #define n_string_trunc(S,L) \ - (assert(UICMP(z, L, <=, (S)->s_len)), (S)->s_len = (ui32_t)(L), (S)) + (assert(UICMP(z, L, <=, (S)->s_len)), (S)->s_len = (ui32_t)(L), (S)) /* Release buffer ownership */ #define n_string_drop_ownership(S) \ @@ -2049,10 +2049,11 @@ FL struct n_string *n_string_clear(struct n_string *self SMALLOC_DEBUG_ARGS); # define n_string_clear(S) ((S)->s_size != 0 ? (n_string_clear)(S) : (S)) #endif -/* Reserve room for noof additional bytes. The latter also adjusts the length - * accordingly, but the (possible) additional storage isn't initialized */ +/* Reserve room for noof additional bytes, but don't adjust length (yet) */ FL struct n_string *n_string_reserve(struct n_string *self, size_t noof SMALLOC_DEBUG_ARGS); + +/* Resize to exactly nlen bytes; any new storage isn't initialized */ FL struct n_string *n_string_resize(struct n_string *self, size_t nlen SMALLOC_DEBUG_ARGS); diff --git a/strings.c b/strings.c index 603de0a9..84c48ce3 100644 --- a/strings.c +++ b/strings.c @@ -647,17 +647,17 @@ FL struct n_string * FL struct n_string * (n_string_resize)(struct n_string *self, size_t nlen SMALLOC_DEBUG_ARGS){ - ui32_t l; NYD_ENTER; assert(self != NULL); +#if 0 /* FIXME memory alloc too large */ + if(SI32_MAX - n_ALIGN(1) - l <= noof) + n_panic(_("Memory allocation too large")); +#endif - if((l = self->s_len) > nlen) - self->s_len = nlen; - else{ - nlen -= l; + if(self->s_len < nlen) self = (n_string_reserve)(self, nlen SMALLOC_DEBUG_ARGSCALL); - } + self->s_len = (ui32_t)nlen; NYD_LEAVE; return self; } -- 2.11.4.GIT