From 0220cbfa47f4f3cb3f99f8f33530698e491d552d Mon Sep 17 00:00:00 2001 From: dasha_uwu Date: Tue, 13 Oct 2026 22:11:54 +0500 Subject: [PATCH] linux: fix calculating size incorrectly and handle read errors while reading environment --- linux/Platform.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/linux/Platform.c b/linux/Platform.c index dd4121e8e6..030620e730 100644 --- a/linux/Platform.c +++ b/linux/Platform.c @@ -530,22 +530,25 @@ char* Platform_getProcessEnv(pid_t pid) { size_t capacity = 0; size_t size = 0; - ssize_t bytes = 0; - do { - size += bytes; + for (;;) { capacity += 4096; env = xRealloc(env, capacity); - } while (!ferror(fp) && !feof(fp) && (bytes = fread(env + size, 1, capacity - size, fp)) > 0); - fclose(fp); + size_t n = capacity - size; + size_t bytes = fread(env + size, 1, n, fp); + size += bytes; + if (bytes < n) + break; + } - if (bytes < 0) { + if (ferror(fp)) { + fclose(fp); free(env); return NULL; } - size += bytes; + fclose(fp); env = xRealloc(env, size + 2);