comparison th_string.c @ 714:ec6a3790a91b

Do not use th_strlen() in th_strndup(), as the string might not have a terminating NUL before the n'th character.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 27 Apr 2020 03:33:48 +0300
parents 57d30f49fe1e
children 6f627f4f11db
comparison
equal deleted inserted replaced
713:3ea040bb0dca 714:ec6a3790a91b
85 size_t len; 85 size_t len;
86 86
87 if (src == NULL) 87 if (src == NULL)
88 return NULL; 88 return NULL;
89 89
90 len = th_strlen(src); 90 for (len = 0; len < n && src[len]; len++);
91 if (len > n)
92 len = n;
93 91
94 if ((res = th_malloc((len + 1) * sizeof(th_char_t))) == NULL) 92 if ((res = th_malloc((len + 1) * sizeof(th_char_t))) == NULL)
95 return NULL; 93 return NULL;
96 94
97 memcpy(res, src, len * sizeof(th_char_t)); 95 memcpy(res, src, len * sizeof(th_char_t));