mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 21:14:14 +08:00
Cast to unsigned char to avoid UB (#152360)
The standard requires that the argument to functions like `isdigit`, `isalpha`, and similar must be either `EOF` or an `unsigned char`; otherwise, the behavior is undefined (UB). To avoid out-of-bounds reads, modern implementations of some libraries (such as glibc) deliberately pad their internal tables to guarantee valid memory access even for negative values. However, this is implementation-specific, and other libraries may not do this. Properly casting the argument to `unsigned char` is good practice to avoid potential issues on some platforms. Pull Request resolved: https://github.com/pytorch/pytorch/pull/152360 Approved by: https://github.com/cyyever, https://github.com/Skylion007
This commit is contained in:
@ -25,9 +25,10 @@ bool Dimname::isValidName(const std::string& name) {
|
||||
}
|
||||
for (auto it = name.begin(); it != name.end(); ++it) {
|
||||
// NOLINTNEXTLINE(bugprone-branch-clone)
|
||||
if (std::isalpha(*it) || *it == '_') {
|
||||
const unsigned char ch = static_cast<unsigned char>(*it);
|
||||
if (std::isalpha(ch) || ch == '_') {
|
||||
continue;
|
||||
} else if (it != name.begin() && std::isdigit(*it)) {
|
||||
} else if (it != name.begin() && std::isdigit(ch)) {
|
||||
continue;
|
||||
}
|
||||
return false;
|
||||
|
@ -83,10 +83,11 @@ Device::Device(const std::string& device_string) : Device(Type::CPU) {
|
||||
pstate != DeviceStringParsingState::ERROR && i < device_string.size();
|
||||
++i) {
|
||||
const char ch = device_string.at(i);
|
||||
const unsigned char uch = static_cast<unsigned char>(ch);
|
||||
switch (pstate) {
|
||||
case DeviceStringParsingState::START:
|
||||
if (ch != ':') {
|
||||
if (isalpha(ch) || ch == '_') {
|
||||
if (std::isalpha(uch) || ch == '_') {
|
||||
device_name.push_back(ch);
|
||||
} else {
|
||||
pstate = DeviceStringParsingState::ERROR;
|
||||
@ -97,7 +98,7 @@ Device::Device(const std::string& device_string) : Device(Type::CPU) {
|
||||
break;
|
||||
|
||||
case DeviceStringParsingState::INDEX_START:
|
||||
if (isdigit(ch)) {
|
||||
if (std::isdigit(uch)) {
|
||||
device_index_str.push_back(ch);
|
||||
pstate = DeviceStringParsingState::INDEX_REST;
|
||||
} else {
|
||||
@ -110,7 +111,7 @@ Device::Device(const std::string& device_string) : Device(Type::CPU) {
|
||||
pstate = DeviceStringParsingState::ERROR;
|
||||
break;
|
||||
}
|
||||
if (isdigit(ch)) {
|
||||
if (std::isdigit(uch)) {
|
||||
device_index_str.push_back(ch);
|
||||
} else {
|
||||
pstate = DeviceStringParsingState::ERROR;
|
||||
|
Reference in New Issue
Block a user