feat: safer `free()'s

`free' allocated buffers only on success inside the main loop, and
`free' them on error inside their respective functions

(fixes an UB where an already freed buffer is freed again in the main
loop)
This commit is contained in:
Tom MTT 2022-11-23 07:08:13 +01:00
parent be74fa43cc
commit 6c57a4d03c
2 changed files with 12 additions and 6 deletions

7
bin.c
View file

@ -13,6 +13,7 @@
#include "bin.h"
#include <errno.h> /* for errno */
#include <stdio.h> /* for NULL, fclose, fopen, fputs, snprintf, FILE */
#include <stdlib.h> /* for calloc, free, malloc, rand, realloc */
#include <string.h> /* for strlen */
@ -39,8 +40,11 @@ char *generate_id(int min_length)
/* for each letter, generate a random one */
for (int i = 0; i < length; i++) {
if (i > 8 * min_length)
if (i > 8 * min_length) {
errno = EFBIG;
free(buffer);
return NULL;
}
buffer[i] = id_symbols[rand() % strlen(id_symbols)];
buffer[i + 1] = 0;
@ -55,6 +59,7 @@ char *generate_id(int min_length)
free(buffer);
return NULL;
}
buffer = tmp;
}
}