From 5fe891c10d2f9d9efd24dbea2703ad56e16bd9e3 Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Fri, 4 Oct 2024 12:36:18 -0400 Subject: (w)str: Guard against error-prone str_c() usage Also, reorder al_str_insert() arguments. Signed-off-by: Andrew Opalach --- tests/str.c | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/str.c b/tests/str.c index ce1c3b5..4af5dc4 100644 --- a/tests/str.c +++ b/tests/str.c @@ -1,5 +1,6 @@ #include #include +#include static bool str_test_cmp(void) { @@ -62,7 +63,7 @@ static bool str_test_insert(void) al_str_from(&insert_str, "testtestasdf"); al_str_from(&prepend_str, "testtestasdf"); - al_str_insert(&insert_str, al_str_c("[insert]"), 8); + al_str_insert(&insert_str, 8, al_str_c("[insert]")); al_str_prepend(&prepend_str, al_str_c("[prepend]")); AL_TEST_TRUE(al_str_eq(insert_result, &insert_str)); @@ -191,6 +192,45 @@ static bool str_test_to_long(void) AL_TEST_END(); } +static str *global __attribute__((unused)) = al_str_c("yeeee"); + +static bool str_test_error_prone_str_c(void) +{ + AL_TEST_START("str_error_prone_str_c"); + + str *n = al_str_c("yoyoyoyoyo"); + AL_TEST_EQ(n->len, al_strlen("yoyoyoyoyo"), u32); + AL_TEST_EQ(n->alloc, 0, u32); + AL_TEST_EQ(al_memcmp(n->data, "yoyoyoyoyo", al_strlen("yoyoyoyoyo") + 1), 0, u32); + +// This is expected to cause a compile error. +#if 0 + char *d = al_malloc(26); + char *s = "yoyoyoyoyo"; + al_memcpy(d, s, strlen(s) + 1); + + str ds = *al_str_c(d); + str ss = *al_str_cr(d); + AL_TEST_EQ(ds.len, (u32)(sizeof(char *) - 1), u32); + AL_TEST_EQ(ss.len, (u32)al_strlen(s), u32); + + al_free(d); + + wchar_t *wd = al_malloc(sizeof(wchar_t) * 26); + wchar_t *ws = L"yoyoyoyoyo"; + al_memcpy(wd, ws, (wcslen(ws) * sizeof(wchar_t)) + sizeof(wchar_t)); + + wstr wds = *al_wstr_c(wd); + wstr wss = *al_wstr_cr(wd); + AL_TEST_EQ(wds.len, (u32)((sizeof(wchar_t *) / sizeof(wchar_t)) - 1), u32); + AL_TEST_EQ(wss.len, (u32)wcslen(ws), u32); + + al_free(wd); +#endif + + AL_TEST_END(); +} + bool str_tests_run(void) { AL_TEST_START_GROUP("str"); @@ -202,6 +242,7 @@ bool str_tests_run(void) AL_TEST_RUN(str_test_substr); AL_TEST_RUN(str_test_get_line); AL_TEST_RUN(str_test_to_long); + AL_TEST_RUN(str_test_error_prone_str_c); AL_TEST_END_GROUP(); } -- cgit v1.2.3-101-g0448