2
0
Fork 0
forked from zynk/zynk

fix: check asprintf return values to prevent undefined behavior on allocation failure

This commit is contained in:
Armin 2026-06-29 16:57:31 +02:00
commit 598a80c927

13
calc.c
View file

@ -19,6 +19,7 @@
#include <sys/wait.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
int asprintf(char **, const char *, ...);
#define DEFAULT_SERVER "irc.libera.chat"
enum { RBUF_SZ = 16384, DEF_PORT = 6697, POLL_MS = 100 };
#define CLR_RESET "\033[0m"
@ -225,7 +226,7 @@ static char *db_get_random(void) {
const char *n = (const char*)db_column_text(stmt, 2);
const char *t = (const char*)db_column_text(stmt, 3);
char *r = NULL;
asprintf(&r, "%s = %s (by %s on %s)", k, v, n, t);
if (asprintf(&r, "%s = %s (by %s on %s)", k, v, n, t) < 0) r = NULL;
db_finalize(stmt);
return r;
}
@ -239,7 +240,7 @@ static char *db_get(const char *key) {
const char *n = (const char*)db_column_text(stmt, 1);
const char *t = (const char*)db_column_text(stmt, 2);
char *r = NULL;
asprintf(&r, "%s (set by %s on %s)", v, n, t);
if (asprintf(&r, "%s (set by %s on %s)", v, n, t) < 0) r = NULL;
db_finalize(stmt);
return r;
}
@ -287,7 +288,7 @@ static char *ai_get_answer(void) {
const char *a = (const char*)db_column_text(stmt, 0);
const char *t = (const char*)db_column_text(stmt, 1);
char *r = NULL;
if (a && t) asprintf(&r, "%s\037%s", a, t);
if (a && t) { if (asprintf(&r, "%s\037%s", a, t) < 0) r = NULL; }
db_finalize(stmt);
return r;
}
@ -736,9 +737,9 @@ static char *fetch_forecast(const char *city_url, const char *city_display) {
free(json);
if (!ndays) return NULL;
char *out = NULL;
if (ndays == 1) asprintf(&out, "Forecast for %s: %s", city_display, line[0]);
else if (ndays == 2) asprintf(&out, "Forecast for %s: %s / %s", city_display, line[0], line[1]);
else asprintf(&out, "Forecast for %s: %s / %s / %s", city_display, line[0], line[1], line[2]);
if (ndays == 1) { if (asprintf(&out, "Forecast for %s: %s", city_display, line[0]) < 0) out = NULL; }
else if (ndays == 2) { if (asprintf(&out, "Forecast for %s: %s / %s", city_display, line[0], line[1]) < 0) out = NULL; }
else { if (asprintf(&out, "Forecast for %s: %s / %s / %s", city_display, line[0], line[1], line[2]) < 0) out = NULL; }
return out;
}
static void irc_handle(Session *s, const char *raw) {