From 3c5db5339454f48edb48c9b1498c1832c6711eb1 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Mon, 26 Jun 2023 17:19:35 -0700 Subject: til_str: exclude \0 from res_len for til_str_buf()/til_str_to_buf() It's more ergonomic more often to behave consistently with strlen() here, plus it's just the established mental model. While here I made til_settings_path_as_buf() private as nothing external uses it, it's essentially just a logically distinct private helper function from the public wrappers around it. Dragged into this changeset due to clarifying some naming/semantics as it's one of the few til_str_to_buf() callers. But nobody was actually passing a non-NULL res_bufsz/res_len to it anyways, as its use is minimal. --- src/til_str.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/til_str.c') diff --git a/src/til_str.c b/src/til_str.c index fa256d7..6b85556 100644 --- a/src/til_str.c +++ b/src/til_str.c @@ -152,13 +152,14 @@ char * til_str_strdup(const til_str_t *str) /* a valid \0-terminated string is _always_ maintained @ str->buf so callers can just use it as a string.. * but must not hang onto that pointer across more til_str() calls on the same str. + * The length (excluding the \0) is returned in res_len if non-NULL */ const char * til_str_buf(const til_str_t *str, size_t *res_len) { assert(str); if (res_len) - *res_len = str->size.used; + *res_len = str->size.used - 1; return str->buf; } @@ -172,7 +173,7 @@ char * til_str_to_buf(til_str_t *str, size_t *res_len) assert(str); if (res_len) - *res_len = str->size.used; + *res_len = str->size.used - 1; buf = str->buf; free(str); -- cgit v1.2.1