From e12a707ca1a921e4912903fd43ea8199a2d9b476 Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Tue, 21 Sep 2021 19:18:19 +0300 Subject: [PATCH] checksum: Stop reusing the same Core::File for multiple files Since we were just repeatedly calling `->open()` on the same Core::File no one was clearing it's internal buffer, which means hashing multiple files in one go would result in different hashes than hashing each file separately. --- Userland/Utilities/checksum.cpp | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/Userland/Utilities/checksum.cpp b/Userland/Utilities/checksum.cpp index e6ecbefd55..254df5bd5a 100644 --- a/Userland/Utilities/checksum.cpp +++ b/Userland/Utilities/checksum.cpp @@ -48,21 +48,18 @@ int main(int argc, char** argv) Crypto::Hash::Manager hash; hash.initialize(hash_kind); - bool success; auto has_error = false; - auto file = Core::File::construct(); for (auto const& path : paths) { - if (path == "-") { - success = file->open(STDIN_FILENO, Core::OpenMode::ReadOnly, Core::File::ShouldCloseFileDescriptor::No); - } else { - file->set_filename(path); - success = file->open(Core::OpenMode::ReadOnly); - } - if (!success) { - warnln("{}: {}: {}", argv[0], path, file->error_string()); - has_error = true; - continue; + NonnullRefPtr file = Core::File::standard_input(); + if (path != "-"sv) { + auto file_or_error = Core::File::open(path, Core::OpenMode::ReadOnly); + if (file_or_error.is_error()) { + warnln("{}: {}: {}", argv[0], path, file->error_string()); + has_error = true; + continue; + } + file = file_or_error.release_value(); } while (!file->eof() && !file->has_error())