mirror of
https://github.com/zebrajr/pytorch.git
synced 2026-01-15 12:15:51 +00:00
Inspired by #168369
I found 9a38bb8622/c10/core/Device.h (L19)
When device indices (DeviceIndex) with value 0 are passed to TORCH_CHECK macros,they are interpreted as string terminators (\0), causing error messages to be truncated.
For example:
```cpp
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdint>
int8_t device = 0;
int main() {
std::cout << std::strlen((std::stringstream() << "Head" << device << "Tail").str().c_str()) << std::endl;
std::cout << std::strlen((std::stringstream() << "Head" << static_cast<int>(device) << "Tail").str().c_str()) << std::endl;
std::cout << std::strlen((std::stringstream() << "Head" << +device << "Tail").str().c_str()) << std::endl;
return 0;
}
```
output
```bash
4
9
9
```
Maybe we can use `+` instead of `static_cast<int>`, but it needs discussion.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/168942
Approved by: https://github.com/cyyever, https://github.com/eqy
12 KiB
12 KiB