# HG changeset patch # User Matti Hamalainen # Date 1580097717 -7200 # Node ID da9618444c81ea90596c3f6d7dea7fbe476055e9 # Parent c17a1072974a6d6ddf2e90767ce725af220b4bfd Cleanups. diff -r c17a1072974a -r da9618444c81 th_string.c --- a/th_string.c Mon Jan 27 00:38:46 2020 +0200 +++ b/th_string.c Mon Jan 27 06:01:57 2020 +0200 @@ -39,6 +39,7 @@ * characters from the source. A NUL terminator is always added, * unless @p src was @c NULL or memory could not be allocated, in which * case @c NULL pointer is returned. + * * @param[in] src string pointer to be copied from * @param[in] n maximum number of characters to copy * @returns the resulting string or @c NULL pointer @@ -55,10 +56,10 @@ if (len > n) len = n; - if ((res = th_malloc(len + 1)) == NULL) + if ((res = th_malloc((len + 1) * sizeof(char))) == NULL) return NULL; - memcpy(res, src, len); + memcpy(res, src, len * sizeof(char)); res[len] = 0; return res; @@ -100,10 +101,10 @@ return NULL; len = end - start + 1; - if ((res = th_malloc(len + 1)) == NULL) + if ((res = th_malloc((len + 1) * sizeof(char))) == NULL) return NULL; - memcpy(res, src + start, len); + memcpy(res, src + start, len * sizeof(char)); res[len] = 0; return res; } @@ -139,6 +140,7 @@ char *th_strndup_trim(const char *src, const size_t n, const int flags) { size_t len; + if (src == NULL || n == 0) return NULL; @@ -152,10 +154,10 @@ { char *res; - if ((res = th_malloc(len + 1)) == NULL) + if ((res = th_malloc((len + 1) * sizeof(char))) == NULL) return NULL; - memcpy(res, src, len); + memcpy(res, src, len * sizeof(char)); res[len] = 0; return res; @@ -317,17 +319,18 @@ th_vprintf_ctx ctx; va_list ap; - // Get size + // Get the size of the output va_copy(ap, args); ctx.pos = 0; th_vprintf_do(&ctx, th_pbuf_alloc_vputch1, fmt, ap); va_end(ap); - // Allocate memory + // Allocate memory for it ctx.size = ctx.pos + 1; - if ((ctx.buf = th_malloc(ctx.size)) == NULL) + if ((ctx.buf = th_malloc(ctx.size * sizeof(char))) == NULL) return NULL; + // Print the final result into the buffer va_copy(ap, args); ctx.pos = 0; th_vprintf_do(&ctx, th_pbuf_alloc_vputch2, fmt, ap); @@ -344,7 +347,7 @@ if (fmt == NULL) return NULL; - if ((buf = th_malloc(size)) == NULL) + if ((buf = th_malloc(size * sizeof(char))) == NULL) return NULL; while (1) @@ -362,7 +365,7 @@ else size *= 2; - if ((tmp = th_realloc(buf, size)) == NULL) + if ((tmp = th_realloc(buf, size * sizeof(char))) == NULL) { th_free(buf); return NULL; @@ -535,10 +538,10 @@ th_free(*pdst); size_t slen = strlen(src); - if ((*pdst = th_malloc(slen + 1)) == NULL) + if ((*pdst = th_malloc((slen + 1) * sizeof(char))) == NULL) return THERR_MALLOC; - memcpy(*pdst, src, slen + 1); + memcpy(*pdst, src, (slen + 1) * sizeof(char)); return THERR_OK; } @@ -556,18 +559,18 @@ if (*pdst != NULL) { size_t dlen = strlen(*pdst), slen = strlen(src); - if ((*pdst = th_realloc(*pdst, dlen + slen + 1)) == NULL) + if ((*pdst = th_realloc(*pdst, (dlen + slen + 1) * sizeof(char))) == NULL) return THERR_MALLOC; - memcpy((*pdst) + dlen, src, slen + 1); + memcpy((*pdst) + dlen, src, (slen + 1) * sizeof(char)); } else { size_t slen = strlen(src); - if ((*pdst = th_malloc(slen + 1)) == NULL) + if ((*pdst = th_malloc((slen + 1) * sizeof(char))) == NULL) return THERR_MALLOC; - memcpy(*pdst, src, slen + 1); + memcpy(*pdst, src, (slen + 1) * sizeof(char)); } return THERR_OK;