diff --git a/example/amd_smi_drm_example.cc b/example/amd_smi_drm_example.cc index 430bd304a5..7ed532da23 100644 --- a/example/amd_smi_drm_example.cc +++ b/example/amd_smi_drm_example.cc @@ -1233,8 +1233,8 @@ int main() { uint64_t mem = 0, gtt_mem = 0, cpu_mem = 0, vram_mem = 0; uint64_t gfx = 0, enc = 0; uint32_t cu_occupancy = 0; - char bdf_str[20]; - sprintf(bdf_str, "%04" PRIx64 ":%02" PRIx32 ":%02" PRIx32 ".%" PRIu32, + char bdf_str[64] = {0}; + snprintf(bdf_str, sizeof(bdf_str), "%04" PRIx64 ":%02" PRIx32 ":%02" PRIx32 ".%" PRIu32, static_cast(bdf.domain_number), static_cast(bdf.bus_number), static_cast(bdf.device_number), @@ -1265,7 +1265,7 @@ int main() { struct passwd *pwd = nullptr; struct stat st; - sprintf(command, "/proc/%d", process_info_list[it].pid); + snprintf(command, sizeof(command), "/proc/%d", process_info_list[it].pid); if (stat(command, &st)) continue; pwd = getpwuid(st.st_uid); diff --git a/rocm_smi/src/rocm_smi.cc b/rocm_smi/src/rocm_smi.cc index 8b24aa46a8..afaf4161e3 100644 --- a/rocm_smi/src/rocm_smi.cc +++ b/rocm_smi/src/rocm_smi.cc @@ -7360,9 +7360,14 @@ rsmi_event_notification_get(int timeout_ms, * Both event are expressed in hex. * information is a string */ - char message[MAX_EVENT_NOTIFICATION_MSG_SIZE]; + char message[MAX_EVENT_NOTIFICATION_MSG_SIZE] = {0}; // parse the line here for event_number and rest of message_information - sscanf(event_in, "%x %[^\n]\n", &event, message); + // sscanf(event_in, "%x %[^\n]\n", &event, message); // This is unsafe code and flagged by codeql. Replace with iss below: + std::istringstream iss(event_in); + iss >> std::hex >> event; + std::string message_str; + std::getline(iss >> std::ws, message_str); + snprintf(message, sizeof(message), "%s", message_str.c_str()); // parse message based on event received switch (event){ diff --git a/src/amd_smi/amd_smi.cc b/src/amd_smi/amd_smi.cc index 3c38b54d57..2c3b37b190 100644 --- a/src/amd_smi/amd_smi.cc +++ b/src/amd_smi/amd_smi.cc @@ -414,7 +414,7 @@ amdsmi_status_t amdsmi_get_socket_info( amdsmi_status_t amdsmi_get_processor_info( amdsmi_processor_handle processor_handle, size_t len, char *name) { - char proc_id[10]; + char proc_id[16] = {0}; AMDSMI_CHECK_INIT(); if (processor_handle == nullptr || name == nullptr) { @@ -426,7 +426,7 @@ amdsmi_status_t amdsmi_get_processor_info( .handle_to_processor(processor_handle, &processor); if (r != AMDSMI_STATUS_SUCCESS) return r; - sprintf(proc_id, "%d", processor->get_processor_index()); + snprintf(proc_id, sizeof(proc_id), "%d", processor->get_processor_index()); strncpy(name, proc_id, len); return AMDSMI_STATUS_SUCCESS;