From c2d8b8ec14d033972d5b51afedf821bfc04bcfe2 Mon Sep 17 00:00:00 2001 From: Andrew Kaster Date: Mon, 25 Jan 2021 23:04:35 -0700 Subject: [PATCH] Userland: Don't leak buffer from getline in shuf program Probably doesn't matter too too much since the program exits almost immediately after, but there's the principle of the thing to consider. --- Userland/Utilities/shuf.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Userland/Utilities/shuf.cpp b/Userland/Utilities/shuf.cpp index 79b3d9f60b..953323f5a0 100644 --- a/Userland/Utilities/shuf.cpp +++ b/Userland/Utilities/shuf.cpp @@ -40,12 +40,11 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char** argv) Vector lines; + char* buffer = nullptr; for (;;) { - char* buffer = nullptr; - ssize_t buflen = 0; - size_t n; + size_t n = 0; errno = 0; - buflen = getline(&buffer, &n, stdin); + ssize_t buflen = getline(&buffer, &n, stdin); if (buflen == -1 && errno != 0) { perror("getline"); exit(1); @@ -54,6 +53,7 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char** argv) break; lines.append({ buffer, AK::ShouldChomp::Chomp }); } + free(buffer); // Fisher-Yates shuffle String tmp;