Files
pytorch/c10/cuda/CUDAFunctions.cpp
linhaifeng 7bc2a66ded [CUDA][BugFix] fix truncated error messages (#168942)
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
2025-11-28 04:58:46 +00:00

12 KiB