From 030f1be0bb818526b5d67be87c81c17443d9f594 Mon Sep 17 00:00:00 2001 From: lotherk Date: Mon, 23 Nov 2026 10:23:37 +0000 Subject: [PATCH] leaks and double getenv: typing thread, program_found typing_thread() returned NULL with url/reply already built when a later sp_buf_init() failed - a small leak, but on a thread that outlives the turn and is never reclaimed. It now leaves through one exit that frees all four. program_found() called getenv("PATH") twice; one read is the whole truth. --- src/backend/telegram.c | 21 +++++++++++++++++++-- src/main.c | 5 ++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/backend/telegram.c b/src/backend/telegram.c index 59552a7..5fb5ead 100644 --- a/src/backend/telegram.c +++ b/src/backend/telegram.c @@ -84,15 +84,32 @@ typing_thread(void *arg) struct sp_buf url, reply, err; int i; + memset(&url, 0, sizeof(url)); + memset(&reply, 0, sizeof(reply)); + memset(&err, 0, sizeof(err)); + /* + * Every buffer goes through the same exit: an init that fails must not + * leak the ones already built, and this runs on its own thread where a + * leak is never reclaimed. sp_buf_free() is safe on a zeroed buffer. + */ if (sp_buf_init(&url) != 0 || sp_buf_init(&reply) != 0 || - sp_buf_init(&err) != 0) + sp_buf_init(&err) != 0) { + sp_buf_free(&url); + sp_buf_free(&reply); + sp_buf_free(&err); return NULL; + } sp_buf_printf(&url, "%s/bot%s/sendChatAction", api_base(), t->token); { struct sp_buf body; - if (sp_buf_init(&body) != 0) + memset(&body, 0, sizeof(body)); + if (sp_buf_init(&body) != 0) { + sp_buf_free(&url); + sp_buf_free(&reply); + sp_buf_free(&err); return NULL; + } sp_buf_printf(&body, "{\"chat_id\":%ld,\"action\":\"typing\"}", t->chat_id); for (i = 0; i < 300 && *t->stop == 0; i++) { diff --git a/src/main.c b/src/main.c index 995d2e4..6613579 100644 --- a/src/main.c +++ b/src/main.c @@ -743,6 +743,7 @@ static int program_found(const char *cmd) { char word[128], *path, *dir, *save = NULL; + const char *penv; size_t n = strcspn(cmd, " \t"); if (n == 0 || n >= sizeof(word)) @@ -751,7 +752,9 @@ program_found(const char *cmd) word[n] = '\0'; if (strchr(word, '/') != NULL) return access(word, X_OK) == 0; - path = getenv("PATH") != NULL ? strdup(getenv("PATH")) : NULL; + /* one getenv: asking twice reads as if the second answer may differ */ + penv = getenv("PATH"); + path = penv != NULL ? strdup(penv) : NULL; if (path == NULL) return 0; for (dir = strtok_r(path, ":", &save); dir != NULL;