Skip to content

Commit 21945b8

Browse files
committed
fix cctype usage
According to the documentation, we should cast to unsigned char. Anything negative is undefined behavior.
1 parent df92b1d commit 21945b8

32 files changed

Lines changed: 170 additions & 90 deletions

Utilities/Config.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "util/logs.hpp"
66
#include "util/atomic.hpp"
77
#include "util/shared_ptr.hpp"
8+
#include "util/cctype.hpp"
89

910
#include <algorithm>
1011
#include <utility>
@@ -220,7 +221,7 @@ namespace cfg
220221
}
221222

222223
char copy[5];
223-
std::transform(value.begin(), value.end(), std::begin(copy), ::tolower);
224+
std::transform(value.begin(), value.end(), std::begin(copy), utils::tolower<char>);
224225

225226
if (value.size() == 5 && std::string_view{copy, 5} == "false")
226227
m_value = false;

Utilities/JITLLVM.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#if defined(__APPLE__)
1616
#include <pthread.h>
17+
#elif defined(ANDROID)
18+
#include "util/cctype.hpp"
1719
#endif
1820

1921
LOG_CHANNEL(jit_log, "JIT");
@@ -1039,7 +1041,7 @@ const char * fallback_cpu_detection()
10391041
return "cortex-a78";
10401042
}
10411043

1042-
std::transform(result.begin(), result.end(), result.begin(), ::tolower);
1044+
std::transform(result.begin(), result.end(), result.begin(), utils::tolower<char>);
10431045
return result;
10441046
}();
10451047

Utilities/LUrlParser.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
*/
2727

2828
#include "LUrlParser.h"
29+
#include "util/cctype.hpp"
2930

3031
#include <algorithm>
3132
#include <cstring>
@@ -36,7 +37,7 @@ static bool IsSchemeValid( const std::string& SchemeName )
3637
{
3738
return std::all_of(SchemeName.cbegin(), SchemeName.cend(), [](const auto& c)
3839
{
39-
return isalpha(c) || c == '+' || c == '-' || c == '.';
40+
return utils::isalpha(c) || c == '+' || c == '-' || c == '.';
4041
});
4142
}
4243

Utilities/StrFmt.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "cfmt.h"
44
#include "util/endian.hpp"
55
#include "util/v128.hpp"
6+
#include "util/cctype.hpp"
67

