Parcourir la source

grep: Adapt maximum line size depending on file size

Otherwise, we would never stop trying to fetch new lines if the next
newline is found beyond a range that the (default sized) buffer can
hold.
Tim Schumacher il y a 3 ans
Parent
commit
c1004b095e
1 fichiers modifiés avec 10 ajouts et 1 suppressions
  1. 10 1
      Userland/Utilities/grep.cpp

+ 10 - 1
Userland/Utilities/grep.cpp

@@ -195,8 +195,17 @@ ErrorOr<int> serenity_main(Main::Arguments args)
                 return false;
             }
 
+            auto file_size_or_error = Core::File::size(filename);
+            if (file_size_or_error.is_error()) {
+                if (!suppress_errors)
+                    warnln("Failed to retrieve size of {}: {}", filename, strerror(file_size_or_error.error().code()));
+                return false;
+            }
+
+            auto file_size = file_size_or_error.release_value();
+
             for (size_t line_number = 1; file->can_read_line(); ++line_number) {
-                auto line = file->read_line();
+                auto line = file->read_line(file_size);
                 auto is_binary = memchr(line.characters(), 0, line.length()) != nullptr;
 
                 if (matches(line, filename, line_number, print_filename, is_binary) && is_binary && binary_mode == BinaryFileMode::Binary)