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.
This commit is contained in:
1 parent
4f7255548d
commit
030f1be0bb
2 files changed
+23
-3
No files matched your search
+19
-2
@@ -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++) {
|
||||
|
||||
+4
-1
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user