78
#include <locale>
89
#include <codecvt>
@@ -198,12 +199,12 @@ fmt::base57_result fmt::base57_result::from_string(std::string_view str)
198199
{
199200
auto to_val = [](u8 c) -> u64
200201
{
201-
if (std::isdigit(c))
202+
if (utils::isdigit(c))
202203
{
203204
return c - '0';
204205
}
205206

206-
if (std::isupper(c))
207+
if (utils::isupper(c))
207208
{
208209
// Omitted characters
209210
if (c == 'B' || c == 'D' || c == 'I' || c == 'O')
@@ -231,7 +232,7 @@ fmt::base57_result fmt::base57_result::from_string(std::string_view str)
231232
return c - 'A' + 10;
232233
}
233234

234-
if (std::islower(c))
235+
if (utils::islower(c))
235236
{
236237
// Omitted characters
237238
if (c == 'l')
@@ -952,15 +953,15 @@ std::string fmt::to_upper(std::string_view string)
952953
{
953954
std::string result;
954955
result.resize(string.size());
955-
std::transform(string.begin(), string.end(), result.begin(), ::toupper);
956+
std::transform(string.begin(), string.end(), result.begin(), utils::toupper<char>);
956957
return result;
957958
}
958959

959960
std::string fmt::to_lower(std::string_view string)
960961
{
961962
std::string result;
962963
result.resize(string.size());
963-
std::transform(string.begin(), string.end(), result.begin(), ::tolower);
964+
std::transform(string.begin(), string.end(), result.begin(), utils::tolower<char>);
964965
return result;
965966
}
966967

Utilities/Thread.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ DYNAMIC_IMPORT_RENAME("Kernel32.dll", SetThreadDescriptionImport, "SetThreadDesc
9696
#include "util/asm.hpp"
9797
#include "util/v128.hpp"
9898
#include "util/simd.hpp"
99+
#include "util/cctype.hpp"
99100
#include "util/sysinfo.hpp"
100101
#include "Emu/Memory/vm_locking.h"
101102

@@ -185,9 +186,9 @@ bool IsDebuggerPresent()
185186

186187
for (const char* cp = status.data() + found + 10; cp <= status.data() + num_read; ++cp)
187188
{
188-
if (!std::isspace(*cp))
189+
if (!utils::isspace(*cp))
189190
{
190-
return std::isdigit(*cp) != 0 && *cp != '0';
191+
return utils::isdigit(*cp) != 0 && *cp != '0';
191192
}
192193
}
193194

Utilities/bin_patch.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "util/types.hpp"
1212
#include "util/asm.hpp"
13+
#include "util/cctype.hpp"
1314

1415
#include <charconv>
1516
#include <regex>
@@ -329,7 +330,7 @@ bool patch_engine::load(patch_map& patches_map, const std::string& path, std::st
329330
is_valid = false;
330331
continue;
331332
}
332-
else if (serial.size() != 9 || !std::all_of(serial.begin(), serial.end(), [](char c) { return std::isalnum(static_cast<unsigned char>(c)); }))
333+
else if (serial.size() != 9 || !std::all_of(serial.begin(), serial.end(), [](char c) { return utils::isalnum(c); }))
333334
{
334335
append_log_message(log_messages, fmt::format("Error: Serial '%s' invalid (patch: %s, key: %s, location: %s, file: %s)", serial, description, main_key, get_yaml_node_location(serial_node), path), &patch_log.error);
335336
is_valid = false;

rpcs3/Crypto/unedat.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include "Emu/system_utils.hpp"
1010

1111
#include "util/asm.hpp"
12+
#include "util/cctype.hpp"
13+
1214
#include <algorithm>
1315
#include <span>
1416

@@ -628,8 +630,8 @@ bool validate_npd_hashes(std::string_view file_name, const u8* klicensee, const
628630
for (usz i = std::distance(it, buf_span.rend()) - 1; i < buf_len; ++i)
629631
{
630632
const u8 c = buf[i];
631-
buf_upper[i] = std::toupper(c);
632-
buf_lower[i] = std::tolower(c);
633+
buf_upper[i] = utils::toupper(c);
634+
buf_lower[i] = utils::tolower(c);
633635
}
634636

635637
// Hash with NPDRM_OMAC_KEY_3 and compare with title_hash.

rpcs3/Emu/Cell/Modules/cellGame.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "Utilities/StrUtil.h"
1919
#include "util/init_mutex.hpp"
2020
#include "util/asm.hpp"
21+
#include "util/cctype.hpp"
2122
#include "Crypto/utils.h"
2223

2324
#include <span>
@@ -228,13 +229,13 @@ static bool check_system_ver(vm::cptr<char> systemVersion)
228229
return (
229230
systemVersion &&
230231
std::strlen(systemVersion.get_ptr()) == 7 &&
231-
std::isdigit(systemVersion[0]) &&
232-
std::isdigit(systemVersion[1]) &&
232+
utils::isdigit(systemVersion[0]) &&
233+
utils::isdigit(systemVersion[1]) &&
233234
systemVersion[2] == '.' &&
234-
std::isdigit(systemVersion[3]) &&
235-
std::isdigit(systemVersion[4]) &&
236-
std::isdigit(systemVersion[5]) &&
237-
std::isdigit(systemVersion[6])
235+
utils::isdigit(systemVersion[3]) &&
236+
utils::isdigit(systemVersion[4]) &&
237+
utils::isdigit(systemVersion[5]) &&
238+
utils::isdigit(systemVersion[6])
238239
);
239240
}
240241

rpcs3/Emu/Cell/Modules/cellKb.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
#include "Emu/IdManager.h"
33
#include "Emu/System.h"
44
#include "Emu/Cell/PPUModule.h"
5-
65
#include "Emu/Io/KeyboardHandler.h"
6+
#include "util/cctype.hpp"
77
#include "cellKb.h"
88

99
error_code sys_config_start(ppu_thread& ppu);
@@ -198,7 +198,7 @@ u16 cellKbCnvRawCode(u32 arrange, u32 mkey, u32 led, u16 rawcode)
198198
const auto get_ascii = [&](u16 raw, u16 shifted = 0, u16 altered = 0)
199199
{
200200
// Usually caps lock only applies uppercase to letters, but some layouts treat it as shift lock for all keys.
201-
if ((is_shift || (is_caps_lock && (is_shift_lock || std::isalpha(raw)))) && shifted)
201+
if ((is_shift || (is_caps_lock && (is_shift_lock || utils::isalpha(raw)))) && shifted)
202202
{
203203
return shifted;
204204
}

0 commit comments

Comments
 (0)