From 9724797da7816525e71f89e464752aeaa72f48be Mon Sep 17 00:00:00 2001 From: Victor Song Date: Mon, 26 Dec 2022 18:15:28 -0500 Subject: [PATCH] Utilities: Print arbitrary bytes in `ls` Currently, `ls` crashes when printing certain byte sequences. This is likely due to an out-of-bounds error when iterating through the `StringView` representing the file name to be printed. We switch to using an index-based for loop to a range-based for loop. This fixes #16678. --- Userland/Utilities/ls.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Userland/Utilities/ls.cpp b/Userland/Utilities/ls.cpp index 065466e433..1779e4c4f5 100644 --- a/Userland/Utilities/ls.cpp +++ b/Userland/Utilities/ls.cpp @@ -207,12 +207,12 @@ static int print_escaped(StringView name) return utf8_name.length(); } - for (int i = 0; name[i] != '\0'; i++) { - if (isprint(name[i])) { - putchar(name[i]); + for (auto c : name) { + if (isprint(c)) { + putchar(c); printed++; } else { - printed += printf("\\%03d", name[i]); + printed += printf("\\%03d", c); } }