commit 6e1c41adc8ee3e9afc984ed4ba5376531c4fa980 from: Mark Jamsek date: Thu Jun 16 14:41:49 2022 UTC plug realloc memleak and style(9) fixes in expand_tab() fixes and ok from tb@ commit - ccda2f4db6c3f1c5132eefc87f1fc2dbd5298cc4 commit + 6e1c41adc8ee3e9afc984ed4ba5376531c4fa980 blob - 285021accb225e5cc858097d09e1a68d4b32e1ca blob + 17dcaead07084e8929e136a76c67ee632353275d --- tog/tog.c +++ tog/tog.c @@ -1232,7 +1232,7 @@ expand_tab(char **ptr, const char *src) *ptr = NULL; n = len = strlen(src); - dst = malloc((n + 1) * sizeof(char)); + dst = malloc(n + 1); if (dst == NULL) return got_error_from_errno("malloc"); @@ -1241,11 +1241,15 @@ expand_tab(char **ptr, const char *src) if (c == '\t') { size_t nb = TABSIZE - sz % TABSIZE; + char *p = realloc(dst, n + nb); + if (p == NULL) { + free(dst); + return got_error_from_errno("realloc"); + + } + dst = p; n += nb; - dst = reallocarray(dst, n, sizeof(char)); - if (dst == NULL) - return got_error_from_errno("reallocarray"); - memcpy(dst + sz, " ", nb); + memset(dst + sz, ' ', nb); sz += nb; } else dst[sz++] = src[idx];