summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/al/str.h7
-rw-r--r--tests/str.c3
2 files changed, 7 insertions, 3 deletions
diff --git a/include/al/str.h b/include/al/str.h
index 20a1c26..a572dec 100644
--- a/include/al/str.h
+++ b/include/al/str.h
@@ -79,9 +79,10 @@ static inline void al_str_to_upper(str *s)
// Returned c_str must be free'd by the user.
static inline char *al_str_to_c_str(str *s)
{
- char *c_str = (char *)al_malloc(s->length + 1);
- al_memcpy(c_str, s->data, s->length);
- c_str[s->length] = '\0';
+ size_t length = (size_t)s->length;
+ char *c_str = (char *)al_malloc(length + 1);
+ al_memcpy(c_str, s->data, length);
+ c_str[length] = '\0';
return c_str;
}
diff --git a/tests/str.c b/tests/str.c
index c783673..933b1f9 100644
--- a/tests/str.c
+++ b/tests/str.c
@@ -344,6 +344,9 @@ static bool str_test_limits(void)
}
al_str_cat(&s, &al_str_c("x"));
+ char *c_str = al_str_to_c_str(&s);
+ AL_TEST_TRUE(al_strlen(c_str) == MAX_ALLOC_32);
+
AL_TEST_TRUE(al_str_atr(&s, 1) == 'x');
AL_TEST_TRUE(al_str_at(&s, MAX_ALLOC_32 - 1) == 'x');
AL_TEST_TRUE(al_str_atr(&s, 2) == 'y');