From 47276a09dd103cbdd8150f852c727ea278548902 Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Sun, 5 Jan 2020 17:25:52 +1300 Subject: [PATCH] LibC: Remove dubious String ends_with usage As mentioned in #917, the String destructor could potentially be clobbering the errno. Use memcpy so that we do not need String at all. --- Libraries/LibC/stdlib.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Libraries/LibC/stdlib.cpp b/Libraries/LibC/stdlib.cpp index 661c65a72b..f8e3477ec4 100644 --- a/Libraries/LibC/stdlib.cpp +++ b/Libraries/LibC/stdlib.cpp @@ -1,7 +1,6 @@ #include #include #include -#include #include #include #include @@ -104,16 +103,16 @@ static inline T strtol_impl(const char* nptr, char** endptr, int base) return num; } -[[nodiscard]] int __generate_unique_filename(char* pattern) +__attribute__((warn_unused_result)) int __generate_unique_filename(char* pattern) { - int length = strlen(pattern); + size_t length = strlen(pattern); - if (length < 6 || !String(pattern).ends_with("XXXXXX")) { + if (length < 6 || memcmp(pattern + length - 6, "XXXXXX", 6)) { errno = EINVAL; return -1; } - int start = length - 6; + size_t start = length - 6; static constexpr char random_characters[] = "abcdefghijklmnopqrstuvwxyz0123456789";