Commit: ec546ff7b1b37b59695ab3217399cb9f4475405f
Parent: 753429c84ef37eed2a792e43c5110f0d356365fb
Author: Randy Palamar
Date: Sun, 9 Mar 2025 07:35:03 -0600
make sure statusline buffer can't be overflowed
Diffstat:
1 file changed, 14 insertions(+), 9 deletions(-)
diff --git a/status.c b/status.c
@@ -223,16 +223,22 @@ stream_ensure_c_str(Stream *s)
}
static void
-stream_push_s8(Stream *s, s8 str)
+stream_push(Stream *s, void *data, size len)
{
- s->errors |= s->capacity <= (s->write_index + str.len);
+ s->errors |= s->capacity <= (s->write_index + len);
if (!s->errors) {
- memcpy(s->buffer + s->write_index, str.data, str.len);
- s->write_index += str.len;
+ memcpy(s->buffer + s->write_index, data, len);
+ s->write_index += len;
}
}
static void
+stream_push_s8(Stream *s, s8 str)
+{
+ stream_push(s, str.data, str.len);
+}
+
+static void
add_file_watch(Arena *a, char *path, i32 block_index, block_update_fn *update_fn)
{
i32 wd = inotify_add_watch(file_watches.wd, path, IN_CLOSE_WRITE|IN_MODIFY);
@@ -315,15 +321,14 @@ update_status(void)
for (; block_index < ARRAY_COUNT(blocks); block_index++) {
Block *b = blocks + block_index;
- memcpy(statusline.buffer + statusline.write_index, b->data, b->len);
- statusline.write_index += b->len;
+ stream_push(&statusline, b->data, b->len);
}
- statusline.buffer[statusline.write_index] = 0;
if (dflag) {
- puts((char *)statusline.buffer);
+ puts(stream_ensure_c_str(&statusline));
} else {
- XStoreName(display, DefaultRootWindow(display), (char *)statusline.buffer);
+ XStoreName(display, DefaultRootWindow(display),
+ stream_ensure_c_str(&statusline));
XSync(display, 0);
}
}