From 23a54636eae332fc1815b4463fe2b3f537f69fab Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 19 Feb 2020 23:01:07 +0100 Subject: [PATCH] AK: Fix bug where "%s" with field width would print too many characters I introduced this while implementing "%.*s", oops. --- AK/PrintfImplementation.h | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/AK/PrintfImplementation.h b/AK/PrintfImplementation.h index ae2633cf7a..3fa916c20c 100644 --- a/AK/PrintfImplementation.h +++ b/AK/PrintfImplementation.h @@ -28,6 +28,7 @@ #include #include +#include #include #include @@ -228,20 +229,22 @@ template } template -[[gnu::always_inline]] inline int print_string(PutChFunc putch, char*& bufptr, const char* str, bool leftPad, u32 field_width, bool dot) +[[gnu::always_inline]] inline int print_string(PutChFunc putch, char*& bufptr, const char* str, bool left_pad, size_t field_width, bool dot) { size_t len = strlen(str); if (!dot && (!field_width || field_width < len)) field_width = len; - if (!leftPad && field_width > len) { - for (unsigned i = 0; i < field_width - len; ++i) + size_t pad_amount = field_width > len ? field_width - len : 0; + + if (!left_pad) { + for (size_t i = 0; i < pad_amount; ++i) putch(bufptr, ' '); } - for (unsigned i = 0; i < field_width; ++i) { + for (size_t i = 0; i < min(len, field_width); ++i) { putch(bufptr, str[i]); } - if (leftPad && field_width > len) { - for (unsigned i = 0; i < field_width - len; ++i) + if (left_pad) { + for (size_t i = 0; i < pad_amount; ++i) putch(bufptr, ' '); } return field_width;