Skip to content

Commit baa758d

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 baa758d

36 files changed

Lines changed: 522 additions & 113 deletions

Utilities/Config.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "Config.h"
33
#include "util/types.hpp"
44
#include "util/yaml.hpp"
5+
#include "util/cctype.hpp"
56

67
#include <charconv>
78

@@ -712,6 +713,26 @@ bool cfg::node::validate(std::string_view value)
712713
return false;
713714
}
714715

716+
bool cfg::_bool::from_string(std::string_view value, bool /*dynamic*/)
717+
{
718+
if (value.size() != 4 && value.size() != 5)
719+
{
720+
return false;
721+
}
722+
723+
char copy[5];
724+
std::transform(value.begin(), value.end(), std::begin(copy), utils::tolower<char>);
725+
726+
if (value.size() == 5 && std::string_view{copy, 5} == "false")
727+
m_value = false;
728+
else if (value.size() == 4 && std::string_view{copy, 4} == "true")
729+
m_value = true;
730+
else
731+
return false;
732+
733+
return true;
734+
}
735+
715736
std::string cfg::map_entry::get_value(std::string_view key)
716737
{
717738
if (auto it = m_map.find(key); it != m_map.end())

Utilities/Config.h

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -212,25 +212,7 @@ namespace cfg
212212
return def ? "true" : "false";
213213
}
214214

215-
bool from_string(std::string_view value, bool /*dynamic*/ = false) override
216-
{
217-
if (value.size() != 4 && value.size() != 5)
218-
{
219-
return false;
220-
}
221-
222-
char copy[5];
223-
std::transform(value.begin(), value.end(), std::begin(copy), ::tolower);
224-
225-
if (value.size() == 5 && std::string_view{copy, 5} == "false")
226-
m_value = false;
227-
else if (value.size() == 4 && std::string_view{copy, 4} == "true")
228-
m_value = true;
229-
else
230-
return false;
231-
232-
return true;
233-
}
215+
bool from_string(std::string_view value, bool dynamic = false) override;
234216

235217
void set(const bool& value)
236218
{

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/CMakeLists.txt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,17 +182,19 @@ if(BUILD_RPCS3_TESTS)
182182
target_sources(rpcs3_test
183183
PRIVATE
184184
tests/test.cpp
185+
tests/test_address_range.cpp
185186
tests/test_bit_set.cpp
187+
tests/test_cctype.cpp
188+
tests/test_dmux_pamf.cpp
186189
tests/test_fmt.cpp
187190
tests/test_pair.cpp
188-
tests/test_tuple.cpp
189-
tests/test_simple_array.cpp
190-
tests/test_address_range.cpp
191-
tests/test_sys_fs.cpp
192191
tests/test_rsx_cfg.cpp
193192
tests/test_rsx_fp_asm.cpp
194-
tests/test_dmux_pamf.cpp
193+
tests/test_rsx_mm_queue.cpp
194+
tests/test_simple_array.cpp
195195
tests/test_spu_analyser.cpp
196+
tests/test_sys_fs.cpp
197+
tests/test_tuple.cpp
196198
tests/test_types_util.cpp
197199
)
198200

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

0 commit comments

Comments
 (0)