commit e3644194d3747c9566be600b58e5cc6790d0bc6c from: Omar Polo date: Wed Dec 21 10:23:57 2022 UTC kamiftp: ensure compl_add_entry leaves a NULL terminator while here also rearrange a bit the compl_state_reset/compl_add_entry dance and do the allocations only in the latter. commit - 3dd0b26000acfddf863190d65816a2f8f44d115e commit + e3644194d3747c9566be600b58e5cc6790d0bc6c blob - bb79125082d73d0e0582a582c068c5b6fd716a7e blob + 95de45a9235cc7b875f9ae926eff75b27e0b383b --- kamiftp/rl.c +++ kamiftp/rl.c @@ -84,10 +84,7 @@ compl_state_reset(void) free(compl_state.entries[i]); free(compl_state.entries); - compl_state.len = 0; - compl_state.size = 16; - if ((compl_state.entries = calloc(16, sizeof(char *))) == NULL) - compl_state.size = 0; + memset(&compl_state, 0, sizeof(compl_state)); } static int @@ -101,8 +98,12 @@ compl_add_entry(const struct np_stat *st) size_t newsz = compl_state.size * 1.5; void *t; + if (newsz == 0) + newsz = 16; + + /* one for the NULL entry at the end */ t = recallocarray(compl_state.entries, compl_state.size, - newsz, sizeof(char *)); + newsz + 1, sizeof(char *)); if (t == NULL) return -1; compl_state.entries = t;