diff options
author | pommicket <pommicket@gmail.com> | 2022-12-06 12:05:32 -0500 |
---|---|---|
committer | pommicket <pommicket@gmail.com> | 2022-12-06 12:05:32 -0500 |
commit | ac3a7a0c40864339b4c94ab3f58f8dd5d144ed2e (patch) | |
tree | 351ddd93af391171a4052bcdb4a11d2f98ba7488 /util.c | |
parent | 05862b802237df8bc61b0b45ef1ba4e70b0773f5 (diff) |
more lsp
Diffstat (limited to 'util.c')
-rw-r--r-- | util.c | 16 |
1 files changed, 13 insertions, 3 deletions
@@ -134,9 +134,14 @@ static void str_cat(char *dst, size_t dst_sz, char const *src) { } // safer version of strncpy. dst_sz includes a null terminator. -static void str_cpy(char *dst, size_t dst_sz, char const *src) { - size_t srclen = strlen(src); - size_t n = srclen; // number of bytes to copy +static void strn_cpy(char *dst, size_t dst_sz, char const *src, size_t src_len) { + size_t n = src_len; // number of bytes to copy + for (size_t i = 0; i < n; ++i) { + if (src[i] == '\0') { + n = i; + break; + } + } if (dst_sz == 0) { assert(0); @@ -149,6 +154,11 @@ static void str_cpy(char *dst, size_t dst_sz, char const *src) { dst[n] = 0; } +// safer version of strcpy. dst_sz includes a null terminator. +static void str_cpy(char *dst, size_t dst_sz, char const *src) { + strn_cpy(dst, dst_sz, src, SIZE_MAX); +} + #define strbuf_cpy(dst, src) str_cpy(dst, sizeof dst, src) #define strbuf_cat(dst, src) str_cat(dst, sizeof dst, src) |