mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-06 12:20:52 +01: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:
parent
4408701fed
commit
d88e0ceb64
|
|
@ -25,9 +25,10 @@ bool Dimname::isValidName(const std::string& name) {
|
||||||
}
|
}
|
||||||
for (auto it = name.begin(); it != name.end(); ++it) {
|
for (auto it = name.begin(); it != name.end(); ++it) {
|
||||||
// NOLINTNEXTLINE(bugprone-branch-clone)
|
// NOLINTNEXTLINE(bugprone-branch-clone)
|
||||||
if (std::isalpha(*it) || *it == '_') {
|
const unsigned char ch = static_cast<unsigned char>(*it);
|
||||||
|
if (std::isalpha(ch) || ch == '_') {
|
||||||
continue;
|
continue;
|
||||||
} else if (it != name.begin() && std::isdigit(*it)) {
|
} else if (it != name.begin() && std::isdigit(ch)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|
|
||||||
|
|
@ -83,10 +83,11 @@ Device::Device(const std::string& device_string) : Device(Type::CPU) {
|
||||||
pstate != DeviceStringParsingState::ERROR && i < device_string.size();
|
pstate != DeviceStringParsingState::ERROR && i < device_string.size();
|
||||||
++i) {
|
++i) {
|
||||||
const char ch = device_string.at(i);
|
const char ch = device_string.at(i);
|
||||||
|
const unsigned char uch = static_cast<unsigned char>(ch);
|
||||||
switch (pstate) {
|
switch (pstate) {
|
||||||
case DeviceStringParsingState::START:
|
case DeviceStringParsingState::START:
|
||||||
if (ch != ':') {
|
if (ch != ':') {
|
||||||
if (isalpha(ch) || ch == '_') {
|
if (std::isalpha(uch) || ch == '_') {
|
||||||
device_name.push_back(ch);
|
device_name.push_back(ch);
|
||||||
} else {
|
} else {
|
||||||
pstate = DeviceStringParsingState::ERROR;
|
pstate = DeviceStringParsingState::ERROR;
|
||||||
|
|
@ -97,7 +98,7 @@ Device::Device(const std::string& device_string) : Device(Type::CPU) {
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case DeviceStringParsingState::INDEX_START:
|
case DeviceStringParsingState::INDEX_START:
|
||||||
if (isdigit(ch)) {
|
if (std::isdigit(uch)) {
|
||||||
device_index_str.push_back(ch);
|
device_index_str.push_back(ch);
|
||||||
pstate = DeviceStringParsingState::INDEX_REST;
|
pstate = DeviceStringParsingState::INDEX_REST;
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -110,7 +111,7 @@ Device::Device(const std::string& device_string) : Device(Type::CPU) {
|
||||||
pstate = DeviceStringParsingState::ERROR;
|
pstate = DeviceStringParsingState::ERROR;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (isdigit(ch)) {
|
if (std::isdigit(uch)) {
|
||||||
device_index_str.push_back(ch);
|
device_index_str.push_back(ch);
|
||||||
} else {
|
} else {
|
||||||
pstate = DeviceStringParsingState::ERROR;
|
pstate = DeviceStringParsingState::ERROR;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user