소스 검색

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.
Andrew Kaster 4 년 전
부모
커밋
c2d8b8ec14
1개의 변경된 파일4개의 추가작업 그리고 4개의 파일을 삭제
  1. 4 4
      Userland/Utilities/shuf.cpp

+ 4 - 4
Userland/Utilities/shuf.cpp

@@ -40,12 +40,11 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char** argv)
 
 
     Vector<String> lines;
     Vector<String> lines;
 
 
+    char* buffer = nullptr;
     for (;;) {
     for (;;) {
-        char* buffer = nullptr;
-        ssize_t buflen = 0;
-        size_t n;
+        size_t n = 0;
         errno = 0;
         errno = 0;
-        buflen = getline(&buffer, &n, stdin);
+        ssize_t buflen = getline(&buffer, &n, stdin);
         if (buflen == -1 && errno != 0) {
         if (buflen == -1 && errno != 0) {
             perror("getline");
             perror("getline");
             exit(1);
             exit(1);
@@ -54,6 +53,7 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char** argv)
             break;
             break;
         lines.append({ buffer, AK::ShouldChomp::Chomp });
         lines.append({ buffer, AK::ShouldChomp::Chomp });
     }
     }
+    free(buffer);
 
 
     // Fisher-Yates shuffle
     // Fisher-Yates shuffle
     String tmp;
     String tmp;