For these two buffers we can be a stricter and use the count instead
of the number allocated for COUNTED_BY. One just needs to make sure
the count is incremented first before a subsequent write.
While it would be better to do this in all cases, in some cases it
doesn't map cleanly. E.g., the base58 buffer in 'basenc' has an
implicit NUL byte appended the count.
-- 8< --
* src/join.c (struct line): Change the COUNTED_BY to use the nfields
member instead of nfields_allocated.
(extract_field): Increment the count before writing to the last field,
otherwise -fsanitize=bounds would kill the program.
---
src/join.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/src/join.c b/src/join.c
index 999f248c8..e2b4e5cac 100644
--- a/src/join.c
+++ b/src/join.c
@@ -74,7 +74,7 @@ struct line
struct linebuffer buf; /* The line itself. */
idx_t nfields; /* Number of elements in 'fields'. */
idx_t nfields_allocated; /* Number of elements allocated for 'fields'. */
- struct field *fields COUNTED_BY (nfields_allocated);
+ struct field *fields COUNTED_BY (nfields);
};
/* One or more consecutive lines read from a file that all have the
@@ -294,9 +294,9 @@ extract_field (struct line *line, char *field, idx_t len)
if (line->nfields >= line->nfields_allocated)
line->fields = xpalloc (line->fields, &line->nfields_allocated, 1,
-1, sizeof *line->fields);
- line->fields[line->nfields].beg = field;
- line->fields[line->nfields].len = len;
- ++(line->nfields);
+ idx_t const field_index = line->nfields++;
+ line->fields[field_index].beg = field;
+ line->fields[field_index].len = len;
}
static bool
--
2.56.0