From 60d4322f57b92f20089d494764fbc9f26eb49b04 Mon Sep 17 00:00:00 2001 From: Matthew Fernandez Date: Sun, 18 Jul 2021 17:09:59 -0700 Subject: [PATCH] scformat: use vmalloc instead of vmresize when processing '[' Though it is hard to see, the previous code bottomed out in a call to vmresize. After allocating, it zeroes the allocated region. So vmresize (itself calling realloc) signals an unnecessary constraint to the allocator that previous data in this memory must be preserved. To simplify this, this change converts the sequence to a vmfree of the prior memory and then a new vmalloc, more clearly divorcing the new buffer from the old one. --- lib/expr/exeval.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/expr/exeval.c b/lib/expr/exeval.c index eda92e258..0fa6e54f4 100644 --- a/lib/expr/exeval.c +++ b/lib/expr/exeval.c @@ -463,7 +463,7 @@ scformat(Sfio_t* sp, void* vp, Sffmt_t* dp) *(void**)vp = &node->data.variable.symbol->value->data.constant.value; break; case 's': - case '[': + case '[': { if (node->type != STRING) { exerror("scanf: %s: string variable address argument expected", node->data.variable.symbol->name); @@ -472,9 +472,14 @@ scformat(Sfio_t* sp, void* vp, Sffmt_t* dp) if (node->data.variable.symbol->value->data.constant.value.string == expr.nullstring) node->data.variable.symbol->value->data.constant.value.string = 0; fmt->fmt.size = 1024; - *(void**)vp = node->data.variable.symbol->value->data.constant.value.string = vmnewof(fmt->expr->vm, node->data.variable.symbol->value->data.constant.value.string, char, fmt->fmt.size, 0); - memset(node->data.variable.symbol->value->data.constant.value.string, 0, sizeof(char) * (size_t)fmt->fmt.size); + char *s = node->data.variable.symbol->value->data.constant.value.string; + vmfree(fmt->expr->vm, s); + s = vmalloc(fmt->expr->vm, sizeof(char) * (size_t)fmt->fmt.size); + memset(s, 0, sizeof(char) * (size_t)fmt->fmt.size); + *(void**)vp = s; + node->data.variable.symbol->value->data.constant.value.string = s; break; + } case 'c': if (node->type != CHARACTER) { exerror("scanf: %s: char variable address argument expected", node->data.variable.symbol->name); -- 2.40.0