From 9785173dec2c615fc2c932b69fdfb7b3866a0fc1 Mon Sep 17 00:00:00 2001 From: Ben Wiederhake Date: Sun, 23 Aug 2020 14:43:16 +0200 Subject: [PATCH] LibC: Fix strftime() for max_size=0 Before, strftime unintentionally interpreted 0 as 'unlimited'. The specification of strftime says no such thing. Now, it properly returns 0 in that case (because the NUL byte doesn't fit). --- Libraries/LibC/time.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Libraries/LibC/time.cpp b/Libraries/LibC/time.cpp index 6b1d0239a0..a3fe523de2 100644 --- a/Libraries/LibC/time.cpp +++ b/Libraries/LibC/time.cpp @@ -185,7 +185,7 @@ size_t strftime(char* destination, size_t max_size, const char* format, const st "July", "Auguest", "September", "October", "November", "December" }; - StringBuilder builder { max_size - 1 }; + StringBuilder builder { max_size }; const int format_len = strlen(format); for (int i = 0; i < format_len; ++i) { @@ -307,10 +307,12 @@ size_t strftime(char* destination, size_t max_size, const char* format, const st return 0; } } - if (builder.length() > max_size - 1) + if (builder.length() + 1 > max_size) return 0; } + if (builder.length() + 1 > max_size) + return 0; strcpy(destination, builder.build().characters()); return builder.length(); }