diff --git a/src/backend/cuda/platform.cpp b/src/backend/cuda/platform.cpp index 0de2451c4d..84dddadbab 100644 --- a/src/backend/cuda/platform.cpp +++ b/src/backend/cuda/platform.cpp @@ -287,7 +287,11 @@ void devprop(char *d_name, char *d_platform, char *d_toolkit, char *d_compute) { const cudaDeviceProp &dev = getDeviceProp(getActiveDeviceId()); // Name - snprintf(d_name, 256, "%s", dev.name); + // af_device_info documents a recommended minimum size of 64 for d_name + // (see docs/details/device.dox, \defgroup device_func_prop), and the CPU + // and OpenCL backends honour it. Writing 256 bytes here overflowed + // conforming callers' buffers. + snprintf(d_name, 64, "%s", dev.name); // Platform string cudaRuntime = getCUDARuntimeVersion(); @@ -297,8 +301,10 @@ void devprop(char *d_name, char *d_platform, char *d_toolkit, char *d_compute) { // Compute Version snprintf(d_compute, 10, "%d.%d", dev.major, dev.minor); - // Sanitize input - for (int i = 0; i < 256; i++) { + // Sanitize input. The lookahead reads d_name[i + 1], so i must stop at + // 62 to stay inside the documented 64-byte buffer. Stopping at the + // terminator also avoids scanning past the end of a short device name. + for (int i = 0; i < 63 && d_name[i] != '\0'; i++) { if (d_name[i] == ' ') { if (d_name[i + 1] == 0 || d_name[i + 1] == ' ') { d_name[i] = 0; diff --git a/src/backend/opencl/platform.cpp b/src/backend/opencl/platform.cpp index b6886c97bb..b2d69f13ce 100644 --- a/src/backend/opencl/platform.cpp +++ b/src/backend/opencl/platform.cpp @@ -493,8 +493,9 @@ void devprop(char* d_name, char* d_platform, char* d_toolkit, char* d_compute) { } } - // Sanitize input - for (int i = 0; i < 31; i++) { + // Sanitize input. Stop at the terminator: the lookahead reads d_name[i + 1] + // and the loop must not run past the end of a short device name. + for (int i = 0; i < 63 && d_name[i] != '\0'; i++) { if (d_name[i] == ' ') { if (d_name[i + 1] == 0 || d_name[i + 1] == ' ') { d_name[i] = 0;