From ea411774f017cd099fc77e67e673cdd51dd6aa38 Mon Sep 17 00:00:00 2001 From: Tim Ledbetter Date: Sat, 22 Jul 2023 15:49:22 +0100 Subject: [PATCH] truncate: Clamp file size to 0 when using the `-s` option When using the `-s` option to reduce the size of a file, the file size is now set to zero if the argument given would result in a file size less than zero. This matches the behavior of truncate on Linux and FreeBSD. --- Userland/Utilities/truncate.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Userland/Utilities/truncate.cpp b/Userland/Utilities/truncate.cpp index 6967002bd0..adb20c9453 100644 --- a/Userland/Utilities/truncate.cpp +++ b/Userland/Utilities/truncate.cpp @@ -99,7 +99,7 @@ ErrorOr serenity_main(Main::Arguments arguments) size = stat.st_size + size; break; case OP_Shrink: - size = stat.st_size - size; + size = max(stat.st_size - size, 0); break; }