From ff8ac680ade8860acd522076e13910185f4f7bef Mon Sep 17 00:00:00 2001 From: Adam Honse Date: Thu, 2 Mar 2023 00:14:17 -0600 Subject: [PATCH] Replace most instances of sprintf() with snprintf() --- .../AsusAuraTUFKeyboardController.cpp | 2 +- .../RGBController_CorsairLightingNode.cpp | 4 ++-- .../GaiZhongGaiKeyboardController.cpp | 2 +- .../GigabyteRGBFusion2GPUControllerDetect.cpp | 2 +- .../HyperXAlloyOriginsCoreController.cpp | 2 +- .../RoccatController/RoccatKovaController.cpp | 2 +- .../RGBController_SRGBmodsPico.cpp | 6 ++--- .../RGBController_ThermaltakeRiing.cpp | 4 ++-- .../RGBController_ThermaltakeRiingQuad.cpp | 4 ++-- .../RGBController_ZalmanZSync.cpp | 4 ++-- i2c_smbus/i2c_smbus_i801.cpp | 2 +- i2c_smbus/i2c_smbus_nct6775.cpp | 12 +++++----- i2c_smbus/i2c_smbus_nvapi.cpp | 4 ++-- i2c_tools/i2c_tools.cpp | 22 +++++++++---------- 14 files changed, 36 insertions(+), 36 deletions(-) diff --git a/Controllers/AsusAuraUSBController/AsusAuraTUFKeyboardController.cpp b/Controllers/AsusAuraUSBController/AsusAuraTUFKeyboardController.cpp index eb098792..b0be96b3 100644 --- a/Controllers/AsusAuraUSBController/AsusAuraTUFKeyboardController.cpp +++ b/Controllers/AsusAuraUSBController/AsusAuraTUFKeyboardController.cpp @@ -138,7 +138,7 @@ int AuraTUFKeyboardController::GetLayout() { char layout[4]; - sprintf(layout, "%X", version); + snprintf(layout, 4, "%X", version); int layoutnum = std::stoi(std::string(layout, 1)); diff --git a/Controllers/CorsairLightingNodeController/RGBController_CorsairLightingNode.cpp b/Controllers/CorsairLightingNodeController/RGBController_CorsairLightingNode.cpp index ee0941ce..5f754a48 100644 --- a/Controllers/CorsairLightingNodeController/RGBController_CorsairLightingNode.cpp +++ b/Controllers/CorsairLightingNodeController/RGBController_CorsairLightingNode.cpp @@ -240,7 +240,7 @@ void RGBController_CorsairLightingNode::SetupZones() for (unsigned int channel_idx = 0; channel_idx < CORSAIR_LIGHTING_NODE_NUM_CHANNELS; channel_idx++) { char ch_idx_string[2]; - sprintf(ch_idx_string, "%d", channel_idx + 1); + snprintf(ch_idx_string, 2, "%d", channel_idx + 1); zones[channel_idx].name = "Corsair Channel "; zones[channel_idx].name.append(ch_idx_string); @@ -264,7 +264,7 @@ void RGBController_CorsairLightingNode::SetupZones() for (unsigned int led_ch_idx = 0; led_ch_idx < zones[channel_idx].leds_count; led_ch_idx++) { char led_idx_string[4]; - sprintf(led_idx_string, "%d", led_ch_idx + 1); + snprintf(led_idx_string, 4, "%d", led_ch_idx + 1); led new_led; new_led.name = "Corsair Channel "; diff --git a/Controllers/GaiZongGaiKeyboardController/GaiZhongGaiKeyboardController.cpp b/Controllers/GaiZongGaiKeyboardController/GaiZhongGaiKeyboardController.cpp index 345aec04..a1b09c2e 100644 --- a/Controllers/GaiZongGaiKeyboardController/GaiZhongGaiKeyboardController.cpp +++ b/Controllers/GaiZongGaiKeyboardController/GaiZhongGaiKeyboardController.cpp @@ -18,7 +18,7 @@ GaiZhongGaiKeyboardController::GaiZhongGaiKeyboardController(hid_device* dev_han | Obtaining the Firmware Version | \*-----------------------------------------------------*/ char str[10]; - sprintf(str, "Ver%04X", info->release_number); + snprintf(str, 10, "Ver%04X", info->release_number); version = str; } diff --git a/Controllers/GigabyteRGBFusion2GPUController/GigabyteRGBFusion2GPUControllerDetect.cpp b/Controllers/GigabyteRGBFusion2GPUController/GigabyteRGBFusion2GPUControllerDetect.cpp index 7b9cd5e7..750f3a02 100644 --- a/Controllers/GigabyteRGBFusion2GPUController/GigabyteRGBFusion2GPUControllerDetect.cpp +++ b/Controllers/GigabyteRGBFusion2GPUController/GigabyteRGBFusion2GPUControllerDetect.cpp @@ -55,7 +55,7 @@ bool TestForGigabyteRGBFusion2GPUController(i2c_smbus_interface* bus, unsigned c // Assemble C-string with respons for debugging char str[32]; for(int idx = 0; idx < read_sz; ++idx) - sprintf(&str[5*idx], " 0x%02X", data_readpkt[idx]); + snprintf(&str[5*idx], 32, " 0x%02X", data_readpkt[idx]); LOG_DEBUG("[%s] at address 0x%02X invalid. Expected 0xAB [0x*] but received:%s", GIGABYTEGPU_CONTROLLER_NAME2, address, str); pass = false; } diff --git a/Controllers/HyperXKeyboardController/HyperXAlloyOriginsCoreController.cpp b/Controllers/HyperXKeyboardController/HyperXAlloyOriginsCoreController.cpp index 48ba75f0..2cb47232 100644 --- a/Controllers/HyperXKeyboardController/HyperXAlloyOriginsCoreController.cpp +++ b/Controllers/HyperXKeyboardController/HyperXAlloyOriginsCoreController.cpp @@ -27,7 +27,7 @@ HyperXAlloyOriginsCoreController::HyperXAlloyOriginsCoreController(hid_device* d memset(fw_version_buf, '\0', sizeof(fw_version_buf)); unsigned short version = dev_info->release_number; - sprintf(fw_version_buf, "%.2X.%.2X", (version & 0xFF00) >> 8, version & 0x00FF); + snprintf(fw_version_buf, 8, "%.2X.%.2X", (version & 0xFF00) >> 8, version & 0x00FF); firmware_version = fw_version_buf; } diff --git a/Controllers/RoccatController/RoccatKovaController.cpp b/Controllers/RoccatController/RoccatKovaController.cpp index d967635f..f9ff7051 100644 --- a/Controllers/RoccatController/RoccatKovaController.cpp +++ b/Controllers/RoccatController/RoccatKovaController.cpp @@ -122,7 +122,7 @@ void RoccatKovaController::FetchFirmwareVersion() uint8_t version = buf[ROCCAT_KOVA_FIRMWARE_VERSION_IDX]; char version_str[5] {00}; - sprintf(version_str, "%.2f", version / 100.); + snprintf(version_str, 5, "%.2f", version / 100.); firmware_version = version_str; } diff --git a/Controllers/SRGBmodsController/RGBController_SRGBmodsPico.cpp b/Controllers/SRGBmodsController/RGBController_SRGBmodsPico.cpp index 0d4e3785..b619a638 100644 --- a/Controllers/SRGBmodsController/RGBController_SRGBmodsPico.cpp +++ b/Controllers/SRGBmodsController/RGBController_SRGBmodsPico.cpp @@ -71,7 +71,7 @@ void RGBController_SRGBmodsPico::SetupZones() for(unsigned int channel_idx = 0; channel_idx < SRGBMODS_PICO_NUM_CHANNELS; channel_idx++) { char ch_idx_string[2]; - sprintf(ch_idx_string, "%d", channel_idx + 1); + snprintf(ch_idx_string, 2, "%d", channel_idx + 1); zones[channel_idx].name = "Channel "; zones[channel_idx].name.append(ch_idx_string); @@ -94,7 +94,7 @@ void RGBController_SRGBmodsPico::SetupZones() for(unsigned int led_ch_idx = 0; led_ch_idx < zones[channel_idx].leds_count; led_ch_idx++) { char led_idx_string[4]; - sprintf(led_idx_string, "%d", led_ch_idx + 1); + snprintf(led_idx_string, 4, "%d", led_ch_idx + 1); led new_led; new_led.name = "LED "; @@ -149,4 +149,4 @@ void RGBController_SRGBmodsPico::UpdateSingleLED(int led) void RGBController_SRGBmodsPico::DeviceUpdateMode() { DeviceUpdateLEDs(); -} \ No newline at end of file +} diff --git a/Controllers/ThermaltakeRiingController/RGBController_ThermaltakeRiing.cpp b/Controllers/ThermaltakeRiingController/RGBController_ThermaltakeRiing.cpp index 8627628d..6c9afa8e 100644 --- a/Controllers/ThermaltakeRiingController/RGBController_ThermaltakeRiing.cpp +++ b/Controllers/ThermaltakeRiingController/RGBController_ThermaltakeRiing.cpp @@ -147,7 +147,7 @@ void RGBController_ThermaltakeRiing::SetupZones() for (unsigned int channel_idx = 0; channel_idx < THERMALTAKE_NUM_CHANNELS; channel_idx++) { char ch_idx_string[2]; - sprintf(ch_idx_string, "%d", channel_idx + 1); + snprintf(ch_idx_string, 2, "%d", channel_idx + 1); zones[channel_idx].name = "Riing Channel "; zones[channel_idx].name.append(ch_idx_string); @@ -170,7 +170,7 @@ void RGBController_ThermaltakeRiing::SetupZones() for (unsigned int led_ch_idx = 0; led_ch_idx < zones[channel_idx].leds_count; led_ch_idx++) { char led_idx_string[3]; - sprintf(led_idx_string, "%d", led_ch_idx + 1); + snprintf(led_idx_string, 3, "%d", led_ch_idx + 1); led new_led; new_led.name = "Riing Channel "; diff --git a/Controllers/ThermaltakeRiingController/RGBController_ThermaltakeRiingQuad.cpp b/Controllers/ThermaltakeRiingController/RGBController_ThermaltakeRiingQuad.cpp index 3c7c2d9a..bf9790a2 100644 --- a/Controllers/ThermaltakeRiingController/RGBController_ThermaltakeRiingQuad.cpp +++ b/Controllers/ThermaltakeRiingController/RGBController_ThermaltakeRiingQuad.cpp @@ -74,7 +74,7 @@ void RGBController_ThermaltakeRiingQuad::SetupZones() for (unsigned int channel_idx = 0; channel_idx < THERMALTAKE_QUAD_NUM_CHANNELS; channel_idx++) { char ch_idx_string[2]; - sprintf(ch_idx_string, "%d", channel_idx + 1); + snprintf(ch_idx_string, 2, "%d", channel_idx + 1); zones[channel_idx].name = "Riing Channel "; zones[channel_idx].name.append(ch_idx_string); @@ -97,7 +97,7 @@ void RGBController_ThermaltakeRiingQuad::SetupZones() for (unsigned int led_ch_idx = 0; led_ch_idx < zones[channel_idx].leds_count; led_ch_idx++) { char led_idx_string[3]; - sprintf(led_idx_string, "%d", led_ch_idx + 1); + snprintf(led_idx_string, 3, "%d", led_ch_idx + 1); led new_led; new_led.name = "Riing Channel "; diff --git a/Controllers/ZalmanZSyncController/RGBController_ZalmanZSync.cpp b/Controllers/ZalmanZSyncController/RGBController_ZalmanZSync.cpp index 2f109bc1..2d8a2879 100644 --- a/Controllers/ZalmanZSyncController/RGBController_ZalmanZSync.cpp +++ b/Controllers/ZalmanZSyncController/RGBController_ZalmanZSync.cpp @@ -207,7 +207,7 @@ void RGBController_ZalmanZSync::SetupZones() for (unsigned int channel_idx = 0; channel_idx < ZALMAN_Z_SYNC_NUM_CHANNELS; channel_idx++) { char ch_idx_string[2]; - sprintf(ch_idx_string, "%d", channel_idx + 1); + snprintf(ch_idx_string, 2, "%d", channel_idx + 1); zones[channel_idx].name = "Channel "; zones[channel_idx].name.append(ch_idx_string); @@ -231,7 +231,7 @@ void RGBController_ZalmanZSync::SetupZones() for (unsigned int led_ch_idx = 0; led_ch_idx < zones[channel_idx].leds_count; led_ch_idx++) { char led_idx_string[4]; - sprintf(led_idx_string, "%d", led_ch_idx + 1); + snprintf(led_idx_string, 4, "%d", led_ch_idx + 1); led new_led; new_led.name = "LED "; diff --git a/i2c_smbus/i2c_smbus_i801.cpp b/i2c_smbus/i2c_smbus_i801.cpp index c321f459..9db5cd2f 100644 --- a/i2c_smbus/i2c_smbus_i801.cpp +++ b/i2c_smbus/i2c_smbus_i801.cpp @@ -663,7 +663,7 @@ bool i2c_smbus_i801_detect() return(false); } - sprintf(bus->device_name, "Intel(R) SMBus - %X", bus->pci_device); + snprintf(bus->device_name, 512, "Intel(R) SMBus - %X", bus->pci_device); ((i2c_smbus_i801 *)bus)->i801_smba = ReadConfigPortWord(0x20) & 0xFFFE; ResourceManager::get()->RegisterI2CBus(bus); diff --git a/i2c_smbus/i2c_smbus_nct6775.cpp b/i2c_smbus/i2c_smbus_nct6775.cpp index 03950231..02a481c5 100644 --- a/i2c_smbus/i2c_smbus_nct6775.cpp +++ b/i2c_smbus/i2c_smbus_nct6775.cpp @@ -115,7 +115,7 @@ s32 i2c_smbus_nct6775::nct6775_access(u16 addr, char read_write, u8 command, int len = 0; } - + WriteIoPortByte(SMBHSTCMD, NCT6775_WRITE_BLOCK); } else @@ -271,19 +271,19 @@ bool i2c_smbus_nct6775_detect() switch (val & SIO_ID_MASK) { case SIO_NCT5577_ID: - sprintf(bus->device_name, "Nuvoton NCT5577D SMBus at %X", smba); + snprintf(bus->device_name, 512, "Nuvoton NCT5577D SMBus at %X", smba); break; case SIO_NCT6102_ID: - sprintf(bus->device_name, "Nuvoton NCT6102D/NCT6106D SMBus at %X", smba); + snprintf(bus->device_name, 512, "Nuvoton NCT6102D/NCT6106D SMBus at %X", smba); break; case SIO_NCT6793_ID: - sprintf(bus->device_name, "Nuvoton NCT6793D SMBus at %X", smba); + snprintf(bus->device_name, 512, "Nuvoton NCT6793D SMBus at %X", smba); break; case SIO_NCT6796_ID: - sprintf(bus->device_name, "Nuvoton NCT6796D SMBus at %X", smba); + snprintf(bus->device_name, 512, "Nuvoton NCT6796D SMBus at %X", smba); break; case SIO_NCT6798_ID: - sprintf(bus->device_name, "Nuvoton NCT6798D SMBus at %X", smba); + snprintf(bus->device_name, 512, "Nuvoton NCT6798D SMBus at %X", smba); break; } diff --git a/i2c_smbus/i2c_smbus_nvapi.cpp b/i2c_smbus/i2c_smbus_nvapi.cpp index d78e0e08..befb4c50 100644 --- a/i2c_smbus/i2c_smbus_nvapi.cpp +++ b/i2c_smbus/i2c_smbus_nvapi.cpp @@ -103,7 +103,7 @@ s32 i2c_smbus_nvapi::i2c_smbus_xfer(u8 addr, char read_write, u8 command, int mo case I2C_SMBUS_WORD_DATA: data->word = (i2c_data.data[0] | (i2c_data.data[1] << 8)); break; - + case I2C_SMBUS_BLOCK_DATA: case I2C_SMBUS_I2C_BLOCK_DATA: data->block[0] = i2c_data.size; @@ -179,7 +179,7 @@ bool i2c_smbus_nvapi_detect() { i2c_smbus_nvapi * nvapi_bus = new i2c_smbus_nvapi(gpu_handles[gpu_idx]); - sprintf(nvapi_bus->device_name, "NVidia NvAPI I2C on GPU %d", gpu_idx); + snprintf(nvapi_bus->device_name, 512, "NVidia NvAPI I2C on GPU %d", gpu_idx); res = NvAPI_GPU_GetPCIIdentifiers(gpu_handles[gpu_idx], &device_id, &sub_system_id, &revision_id, &ext_device_id); diff --git a/i2c_tools/i2c_tools.cpp b/i2c_tools/i2c_tools.cpp index c775335b..e482a2c6 100644 --- a/i2c_tools/i2c_tools.cpp +++ b/i2c_tools/i2c_tools.cpp @@ -22,12 +22,12 @@ std::string i2c_detect(i2c_smbus_interface * bus, int mode) char line[128]; std::string text; - sprintf(line, " 0 1 2 3 4 5 6 7 8 9 a b c d e f\r\n"); + snprintf(line, 128, " 0 1 2 3 4 5 6 7 8 9 a b c d e f\r\n"); text.append(line); for (i = 0; i < 128; i += 16) { - sprintf(line, "%02x: ", i); + snprintf(line, 128, "%02x: ", i); text.append(line); for (j = 0; j < 16; j++) @@ -35,7 +35,7 @@ std::string i2c_detect(i2c_smbus_interface * bus, int mode) /* Skip unwanted addresses */ if (i+j < first || i+j > last) { - sprintf(line, " "); + snprintf(line, 128, " "); text.append(line); continue; } @@ -63,16 +63,16 @@ std::string i2c_detect(i2c_smbus_interface * bus, int mode) if (res < 0) { - sprintf(line, "-- "); + snprintf(line, 128, "-- "); text.append(line); } else { - sprintf(line, "%02x ", i + j); + snprintf(line, 128, "%02x ", i + j); text.append(line); } } - sprintf(line, "\r\n"); + snprintf(line, 128, "\r\n"); text.append(line); } @@ -100,21 +100,21 @@ std::string i2c_dump(i2c_smbus_interface * bus, unsigned char address) char line[128]; std::string text; - sprintf(line, " 0 1 2 3 4 5 6 7 8 9 a b c d e f\r\n"); + snprintf(line, 128, " 0 1 2 3 4 5 6 7 8 9 a b c d e f\r\n"); text.append(line); for (i = 0; i < 0xFF; i += 16) { - sprintf(line, "%04x: ", i + start); + snprintf(line, 128, "%04x: ", i + start); text.append(line); for (j = 0; j < 16; j++) { - sprintf(line, "%02x ", (unsigned char )bus->i2c_smbus_read_byte_data(address, start + i + j)); + snprintf(line, 128, "%02x ", (unsigned char )bus->i2c_smbus_read_byte_data(address, start + i + j)); text.append(line); } - sprintf(line, "\r\n"); + snprintf(line, 128, "\r\n"); text.append(line); } @@ -146,7 +146,7 @@ std::string i2c_read(i2c_smbus_interface * bus, unsigned char address, unsigned for(i = 0; i < size; i++) { - sprintf(line, "%02x ", (unsigned char)bus->i2c_smbus_read_byte(address)); + snprintf(line, 128, "%02x ", (unsigned char)bus->i2c_smbus_read_byte(address)); text.append(line); }