From 0bedfa70c597f83078e1a339df1117900e5afc7a Mon Sep 17 00:00:00 2001 From: tocariimaa Date: Mon, 13 Jan 2025 14:34:25 -0300 Subject: [PATCH] convert `Sb` macro to a proper function ...this way we can assert that the provided buffer is actually null-terminated. `Sb()` -> `Str_from_buf()`. --- compiler/pre.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/compiler/pre.h b/compiler/pre.h index 743f2ca..19b19c7 100644 --- a/compiler/pre.h +++ b/compiler/pre.h @@ -86,8 +86,6 @@ typedef Slice(u8) Str; /* Creates a `Str` from a string literal */ #define Sl(s) ((Str){ (u8 *)s, (isize)lengthof(s) }) -/* Creates a `Str` from a buffer of size `len` */ -#define Sb(s, len) ((Str){ (u8 *)s, (isize)len }) /* Creates a `Str` from a C string. */ #define Str_from_c(s) ((Str){ (u8 *)s, (isize)(s != nil ? strlen(s) : 0) }) #define Str_empty(s) ((s).len == 0) @@ -96,6 +94,16 @@ typedef Slice(u8) Str; int vsnprintf(char *, unsigned long, const char *, va_list); +/* Creates a `Str` from a buffer of size `len` */ +static inline Str +Str_from_buf(u8 *buf, isize len) +{ + /* If this triggers an OOB, its because the code that created the string was + * broken anyways. ASan will catch it. */ + Assert(buf[len] == '\0'); + return (Str){ buf, len }; +} + /* "Converts" a `Str` into a C string. Since `Str` are meant to be * null-terminated already, no conversion is made, but ensures that the * null terminator is present. */