From 08d387cae9460676327c19c65328b0d72f6ebe9c Mon Sep 17 00:00:00 2001 From: Aidan Hahn Date: Mon, 7 Mar 2022 00:15:11 -0800 Subject: [PATCH] reduce bad reads Signed-off-by: Aidan Hahn --- alog.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/alog.c b/alog.c index 9abf360..95ae87b 100644 --- a/alog.c +++ b/alog.c @@ -154,10 +154,9 @@ void alog ( // GET TIMESTAMP time_t t = time(NULL); struct tm * p = localtime(&t); - int timestamp_size = strftime(NULL, 0, "[%c]", p); - char *timestamp = malloc(sizeof(char) * (timestamp_size+1)); - strftime(timestamp, timestamp_size, "[%c]", p); - char *msg_and_timestamp = malloc(sizeof(char) * (timestamp_size + strlen(message) + 3)); + char *timestamp = malloc(strftime(NULL, 0, "[%c]", p) + 1); + strftime(timestamp, sizeof(timestamp), "[%c]", p); + char *msg_and_timestamp = malloc(sizeof(timestamp) + 1 + (strlen(message) + 1) + 2); sprintf(msg_and_timestamp, "%s %s\n", timestamp, message); free(timestamp); size = snprintf(NULL, 0, msg_and_timestamp, fmt_list);