summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVito Caputo <vcaputo@pengaru.com>2023-06-26 17:19:35 -0700
committerVito Caputo <vcaputo@pengaru.com>2023-07-04 21:09:16 -0700
commit3c5db5339454f48edb48c9b1498c1832c6711eb1 (patch)
tree2c371c67ffe963ac85904090d45e6f163781b8ee
parenta33c742c87c5e267f00e561c27f96ddfb522f6bc (diff)
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.
-rw-r--r--src/til_settings.c8
-rw-r--r--src/til_settings.h1
-rw-r--r--src/til_setup.c6
-rw-r--r--src/til_str.c5
4 files changed, 12 insertions, 8 deletions
diff --git a/src/til_settings.c b/src/til_settings.c
index 2985635..fb71bf7 100644
--- a/src/til_settings.c
+++ b/src/til_settings.c
@@ -679,7 +679,11 @@ int til_settings_strprint_path(const til_settings_t *settings, til_str_t *str)
}
-int til_settings_path_as_buf(const til_settings_t *settings, char **res_buf, size_t *res_bufsz)
+/*
+ * returns a raw path to settings in *res_buf
+ * if res_len is provided, the returned string length excluding nul is stored there (til_str_to_buf())
+ */
+static int til_settings_path_as_buf(const til_settings_t *settings, char **res_buf, size_t *res_len)
{
til_str_t *str;
int r;
@@ -695,7 +699,7 @@ int til_settings_path_as_buf(const til_settings_t *settings, char **res_buf, siz
if (r < 0)
return r;
- *res_buf = til_str_to_buf(str, res_bufsz);
+ *res_buf = til_str_to_buf(str, res_len);
return 0;
}
diff --git a/src/til_settings.h b/src/til_settings.h
index 43bbee1..8cdaf88 100644
--- a/src/til_settings.h
+++ b/src/til_settings.h
@@ -62,7 +62,6 @@ int til_setting_desc_fprint_path(const til_setting_desc_t *desc, FILE *out);
int til_setting_spec_check(const til_setting_spec_t *spec, const char *value);
int til_settings_label_setting(const til_settings_t *settings, const til_setting_t *setting, char **res_label);
int til_settings_strprint_path(const til_settings_t *settings, til_str_t *str);
-int til_settings_path_as_buf(const til_settings_t *settings, char **res_buf, size_t *res_bufsz);
int til_settings_fprint_path(const til_settings_t *settings, FILE *out);
#ifndef TIL_SETTINGS_STR
diff --git a/src/til_setup.c b/src/til_setup.c
index 23827f3..bd57073 100644
--- a/src/til_setup.c
+++ b/src/til_setup.c
@@ -25,7 +25,7 @@
void * til_setup_new(const til_settings_t *settings, size_t size, void (*free_func)(til_setup_t *setup))
{
char *path_buf = NULL;
- size_t path_sz;
+ size_t path_len;
til_str_t *path_str;
til_setup_t *setup;
int r;
@@ -41,7 +41,7 @@ void * til_setup_new(const til_settings_t *settings, size_t size, void (*free_fu
if (r < 0)
return til_str_free(path_str);
- path_buf = til_str_to_buf(path_str, &path_sz);
+ path_buf = til_str_to_buf(path_str, &path_len);
setup = calloc(1, size);
if (!setup) {
@@ -50,7 +50,7 @@ void * til_setup_new(const til_settings_t *settings, size_t size, void (*free_fu
}
setup->path = path_buf;
- setup->path_hash = til_jenkins((uint8_t *)path_buf, path_sz);
+ setup->path_hash = til_jenkins((uint8_t *)path_buf, path_len + 1 /* include the \0 */);
setup->refcount = 1;
setup->free = free_func;
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);
© All Rights Reserved