fix: replace unsafe fixed-size buffers with dynamic formatting helpers; add util format helpers; centralize log_on_line cleanup

This commit is contained in:
2026-04-26 22:39:55 +02:00
parent 70998643fb
commit 05dfb3725b
9 changed files with 156 additions and 45 deletions
+14 -9
View File
@@ -1,6 +1,7 @@
#include "test.h"
#include "token.h"
#include <string.h>
#include <stdlib.h>
static void test_tokenstream_open_fail(void) {
TokenStream* ts = tokenstream_open(NULL, NULL);
@@ -97,22 +98,26 @@ static void test_tokenstream_info(void) {
Token t1 = tokenstream_next(ts);
if (t1.token != TOKEN_MODULE) fail("expected TOKEN_MODULE");
char buf1[32];
memcpy(buf1, t1.text.data, t1.text.length);
buf1[t1.text.length] = '\0';
assert_str("module", buf1, "info: expected 'module'");
char* buf1 = malloc((size_t)t1.text.length + 1);
if (!buf1) fail("out of memory");
memcpy(buf1, t1.text.data, t1.text.length);
buf1[t1.text.length] = '\0';
assert_str("module", buf1, "info: expected 'module'");
if (t1.location.line != 1) fail("expected line 1");
if (t1.location.column_start != 1) fail("expected column 1");
Token t2 = tokenstream_next(ts);
if (t2.token != TOKEN_IDENTIFIER) fail("expected TOKEN_IDENTIFIER");
char buf2[32];
memcpy(buf2, t2.text.data, t2.text.length);
buf2[t2.text.length] = '\0';
assert_str("main", buf2, "info: expected 'main'");
char* buf2 = malloc((size_t)t2.text.length + 1);
if (!buf2) { free(buf1); fail("out of memory"); }
memcpy(buf2, t2.text.data, t2.text.length);
buf2[t2.text.length] = '\0';
assert_str("main", buf2, "info: expected 'main'");
if (t2.location.line != 1) fail("expected line 1");
if (t2.location.column_start != 8) fail("expected column 8");
tokenstream_close(ts);
free(buf1);
free(buf2);
tokenstream_close(ts);
}