Skip to content

Commit

Permalink
added malloc/realloc call checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Eike-Flath committed Apr 21, 2024
1 parent 8b14fa5 commit 42370e1
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/date/date.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,19 @@ int main(int argc, char **argv) {

size_t cap = INITIAL_BUF_SIZE;
char *buf = malloc(cap);
if (!buf) {
fprintf(stderr, "date: malloc failed: %s\n", strerror(errno));
return EXIT_FAILURE;
}
while (!strftime(buf, cap, &operand[1], tm)) {
cap *= 2;
buf = realloc(buf, cap);
char *new_buf = realloc(buf, cap);
if (!new_buf) {
free(buf);
fprintf(stderr, "date: realloc failed: %s\n", strerror(errno));
return EXIT_FAILURE;
}
buf = new_buf;
}
printf("%s\n", buf);
free(buf);
Expand Down

0 comments on commit 42370e1

Please sign in to comment.