1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 21:18:14 +00:00

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).
This commit is contained in:
Ben Wiederhake 2020-08-23 14:43:16 +02:00 committed by Andreas Kling
parent 41b70ae8ba
commit 9785173dec

View file

@ -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();
}