diff --git a/CMakeLists.txt b/CMakeLists.txt index d111b9c25..99b69403e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.10) project(modular-psu-firmware) -set (CMAKE_CXX_STANDARD 11) +set (CMAKE_CXX_STANDARD 14) if(MSVC) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4 /wd4100 /wd4214 /wd4200 /wd4244 /wd4456 /wd4457 /wd4458 /wd4459 /wd4245 /wd4389 /wd4706 /wd4611 /wd4310") @@ -30,7 +30,7 @@ else() endif() if (UNIX) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fpermissive -pedantic -Wall") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fpermissive -pedantic") endif (UNIX) add_definitions(-DDEBUG) diff --git a/src/eez/gui/data.cpp b/src/eez/gui/data.cpp index 49ec29f49..2e1e488ea 100644 --- a/src/eez/gui/data.cpp +++ b/src/eez/gui/data.cpp @@ -56,7 +56,7 @@ bool compare_INT_value(const Value &a, const Value &b) { } void INT_value_to_text(const Value &value, char *text, int count) { - strcatInt(text, value.getInt()); + strcatInt(text, count, value.getInt()); } bool compare_UINT8_value(const Value &a, const Value &b) { @@ -64,7 +64,7 @@ bool compare_UINT8_value(const Value &a, const Value &b) { } void UINT8_value_to_text(const Value &value, char *text, int count) { - strcatUInt32(text, value.getUInt8()); + strcatUInt32(text, count, value.getUInt8()); } bool compare_UINT16_value(const Value &a, const Value &b) { @@ -72,7 +72,7 @@ bool compare_UINT16_value(const Value &a, const Value &b) { } void UINT16_value_to_text(const Value &value, char *text, int count) { - strcatUInt32(text, value.getUInt16()); + strcatUInt32(text, count, value.getUInt16()); } bool compare_UINT32_value(const Value &a, const Value &b) { @@ -80,7 +80,7 @@ bool compare_UINT32_value(const Value &a, const Value &b) { } void UINT32_value_to_text(const Value &value, char *text, int count) { - strcatUInt32(text, value.getUInt32()); + strcatUInt32(text, count, value.getUInt32()); } bool compare_FLOAT_value(const Value &a, const Value &b) { @@ -176,12 +176,12 @@ void FLOAT_value_to_text(const Value &value, char *text, int count) { } if (fixedDecimals) { - strcatFloat(text, floatValue, FLOAT_OPTIONS_GET_NUM_FIXED_DECIMALS(options)); + strcatFloat(text, count, floatValue, FLOAT_OPTIONS_GET_NUM_FIXED_DECIMALS(options)); } else { if (unit == UNIT_WATT || unit == UNIT_MILLI_WATT) { - strcatFloat(text, floatValue, 2); + strcatFloat(text, count, floatValue, 2); } else { - strcatFloat(text, floatValue); + strcatFloat(text, count, floatValue); } int n = strlen(text); diff --git a/src/eez/index.cpp b/src/eez/index.cpp index 419bee564..ba9f550a8 100644 --- a/src/eez/index.cpp +++ b/src/eez/index.cpp @@ -185,14 +185,18 @@ void Module::resetProfileToDefaults(uint8_t *buffer) { void Module::getProfileParameters(uint8_t *buffer) { auto parameters = (ProfileParameters *)buffer; - strcpy(parameters->label, label); + strncpy(parameters->label, label, sizeof(parameters->label)); + parameters->label[sizeof(parameters->label) - 1] = 0; + parameters->color = color; } void Module::setProfileParameters(uint8_t *buffer, bool mismatch, int recallOptions) { auto parameters = (ProfileParameters *)buffer; - strcpy(label, parameters->label); + strncpy(label, parameters->label, sizeof(label)); + label[sizeof(label) - 1] = 0; + color = parameters->color; } @@ -247,7 +251,7 @@ void Module::setLabel(const char *value, int length) { length = SLOT_LABEL_MAX_LENGTH; } strncpy(label, value, length); - label[length] = 0; + label[length - 1] = 0; } uint8_t Module::getColor() { @@ -888,11 +892,11 @@ Module *getModule(uint16_t moduleType) { void getModuleSerialInfo(uint8_t slotIndex, char *serialStr) { auto &module = *g_slots[slotIndex]; if (module.idw0 != 0 || module.idw1 != 0 || module.idw2 != 0) { - sprintf(serialStr, "%08X", (unsigned int)module.idw0); - sprintf(serialStr + 8, "%08X", (unsigned int)module.idw1); - sprintf(serialStr + 16, "%08X", (unsigned int)module.idw2); + snprintf(serialStr, 9, "%08X", (unsigned int)module.idw0); + snprintf(serialStr + 8, 9, "%08X", (unsigned int)module.idw1); + snprintf(serialStr + 16, 9, "%08X", (unsigned int)module.idw2); } else { - strcpy(serialStr, "N/A"); + strncpy(serialStr, "N/A", 8); } } diff --git a/src/eez/keyboard.cpp b/src/eez/keyboard.cpp index ba91009c7..7c65f3631 100644 --- a/src/eez/keyboard.cpp +++ b/src/eez/keyboard.cpp @@ -284,7 +284,8 @@ namespace gui { void data_usb_keyboard_state(DataOperationEnum operation, Cursor cursor, Value &value) { if (operation == DATA_OPERATION_GET) { - static char g_keyboardInfoStr[2][256] = { 0 }; + static const size_t KEYBOARD_INFO_STRING_SIZE = 256; + static char g_keyboardInfoStr[2][KEYBOARD_INFO_STRING_SIZE] = { 0 }; static KeyboardInfo g_latestKeyboardInfo; static MouseInfo g_latestMouseInfo; static int g_keyboardInfoStrIndex = 0; @@ -303,7 +304,7 @@ void data_usb_keyboard_state(DataOperationEnum operation, Cursor cursor, Value & str[0] = 0; if (g_keyboardInfo.state != 0) { - sprintf(str, "State=0x%02X", g_keyboardInfo.state); + snprintf(str, KEYBOARD_INFO_STRING_SIZE, "State=0x%02X", g_keyboardInfo.state); } if (g_keyboardInfo.lctrl) { @@ -338,36 +339,21 @@ void data_usb_keyboard_state(DataOperationEnum operation, Cursor cursor, Value & strcat(str, " RGUI"); } - if (g_keyboardInfo.keys[0]) { - sprintf(str + strlen(str), " 0x%02X", g_keyboardInfo.keys[0]); - } - - if (g_keyboardInfo.keys[1]) { - sprintf(str + strlen(str), " 0x%02X", g_keyboardInfo.keys[1]); - } - - if (g_keyboardInfo.keys[2]) { - sprintf(str + strlen(str), " 0x%02X", g_keyboardInfo.keys[2]); - } - - if (g_keyboardInfo.keys[3]) { - sprintf(str + strlen(str), " 0x%02X", g_keyboardInfo.keys[3]); - } - - if (g_keyboardInfo.keys[4]) { - sprintf(str + strlen(str), " 0x%02X", g_keyboardInfo.keys[4]); - } - - if (g_keyboardInfo.keys[5]) { - sprintf(str + strlen(str), " 0x%02X", g_keyboardInfo.keys[5]); + for (int i = 0; i < 6; i++) { + if (g_keyboardInfo.keys[0]) { + auto n = strlen(str); + snprintf(str + n, KEYBOARD_INFO_STRING_SIZE - n, " 0x%02X", g_keyboardInfo.keys[0]); + } } if (g_mouseInfo.x != 0) { - sprintf(str + strlen(str), " / X=%d", (int8_t)g_mouseInfo.x); + auto n = strlen(str); + snprintf(str + n, KEYBOARD_INFO_STRING_SIZE - n, " / X=%d", (int8_t)g_mouseInfo.x); } if (g_mouseInfo.y != 0) { - sprintf(str + strlen(str), " Y=%d", (int8_t)g_mouseInfo.y); + auto n = strlen(str); + snprintf(str + n, KEYBOARD_INFO_STRING_SIZE - n, " Y=%d", (int8_t)g_mouseInfo.y); } if (g_mouseInfo.button1) { diff --git a/src/eez/libs/sd_fat/stm32/sd_fat.cpp b/src/eez/libs/sd_fat/stm32/sd_fat.cpp index a5b555f35..49fe898f0 100644 --- a/src/eez/libs/sd_fat/stm32/sd_fat.cpp +++ b/src/eez/libs/sd_fat/stm32/sd_fat.cpp @@ -310,7 +310,7 @@ bool File::sync() { void File::print(float value, int numDecimalDigits) { char buffer[32]; - sprintf(buffer, "%.*f", numDecimalDigits, value); + snprintf(buffer, sizeof(buffer), "%.*f", numDecimalDigits, value); write((uint8_t *)buffer, strlen(buffer)); } diff --git a/src/eez/modules/bp3c/eeprom.cpp b/src/eez/modules/bp3c/eeprom.cpp index 0b0da071c..96e51c071 100644 --- a/src/eez/modules/bp3c/eeprom.cpp +++ b/src/eez/modules/bp3c/eeprom.cpp @@ -99,7 +99,7 @@ bool read(uint8_t slotIndex, uint8_t *buffer, uint16_t bufferSize, uint16_t addr #if defined(EEZ_PLATFORM_SIMULATOR) char fileName[20]; - sprintf(fileName, "EEPROM_SLOT%d.state", slotIndex + 1); + snprintf(fileName, sizeof(fileName), "EEPROM_SLOT%d.state", slotIndex + 1); char *filePath = getConfFilePath(fileName); FILE *fp = fopen(filePath, "r+b"); if (fp == NULL) { @@ -178,7 +178,7 @@ bool write(uint8_t slotIndex, const uint8_t *buffer, uint16_t bufferSize, uint16 #if defined(EEZ_PLATFORM_SIMULATOR) char fileName[20]; - sprintf(fileName, "EEPROM_SLOT%d.state", slotIndex + 1); + snprintf(fileName, sizeof(fileName), "EEPROM_SLOT%d.state", slotIndex + 1); char *filePath = getConfFilePath(fileName); FILE *fp = fopen(filePath, "r+b"); if (fp == NULL) { diff --git a/src/eez/modules/dib-mio168/dib-mio168.cpp b/src/eez/modules/dib-mio168/dib-mio168.cpp index 9319c6115..1bcda0f7f 100644 --- a/src/eez/modules/dib-mio168/dib-mio168.cpp +++ b/src/eez/modules/dib-mio168/dib-mio168.cpp @@ -346,7 +346,7 @@ struct DinChannel { WRITE_PROPERTY("din_pinSpeeds", parameters.pinSpeeds); for (int i = 0; i < 8; i++) { char propName[32]; - sprintf(propName, "din_pin%dLabel", i); + snprintf(propName, sizeof(propName), "din_pin%dLabel", i); WRITE_PROPERTY(propName, parameters.pinLabels + i * (CHANNEL_LABEL_MAX_LENGTH + 1)); } return true; @@ -357,7 +357,7 @@ struct DinChannel { READ_PROPERTY("din_pinSpeeds", parameters.pinSpeeds); for (int i = 0; i < 8; i++) { char propName[32]; - sprintf(propName, "din_pin%dLabel", i); + snprintf(propName, sizeof(propName), "din_pin%dLabel", i); READ_STRING_PROPERTY(propName, parameters.pinLabels + i * (CHANNEL_LABEL_MAX_LENGTH + 1), CHANNEL_LABEL_MAX_LENGTH); } return false; @@ -421,7 +421,7 @@ struct DoutChannel { WRITE_PROPERTY("dout_pinStates", parameters.pinStates); for (int i = 0; i < 8; i++) { char propName[32]; - sprintf(propName, "dout_pin%dLabel", i); + snprintf(propName, sizeof(propName), "dout_pin%dLabel", i); WRITE_PROPERTY(propName, parameters.pinLabels + i * (CHANNEL_LABEL_MAX_LENGTH + 1)); } return true; @@ -431,7 +431,7 @@ struct DoutChannel { READ_PROPERTY("dout_pinStates", parameters.pinStates); for (int i = 0; i < 8; i++) { char propName[32]; - sprintf(propName, "dout_pin%dLabel", i); + snprintf(propName, sizeof(propName), "dout_pin%dLabel", i); READ_STRING_PROPERTY(propName, parameters.pinLabels + i * (CHANNEL_LABEL_MAX_LENGTH + 1), CHANNEL_LABEL_MAX_LENGTH); } return false; @@ -552,22 +552,22 @@ struct AinChannel { bool writeProfileProperties(psu::profile::WriteContext &ctx, int i, ProfileParameters ¶meters) { char propName[32]; - sprintf(propName, "ain_%d_mode", i+1); + snprintf(propName, sizeof(propName), "ain_%d_mode", i+1); WRITE_PROPERTY(propName, parameters.mode); - sprintf(propName, "ain_%d_currentRange", i+1); + snprintf(propName, sizeof(propName), "ain_%d_currentRange", i+1); WRITE_PROPERTY(propName, parameters.currentRange); - sprintf(propName, "ain_%d_voltageRange", i + 1); + snprintf(propName, sizeof(propName), "ain_%d_voltageRange", i + 1); WRITE_PROPERTY(propName, parameters.voltageRange); - sprintf(propName, "ain_%d_currentNPLC", i + 1); + snprintf(propName, sizeof(propName), "ain_%d_currentNPLC", i + 1); WRITE_PROPERTY(propName, parameters.currentNPLC); - sprintf(propName, "ain_%d_voltageNPLC", i + 1); + snprintf(propName, sizeof(propName), "ain_%d_voltageNPLC", i + 1); WRITE_PROPERTY(propName, parameters.voltageNPLC); - sprintf(propName, "ain_%d_label", i+1); + snprintf(propName, sizeof(propName), "ain_%d_label", i+1); WRITE_PROPERTY(propName, parameters.label); return true; @@ -576,22 +576,22 @@ struct AinChannel { bool readProfileProperties(psu::profile::ReadContext &ctx, int i, ProfileParameters ¶meters) { char propName[32]; - sprintf(propName, "ain_%d_mode", i+1); + snprintf(propName, sizeof(propName), "ain_%d_mode", i+1); READ_PROPERTY(propName, parameters.mode); - sprintf(propName, "ain_%d_currentRange", i+1); + snprintf(propName, sizeof(propName), "ain_%d_currentRange", i+1); READ_PROPERTY(propName, parameters.currentRange); - sprintf(propName, "ain_%d_voltageRange", i + 1); + snprintf(propName, sizeof(propName), "ain_%d_voltageRange", i + 1); READ_PROPERTY(propName, parameters.voltageRange); - sprintf(propName, "ain_%d_currentNPLC", i + 1); + snprintf(propName, sizeof(propName), "ain_%d_currentNPLC", i + 1); READ_PROPERTY(propName, parameters.currentNPLC); - sprintf(propName, "ain_%d_voltageNPLC", i + 1); + snprintf(propName, sizeof(propName), "ain_%d_voltageNPLC", i + 1); READ_PROPERTY(propName, parameters.voltageNPLC); - sprintf(propName, "ain_%d_label", i+1); + snprintf(propName, sizeof(propName), "ain_%d_label", i+1); READ_STRING_PROPERTY(propName, parameters.label, CHANNEL_LABEL_MAX_LENGTH); return false; @@ -900,25 +900,25 @@ struct AoutDac7760Channel { bool writeProfileProperties(psu::profile::WriteContext &ctx, int i, ProfileParameters ¶meters) { char propName[32]; - sprintf(propName, "aout_dac7760_%d_outputEnabled", i+1); + snprintf(propName, sizeof(propName), "aout_dac7760_%d_outputEnabled", i+1); WRITE_PROPERTY(propName, parameters.outputEnabled); - sprintf(propName, "aout_dac7760_%d_mode", i+1); + snprintf(propName, sizeof(propName), "aout_dac7760_%d_mode", i+1); WRITE_PROPERTY(propName, parameters.mode); - sprintf(propName, "aout_dac7760_%d_currentRange", i+1); + snprintf(propName, sizeof(propName), "aout_dac7760_%d_currentRange", i+1); WRITE_PROPERTY(propName, parameters.currentRange); - sprintf(propName, "aout_dac7760_%d_voltageRange", i+1); + snprintf(propName, sizeof(propName), "aout_dac7760_%d_voltageRange", i+1); WRITE_PROPERTY(propName, parameters.voltageRange); - sprintf(propName, "aout_dac7760_%d_currentValue", i+1); + snprintf(propName, sizeof(propName), "aout_dac7760_%d_currentValue", i+1); WRITE_PROPERTY(propName, parameters.currentValue); - sprintf(propName, "aout_dac7760_%d_voltageValue", i+1); + snprintf(propName, sizeof(propName), "aout_dac7760_%d_voltageValue", i+1); WRITE_PROPERTY(propName, parameters.voltageValue); - sprintf(propName, "aout_dac7760_%d_label", i+1); + snprintf(propName, sizeof(propName), "aout_dac7760_%d_label", i+1); WRITE_PROPERTY(propName, parameters.label); return true; @@ -927,25 +927,25 @@ struct AoutDac7760Channel { bool readProfileProperties(psu::profile::ReadContext &ctx, int i, ProfileParameters ¶meters) { char propName[32]; - sprintf(propName, "aout_dac7760_%d_outputEnabled", i+1); + snprintf(propName, sizeof(propName), "aout_dac7760_%d_outputEnabled", i+1); READ_PROPERTY(propName, parameters.outputEnabled); - sprintf(propName, "aout_dac7760_%d_mode", i+1); + snprintf(propName, sizeof(propName), "aout_dac7760_%d_mode", i+1); READ_PROPERTY(propName, parameters.mode); - sprintf(propName, "aout_dac7760_%d_currentRange", i+1); + snprintf(propName, sizeof(propName), "aout_dac7760_%d_currentRange", i+1); READ_PROPERTY(propName, parameters.currentRange); - sprintf(propName, "aout_dac7760_%d_voltageRange", i+1); + snprintf(propName, sizeof(propName), "aout_dac7760_%d_voltageRange", i+1); READ_PROPERTY(propName, parameters.voltageRange); - sprintf(propName, "aout_dac7760_%d_currentValue", i+1); + snprintf(propName, sizeof(propName), "aout_dac7760_%d_currentValue", i+1); READ_PROPERTY(propName, parameters.currentValue); - sprintf(propName, "aout_dac7760_%d_voltageValue", i+1); + snprintf(propName, sizeof(propName), "aout_dac7760_%d_voltageValue", i+1); READ_PROPERTY(propName, parameters.voltageValue); - sprintf(propName, "aout_dac7760_%d_label", i+1); + snprintf(propName, sizeof(propName), "aout_dac7760_%d_label", i+1); READ_STRING_PROPERTY(propName, parameters.label, CHANNEL_LABEL_MAX_LENGTH); return false; @@ -1179,10 +1179,10 @@ struct AoutDac7563Channel { bool writeProfileProperties(psu::profile::WriteContext &ctx, int i, ProfileParameters ¶meters) { char propName[32]; - sprintf(propName, "aout_dac7563_%d_value", i+1); + snprintf(propName, sizeof(propName), "aout_dac7563_%d_value", i+1); WRITE_PROPERTY(propName, parameters.value); - sprintf(propName, "aout_dac7563_%d_label", i+1); + snprintf(propName, sizeof(propName), "aout_dac7563_%d_label", i+1); WRITE_PROPERTY(propName, parameters.label); return true; @@ -1191,10 +1191,10 @@ struct AoutDac7563Channel { bool readProfileProperties(psu::profile::ReadContext &ctx, int i, ProfileParameters ¶meters) { char propName[32]; - sprintf(propName, "aout_dac7563_%d_value", i+1); + snprintf(propName, sizeof(propName), "aout_dac7563_%d_value", i+1); READ_PROPERTY(propName, parameters.value); - sprintf(propName, "aout_dac7563_%d_label", i+1); + snprintf(propName, sizeof(propName), "aout_dac7563_%d_label", i+1); READ_STRING_PROPERTY(propName, parameters.label, CHANNEL_LABEL_MAX_LENGTH); return false; @@ -1265,13 +1265,13 @@ struct PwmChannel { bool writeProfileProperties(psu::profile::WriteContext &ctx, int i, ProfileParameters ¶meters) { char propName[32]; - sprintf(propName, "pwm_%d_freq", i+1); + snprintf(propName, sizeof(propName), "pwm_%d_freq", i+1); WRITE_PROPERTY(propName, parameters.freq); - sprintf(propName, "pwm_%d_duty", i+1); + snprintf(propName, sizeof(propName), "pwm_%d_duty", i+1); WRITE_PROPERTY(propName, parameters.duty); - sprintf(propName, "pwm_%d_label", i+1); + snprintf(propName, sizeof(propName), "pwm_%d_label", i+1); WRITE_PROPERTY(propName, parameters.label); return true; @@ -1280,13 +1280,13 @@ struct PwmChannel { bool readProfileProperties(psu::profile::ReadContext &ctx, int i, ProfileParameters ¶meters) { char propName[32]; - sprintf(propName, "pwm_%d_freq", i+1); + snprintf(propName, sizeof(propName), "pwm_%d_freq", i+1); READ_PROPERTY(propName, parameters.freq); - sprintf(propName, "pwm_%d_duty", i+1); + snprintf(propName, sizeof(propName), "pwm_%d_duty", i+1); READ_PROPERTY(propName, parameters.duty); - sprintf(propName, "pwm_%d_label", i+1); + snprintf(propName, sizeof(propName), "pwm_%d_label", i+1); READ_STRING_PROPERTY(propName, parameters.label, CHANNEL_LABEL_MAX_LENGTH); return false; @@ -1412,7 +1412,7 @@ struct Mio168Module : public Module { DlogRecordingStart nextDlogRecordingStart; DlogRecordingStart dlogRecordingStart; - uint8_t selectedPage = 0;; + uint8_t selectedPage = 0; Mio168Module() { assert(sizeof(Request) == sizeof(Response)); diff --git a/src/eez/modules/dib-prel6/dib-prel6.cpp b/src/eez/modules/dib-prel6/dib-prel6.cpp index b24470edc..f29840a3d 100644 --- a/src/eez/modules/dib-prel6/dib-prel6.cpp +++ b/src/eez/modules/dib-prel6/dib-prel6.cpp @@ -643,7 +643,7 @@ struct Prel6Module : public Module { for (int i = 0; i < NUM_RELAYS; i++) { char propName[16]; - sprintf(propName, "channelLabel%d", i+1); + snprintf(propName, sizeof(propName), "channelLabel%d", i+1); WRITE_PROPERTY(propName, parameters->relayLabels[i]); } @@ -660,7 +660,7 @@ struct Prel6Module : public Module { for (int i = 0; i < NUM_RELAYS; i++) { char propName[16]; - sprintf(propName, "channelLabel%d", i+1); + snprintf(propName, sizeof(propName), "channelLabel%d", i+1); READ_STRING_PROPERTY(propName, parameters->relayLabels[i], RELAY_LABEL_MAX_LENGTH); } diff --git a/src/eez/modules/dib-smx46/dib-smx46.cpp b/src/eez/modules/dib-smx46/dib-smx46.cpp index ac8ff925b..ca4bc2105 100644 --- a/src/eez/modules/dib-smx46/dib-smx46.cpp +++ b/src/eez/modules/dib-smx46/dib-smx46.cpp @@ -733,13 +733,13 @@ struct Smx46Module : public Module { for (int i = 0; i < NUM_COLUMNS; i++) { char propName[16]; - sprintf(propName, "xLabel%d", i+1); + snprintf(propName, sizeof(propName), "xLabel%d", i+1); WRITE_PROPERTY(propName, parameters->columnLabels[i]); } for (int i = 0; i < NUM_ROWS; i++) { char propName[16]; - sprintf(propName, "yLabel%d", i+1); + snprintf(propName, sizeof(propName), "yLabel%d", i+1); WRITE_PROPERTY(propName, parameters->rowLabels[i]); } @@ -765,13 +765,13 @@ struct Smx46Module : public Module { for (int i = 0; i < NUM_COLUMNS; i++) { char propName[16]; - sprintf(propName, "xLabel%d", i+1); + snprintf(propName, sizeof(propName), "xLabel%d", i+1); READ_STRING_PROPERTY(propName, parameters->columnLabels[i], MAX_SWITCH_MATRIX_LABEL_LENGTH); } for (int i = 0; i < NUM_ROWS; i++) { char propName[16]; - sprintf(propName, "yLabel%d", i+1); + snprintf(propName, sizeof(propName), "yLabel%d", i+1); READ_STRING_PROPERTY(propName, parameters->rowLabels[i], MAX_SWITCH_MATRIX_LABEL_LENGTH); } diff --git a/src/eez/modules/mcu/display.cpp b/src/eez/modules/mcu/display.cpp index 346bc2865..d28bf0772 100644 --- a/src/eez/modules/mcu/display.cpp +++ b/src/eez/modules/mcu/display.cpp @@ -336,7 +336,7 @@ bool isDirty() { // if (g_dirtyX1 <= g_dirtyX2 && g_dirtyY1 <= g_dirtyY2) { // // char msg[50]; - // // sprintf(msg, "%d x %d\n", g_dirtyX2 - g_dirtyX1 + 1, g_dirtyY2 - g_dirtyY1 + 1); + // // snprintf(msg, sizeof(msg), "%d x %d\n", g_dirtyX2 - g_dirtyX1 + 1, g_dirtyY2 - g_dirtyY1 + 1); // // Serial.println(msg); // return true; // } diff --git a/src/eez/modules/mcu/eeprom.cpp b/src/eez/modules/mcu/eeprom.cpp index db00654ab..2cbb64263 100644 --- a/src/eez/modules/mcu/eeprom.cpp +++ b/src/eez/modules/mcu/eeprom.cpp @@ -22,7 +22,11 @@ #include #include -#define USE_EEPROM (defined(EEZ_PLATFORM_STM32) && !CONF_SURVIVE_MODE) +#if defined(EEZ_PLATFORM_STM32) && !CONF_SURVIVE_MODE +#define USE_EEPROM 1 +#else +#define USE_EEPROM 0 +#endif #if defined(EEZ_PLATFORM_STM32) #include diff --git a/src/eez/modules/psu/devices.cpp b/src/eez/modules/psu/devices.cpp index 9b337cfd8..674c0d58d 100644 --- a/src/eez/modules/psu/devices.cpp +++ b/src/eez/modules/psu/devices.cpp @@ -149,7 +149,7 @@ bool getDevice(int deviceIndex, Device &device) { for (int channelIndex = 0; channelIndex < 6; channelIndex++) { if (deviceIndex == i++) { device.id = (DeviceId)(DEVICE_ID_CH1_TEMP + channelIndex); - sprintf(device.name, "CH%d temp", channelIndex + 1); + snprintf(device.name, sizeof(device.name), "CH%d temp", channelIndex + 1); if (channelIndex < CH_NUM) { device.installed = true; device.testResult = temp_sensor::sensors[temp_sensor::CH1 + channelIndex].g_testResult; @@ -164,7 +164,7 @@ bool getDevice(int deviceIndex, Device &device) { for (int channelIndex = 0; channelIndex < 6; channelIndex++) { if (deviceIndex == i++) { device.id = (DeviceId)(DEVICE_ID_CH1 + channelIndex); - sprintf(device.name, "CH%d", channelIndex + 1); + snprintf(device.name, sizeof(device.name), "CH%d", channelIndex + 1); if (channelIndex < CH_NUM) { device.installed = true; device.testResult = Channel::get(channelIndex).getTestResult(); @@ -179,7 +179,7 @@ bool getDevice(int deviceIndex, Device &device) { for (int slotIndex = 0; slotIndex < 3; slotIndex++) { if (deviceIndex == i++) { device.id = (DeviceId)(DEVICE_ID_SLOT1 + slotIndex); - sprintf(device.name, "SLOT%d", slotIndex + 1); + snprintf(device.name, sizeof(device.name), "SLOT%d", slotIndex + 1); if (slotIndex < NUM_SLOTS) { device.installed = g_slots[slotIndex]->moduleType != MODULE_TYPE_NONE; device.testResult = g_slots[slotIndex]->getTestResult(); diff --git a/src/eez/modules/psu/ethernet.cpp b/src/eez/modules/psu/ethernet.cpp index 1b55bbe4f..742bc8a5c 100644 --- a/src/eez/modules/psu/ethernet.cpp +++ b/src/eez/modules/psu/ethernet.cpp @@ -76,9 +76,9 @@ int SCPI_Error(scpi_t *context, int_fast16_t err) { scpi_result_t SCPI_Control(scpi_t *context, scpi_ctrl_name_t ctrl, scpi_reg_val_t val) { char outputBuffer[256]; if (SCPI_CTRL_SRQ == ctrl) { - sprintf(outputBuffer, "**SRQ: 0x%X (%d)\r\n", val, val); + snprintf(outputBuffer, sizeof(outputBuffer), "**SRQ: 0x%X (%d)\r\n", val, val); } else { - sprintf(outputBuffer, "**CTRL %02x: 0x%X (%d)\r\n", ctrl, val, val); + snprintf(outputBuffer, sizeof(outputBuffer), "**CTRL %02x: 0x%X (%d)\r\n", ctrl, val, val); } g_outputBufferWriter.write(outputBuffer, strlen(outputBuffer)); diff --git a/src/eez/modules/psu/event_queue.cpp b/src/eez/modules/psu/event_queue.cpp index 2f28e35e2..725a2eb8d 100644 --- a/src/eez/modules/psu/event_queue.cpp +++ b/src/eez/modules/psu/event_queue.cpp @@ -546,7 +546,7 @@ static bool writeToLog(QueueEvent *event, uint32_t &logOffset, int &eventType) { eventType = getEventType(event->eventId); char dateTimeAndEventTypeStr[32]; - sprintf(dateTimeAndEventTypeStr, "%04d-%02d-%02d %02d:%02d:%02d %s ", year, month, day, hour, minute, second, EVENT_TYPE_NAMES[eventType]); + snprintf(dateTimeAndEventTypeStr, sizeof(dateTimeAndEventTypeStr), "%04d-%02d-%02d %02d:%02d:%02d %s ", year, month, day, hour, minute, second, EVENT_TYPE_NAMES[eventType]); bool result = bufferedFile.write((const uint8_t *)dateTimeAndEventTypeStr, strlen(dateTimeAndEventTypeStr)); if (result) { @@ -556,7 +556,7 @@ static bool writeToLog(QueueEvent *event, uint32_t &logOffset, int &eventType) { const char *message = getEventMessage(event->eventId); if (event->channelIndex != -1) { char buffer[128]; - sprintf(buffer, message, event->channelIndex + 1); + snprintf(buffer, sizeof(buffer), message, event->channelIndex + 1); result = bufferedFile.write((const uint8_t *)buffer, strlen(buffer)); } else { result = bufferedFile.write((const uint8_t *)message, strlen(message)); @@ -779,7 +779,7 @@ static void readEvents(uint32_t fromPosition) { if (g_writeQueue[i].channelIndex == -1 || g_writeQueue[i].message) { strcpy(event.message, g_writeQueue[i].eventId == EVENT_DEBUG_TRACE || g_writeQueue[i].eventId == EVENT_INFO_TRACE ? g_writeQueue[i].message : getEventMessage(g_writeQueue[i].eventId)); } else { - sprintf(event.message, getEventMessage(g_writeQueue[i].eventId), g_writeQueue[i].channelIndex + 1); + snprintf(event.message, sizeof(event.message), getEventMessage(g_writeQueue[i].eventId), g_writeQueue[i].channelIndex + 1); } updateIsLongMessageText(event); event.logOffset = i; diff --git a/src/eez/modules/psu/gui/data.cpp b/src/eez/modules/psu/gui/data.cpp index 223cdea7b..f196065e5 100644 --- a/src/eez/modules/psu/gui/data.cpp +++ b/src/eez/modules/psu/gui/data.cpp @@ -691,7 +691,7 @@ bool compare_IP_ADDRESS_value(const Value &a, const Value &b) { } void IP_ADDRESS_value_to_text(const Value &value, char *text, int count) { - ipAddressToString(value.getUInt32(), text); + ipAddressToString(value.getUInt32(), text, count); } bool compare_PORT_value(const Value &a, const Value &b) { @@ -1026,7 +1026,7 @@ bool compare_SCPI_ERROR_value(const Value &a, const Value &b) { void SCPI_ERROR_value_to_text(const Value &value, char *text, int count) { if (value.getSecondInt16() != -1) { - sprintf(text, "Ch%d: ", value.getSecondInt16() + 1); + snprintf(text, count, "Ch%d: ", value.getSecondInt16() + 1); } strncpy(text + strlen(text), SCPI_ErrorTranslate(value.getFirstInt16()), count - 1 - strlen(text)); text[count - 1] = 0; @@ -1102,12 +1102,12 @@ void CALIBRATION_POINT_INFO_value_to_text(const Value &value, char *text, int co int numPoints = value.getSecondInt16(); if (currentPointIndex != -1) { - sprintf(text, "%d of %d", currentPointIndex + 1, numPoints); + snprintf(text, count, "%d of %d", currentPointIndex + 1, numPoints); } else { if (numPoints == 1) { strcpy(text, "1 point"); } else { - sprintf(text, "%d points", numPoints); + snprintf(text, count, "%d points", numPoints); } } } @@ -1117,7 +1117,7 @@ bool compare_ZOOM_value(const Value &a, const Value &b) { } void ZOOM_value_to_text(const Value &value, char *text, int count) { - sprintf(text, "\xb8 x%d", value.getInt()); + snprintf(text, count, "\xb8 x%d", value.getInt()); } bool compare_NUM_SELECTED_value(const Value &a, const Value &b) { @@ -1125,7 +1125,7 @@ bool compare_NUM_SELECTED_value(const Value &a, const Value &b) { } void NUM_SELECTED_value_to_text(const Value &value, char *text, int count) { - sprintf(text, "%d of %d selected", (int)value.getFirstUInt16(), (int)value.getSecondUInt16()); + snprintf(text, count, "%d of %d selected", (int)value.getFirstUInt16(), (int)value.getSecondUInt16()); } bool compare_CURRENT_DIRECTORY_TITLE_value(const Value &a, const Value &b) { @@ -1146,7 +1146,7 @@ void MASS_STORAGE_DEVICE_LABEL_value_to_text(const Value &value, char *text, int strcpy(text, "Master (0:)"); } else { int slotIndex = massStorageDevice - 1; - sprintf(text, "%s (%d:)", g_slots[slotIndex]->getLabelOrDefault(), massStorageDevice); + snprintf(text, count, "%s (%d:)", g_slots[slotIndex]->getLabelOrDefault(), massStorageDevice); } } diff --git a/src/eez/modules/psu/gui/edit_mode.cpp b/src/eez/modules/psu/gui/edit_mode.cpp index fda4ef39e..0becc5fdc 100644 --- a/src/eez/modules/psu/gui/edit_mode.cpp +++ b/src/eez/modules/psu/gui/edit_mode.cpp @@ -194,7 +194,7 @@ void getInfoText(char *infoText, int count) { if ((channel.channelIndex < 2 && channel_dispatcher::getCouplingType() != channel_dispatcher::COUPLING_TYPE_NONE) || channel.flags.trackingEnabled) { strcpy(infoText, "Set "); } else { - sprintf(infoText, "Set Ch%d ", getFocusCursor() + 1); + snprintf(infoText, count, "Set Ch%d ", getFocusCursor() + 1); } strcat(infoText, dataName); diff --git a/src/eez/modules/psu/gui/keypad.cpp b/src/eez/modules/psu/gui/keypad.cpp index 0950ea458..5b58fa65e 100644 --- a/src/eez/modules/psu/gui/keypad.cpp +++ b/src/eez/modules/psu/gui/keypad.cpp @@ -447,7 +447,7 @@ void NumericKeypad::init( reset(); if (value.getType() == VALUE_TYPE_IP_ADDRESS) { - ipAddressToString(value.getUInt32(), m_keypadText); + ipAddressToString(value.getUInt32(), m_keypadText, sizeof(m_keypadText)); m_cursorPosition = strlen(m_keypadText); m_state = BEFORE_DOT; } diff --git a/src/eez/modules/psu/gui/page_ch_settings.cpp b/src/eez/modules/psu/gui/page_ch_settings.cpp index 88f283041..544152493 100644 --- a/src/eez/modules/psu/gui/page_ch_settings.cpp +++ b/src/eez/modules/psu/gui/page_ch_settings.cpp @@ -728,7 +728,7 @@ void ChSettingsListsPage::edit() { min.toText(dwell, sizeof(dwell)); strcat(label, dwell); } else { - strcatFloat(label, options.min); + strcatFloat(label, sizeof(label), options.min); } strcat(label, "-"); if (dataId == DATA_ID_CHANNEL_LIST_DWELL) { @@ -736,9 +736,9 @@ void ChSettingsListsPage::edit() { max.toText(dwell, sizeof(dwell)); strcat(label, dwell); } else if (dataId == DATA_ID_CHANNEL_LIST_VOLTAGE) { - strcatVoltage(label, options.max); + strcatVoltage(label, sizeof(label), options.max); } else { - strcatCurrent(label, options.max); + strcatCurrent(label, sizeof(label), options.max); } strcat(label, "]: "); diff --git a/src/eez/modules/psu/gui/page_sys_settings.cpp b/src/eez/modules/psu/gui/page_sys_settings.cpp index bf80ff9f9..60fdc1c58 100644 --- a/src/eez/modules/psu/gui/page_sys_settings.cpp +++ b/src/eez/modules/psu/gui/page_sys_settings.cpp @@ -1270,7 +1270,7 @@ void SysSettingsRampAndDelayPage::drawRamp(const WidgetCursor &widgetCursor, int y2 -= yOffset; char label[20]; - sprintf(label, "Ch%d %s", channelIndex + 1, (drawVoltageRamps ? "U" : "I")); + snprintf(label, sizeof(label), "Ch%d %s", channelIndex + 1, (drawVoltageRamps ? "U" : "I")); auto tmp = g_channelIndex; g_channelIndex = channelIndex; diff --git a/src/eez/modules/psu/profile.cpp b/src/eez/modules/psu/profile.cpp index ff892e5cd..5debaeaa8 100644 --- a/src/eez/modules/psu/profile.cpp +++ b/src/eez/modules/psu/profile.cpp @@ -68,7 +68,7 @@ static List g_listsProfile10[CH_MAX]; static void loadProfileName(int location); static Parameters *getProfileParametersFromCache(int location); -static void getProfileFilePath(int location, char *filePath); +static void getProfileFilePath(int location, char *filePath, size_t filePathStrLength); static void resetProfileToDefaults(Parameters &profile); @@ -147,7 +147,7 @@ bool recallFromLocation(int location, int recallOptions, bool showProgress, int } char filePath[MAX_PATH_LENGTH]; - getProfileFilePath(location, filePath); + getProfileFilePath(location, filePath, sizeof(filePath)); Parameters profile; resetProfileToDefaults(profile); @@ -254,7 +254,7 @@ bool saveToLocation(int location, const char *name, bool showProgress, int *err) } char filePath[MAX_PATH_LENGTH]; - getProfileFilePath(location, filePath); + getProfileFilePath(location, filePath, sizeof(filePath)); Parameters profile; memset(&profile, 0, sizeof(Parameters)); @@ -284,7 +284,7 @@ bool saveToFile(const char *filePath, bool showProgress, int *err) { bool importFileToLocation(const char *filePath, int location, bool showProgress, int *err) { char profileFilePath[MAX_PATH_LENGTH]; - getProfileFilePath(location, profileFilePath); + getProfileFilePath(location, profileFilePath, sizeof(profileFilePath)); if (sd_card::copyFile(filePath, profileFilePath, true, err)) { loadProfileParametersToCache(location); return true; @@ -294,7 +294,7 @@ bool importFileToLocation(const char *filePath, int location, bool showProgress, bool exportLocationToFile(int location, const char *filePath, bool showProgress, int *err) { char profileFilePath[MAX_PATH_LENGTH]; - getProfileFilePath(location, profileFilePath); + getProfileFilePath(location, profileFilePath, sizeof(profileFilePath)); return sd_card::copyFile(profileFilePath, filePath, true, err); } @@ -309,7 +309,7 @@ bool deleteLocation(int location, bool showProgress, int *err) { g_profilesCache[location].flags.isValid = false; char filePath[MAX_PATH_LENGTH]; - getProfileFilePath(location, filePath); + getProfileFilePath(location, filePath, sizeof(filePath)); if (!sd_card::exists(filePath, err)) { return true; } @@ -379,7 +379,7 @@ bool setName(int location, const char *name, bool showProgress, int *err) { if (profileFromCache && profileFromCache->flags.isValid) { char filePath[MAX_PATH_LENGTH]; - getProfileFilePath(location, filePath); + getProfileFilePath(location, filePath, sizeof(filePath)); Parameters profile; resetProfileToDefaults(profile); @@ -447,7 +447,7 @@ void loadProfileParametersToCache(int location) { sendMessageToLowPriorityThread(THREAD_MESSAGE_LOAD_PROFILE, location); } else { char filePath[MAX_PATH_LENGTH]; - getProfileFilePath(location, filePath); + getProfileFilePath(location, filePath, sizeof(filePath)); int err; if (!loadProfileFromFile(filePath, g_profilesCache[location], nullptr, 0, false, &err)) { if (err != SCPI_ERROR_FILE_NOT_FOUND && err != SCPI_ERROR_MISSING_MASS_MEDIA) { @@ -463,7 +463,7 @@ void loadProfileParametersToCache(int location) { static void loadProfileName(int location) { char filePath[MAX_PATH_LENGTH]; - getProfileFilePath(location, filePath); + getProfileFilePath(location, filePath, sizeof(filePath)); int err; if (!loadProfileFromFile(filePath, g_profilesCache[location], nullptr, LOAD_PROFILE_FROM_FILE_OPTION_ONLY_NAME, false, &err)) { if (err != SCPI_ERROR_FILE_NOT_FOUND && err != SCPI_ERROR_MISSING_MASS_MEDIA) { @@ -489,10 +489,10 @@ static Parameters *getProfileParametersFromCache(int location) { //////////////////////////////////////////////////////////////////////////////// -static void getProfileFilePath(int location, char *filePath) { +static void getProfileFilePath(int location, char *filePath, size_t filePathStrLength) { strcpy(filePath, PROFILES_DIR); strcat(filePath, PATH_SEPARATOR); - strcatInt(filePath, location); + strcatInt(filePath, filePathStrLength - strlen(filePath), location); strcat(filePath, getExtensionFromFileType(FILE_TYPE_PROFILE)); } @@ -872,37 +872,37 @@ WriteContext::WriteContext(File &file_) bool WriteContext::group(const char *groupName) { char line[256 + 1]; - sprintf(line, "[%s]\n", groupName); + snprintf(line, sizeof(line), "[%s]\n", groupName); return file.write((uint8_t *)line, strlen(line)); } bool WriteContext::group(const char *groupNamePrefix, unsigned int index) { char line[256 + 1]; - sprintf(line, "[%s%d]\n", groupNamePrefix, index); + snprintf(line, sizeof(line), "[%s%d]\n", groupNamePrefix, index); return file.write((uint8_t *)line, strlen(line)); } bool WriteContext::property(const char *propertyName, int value) { char line[256 + 1]; - sprintf(line, "\t%s=%d\n", propertyName, (int)value); + snprintf(line, sizeof(line), "\t%s=%d\n", propertyName, (int)value); return file.write((uint8_t *)line, strlen(line)); } bool WriteContext::property(const char *propertyName, unsigned int value) { char line[256 + 1]; - sprintf(line, "\t%s=%u\n", propertyName, (unsigned int)value); + snprintf(line, sizeof(line), "\t%s=%u\n", propertyName, (unsigned int)value); return file.write((uint8_t *)line, strlen(line)); } bool WriteContext::property(const char *propertyName, float value) { char line[256 + 1]; - sprintf(line, "\t%s=%g\n", propertyName, value); + snprintf(line, sizeof(line), "\t%s=%g\n", propertyName, value); return file.write((uint8_t *)line, strlen(line)); } bool WriteContext::property(const char *propertyName, const char *str) { char line[256 + 1]; - sprintf(line, "\t%s=\"", propertyName); + snprintf(line, sizeof(line), "\t%s=\"", propertyName); if (!file.write((uint8_t *)line, strlen(line))) { return false; } @@ -937,7 +937,7 @@ bool WriteContext::property( float *currentList, uint16_t ¤tListLength ) { char line[256 + 1]; - sprintf(line, "\t%s=```\n", propertyName); + snprintf(line, sizeof(line), "\t%s=```\n", propertyName); if (!file.write((uint8_t *)line, strlen(line))) { return false; } @@ -947,7 +947,7 @@ bool WriteContext::property( return false; } - sprintf(line, "```\n"); + snprintf(line, sizeof(line), "```\n"); if (!file.write((uint8_t *)line, strlen(line))) { return false; } @@ -1144,7 +1144,7 @@ static bool saveProfileToFile(const char *filePath, Parameters &profile, List *l static void saveStateToProfile0(bool merge) { char filePath[MAX_PATH_LENGTH]; - getProfileFilePath(0, filePath); + getProfileFilePath(0, filePath, sizeof(filePath)); if (!merge) { memset(&g_profilesCache[0], 0, sizeof(Parameters)); diff --git a/src/eez/modules/psu/scpi/appl.cpp b/src/eez/modules/psu/scpi/appl.cpp index b89e346a2..c17f9dfa0 100644 --- a/src/eez/modules/psu/scpi/appl.cpp +++ b/src/eez/modules/psu/scpi/appl.cpp @@ -104,22 +104,22 @@ scpi_result_t scpi_cmd_applyQ(scpi_t *context) { } // return both current and voltage - sprintf(buffer, "CH%d:", channel->channelIndex + 1); - strcatVoltage(buffer, channel_dispatcher::getUMax(*channel)); + snprintf(buffer, sizeof(buffer), "CH%d:", channel->channelIndex + 1); + strcatVoltage(buffer, sizeof(buffer), channel_dispatcher::getUMax(*channel)); strcat(buffer, "/"); - strcatCurrent(buffer, channel_dispatcher::getIMax(*channel)); + strcatCurrent(buffer, sizeof(buffer), channel_dispatcher::getIMax(*channel)); strcat(buffer, ", "); - strcatFloat(buffer, channel_dispatcher::getUSet(*channel)); + strcatFloat(buffer, sizeof(buffer), channel_dispatcher::getUSet(*channel)); strcat(buffer, ", "); - strcatFloat(buffer, channel_dispatcher::getISet(*channel)); + strcatFloat(buffer, sizeof(buffer), channel_dispatcher::getISet(*channel)); } else { if (current_or_voltage == 0) { // return only current - strcatFloat(buffer, channel_dispatcher::getISet(*channel)); + strcatFloat(buffer, sizeof(buffer), channel_dispatcher::getISet(*channel)); } else { // return only voltage - strcatFloat(buffer, channel_dispatcher::getUSet(*channel)); + strcatFloat(buffer, sizeof(buffer), channel_dispatcher::getUSet(*channel)); } } diff --git a/src/eez/modules/psu/scpi/debug.cpp b/src/eez/modules/psu/scpi/debug.cpp index d0e7087df..ecc099530 100644 --- a/src/eez/modules/psu/scpi/debug.cpp +++ b/src/eez/modules/psu/scpi/debug.cpp @@ -196,16 +196,14 @@ scpi_result_t scpi_cmd_debugQ(scpi_t *context) { scpi_result_t scpi_cmd_debugOntimeQ(scpi_t *context) { #ifdef DEBUG char buffer[512] = { 0 }; - char *p = buffer; - sprintf(p, "power active: %d\n", int(ontime::g_mcuCounter.isActive() ? 1 : 0)); - p += strlen(p); + snprintf(buffer, sizeof(buffer), "power active: %d\n", int(ontime::g_mcuCounter.isActive() ? 1 : 0)); for (int i = 0; i < CH_NUM; ++i) { Channel &channel = Channel::get(i); - sprintf(p, "CH%d active: %d\n", channel.channelIndex + 1, int(ontime::g_mcuCounter.isActive() ? 1 : 0)); - p += strlen(p); + auto n = strlen(buffer); + snprintf(buffer + n, sizeof(buffer) - n, "CH%d active: %d\n", channel.channelIndex + 1, int(ontime::g_mcuCounter.isActive() ? 1 : 0)); } SCPI_ResultCharacters(context, buffer, strlen(buffer)); @@ -404,7 +402,7 @@ scpi_result_t scpi_cmd_debugIoexpQ(scpi_t *context) { scpi_result_t scpi_cmd_debugDcm220Q(scpi_t *context) { #if defined(EEZ_PLATFORM_STM32) char text[100]; - sprintf(text, "TODO"); + snprintf(text, sizeof(text), "TODO"); SCPI_ResultText(context, text); return SCPI_RES_OK; #else diff --git a/src/eez/modules/psu/scpi/diag.cpp b/src/eez/modules/psu/scpi/diag.cpp index 9d4920490..ccf76a088 100644 --- a/src/eez/modules/psu/scpi/diag.cpp +++ b/src/eez/modules/psu/scpi/diag.cpp @@ -58,13 +58,13 @@ static void printCalibrationValue(scpi_t *context, calibration::CalibrationBase for (unsigned int i = 0; i < value.configuration.numPoints; i++) { if (value.isPointSet[i]) { - sprintf(buffer, "%s_point%d_dac=%f", prefix, i + 1, value.configuration.points[i].dac); + snprintf(buffer, sizeof(buffer), "%s_point%d_dac=%f", prefix, i + 1, value.configuration.points[i].dac); SCPI_ResultText(context, buffer); - sprintf(buffer, "%s_point%d_data=%f", prefix, i + 1, value.configuration.points[i].value); + snprintf(buffer, sizeof(buffer), "%s_point%d_data=%f", prefix, i + 1, value.configuration.points[i].value); SCPI_ResultText(context, buffer); - sprintf(buffer, "%s_point%d_adc=%f", prefix, i + 1, value.configuration.points[i].adc); + snprintf(buffer, sizeof(buffer), "%s_point%d_adc=%f", prefix, i + 1, value.configuration.points[i].adc); SCPI_ResultText(context, buffer); } } @@ -86,19 +86,19 @@ void printCalibrationParameters(scpi_t *context, Unit unit, uint8_t currentRange char buffer[128] = { 0 }; - sprintf(buffer, "%s_cal_params_exists=%d", prefix, calParamsExists); + snprintf(buffer, sizeof(buffer), "%s_cal_params_exists=%d", prefix, calParamsExists); SCPI_ResultText(context, buffer); if (calParamsExists) { for (unsigned int i = 0; i < calibrationValue.numPoints; i++) { - sprintf(buffer, "%s_point%d_dac=%f", prefix, i + 1, calibrationValue.points[i].dac); + snprintf(buffer, sizeof(buffer), "%s_point%d_dac=%f", prefix, i + 1, calibrationValue.points[i].dac); SCPI_ResultText(context, buffer); - sprintf(buffer, "%s_point%d_data=%f", prefix, i + 1, calibrationValue.points[i].value); + snprintf(buffer, sizeof(buffer), "%s_point%d_data=%f", prefix, i + 1, calibrationValue.points[i].value); SCPI_ResultText(context, buffer); if (!isNaN(calibrationValue.points[i].adc)) { - sprintf(buffer, "%s_point%d_adc=%f", prefix, i + 1, calibrationValue.points[i].adc); + snprintf(buffer, sizeof(buffer), "%s_point%d_adc=%f", prefix, i + 1, calibrationValue.points[i].adc); SCPI_ResultText(context, buffer); } } @@ -121,19 +121,19 @@ scpi_result_t scpi_cmd_diagnosticInformationAdcQ(scpi_t *context) { char buffer[64] = { 0 }; strcpy(buffer, "U_SET="); - strcatVoltage(buffer, channel->u.mon_dac_last); + strcatVoltage(buffer, sizeof(buffer), channel->u.mon_dac_last); SCPI_ResultText(context, buffer); strcpy(buffer, "U_MON="); - strcatVoltage(buffer, channel->u.mon_last); + strcatVoltage(buffer, sizeof(buffer), channel->u.mon_last); SCPI_ResultText(context, buffer); strcpy(buffer, "I_SET="); - strcatCurrent(buffer, channel->i.mon_dac_last); + strcatCurrent(buffer, sizeof(buffer), channel->i.mon_dac_last); SCPI_ResultText(context, buffer); strcpy(buffer, "I_MON="); - strcatCurrent(buffer, channel->i.mon_last); + strcatCurrent(buffer, sizeof(buffer), channel->i.mon_last); SCPI_ResultText(context, buffer); return SCPI_RES_OK; @@ -150,7 +150,7 @@ scpi_result_t scpi_cmd_diagnosticInformationCalibrationQ(scpi_t *context) { if (calibration::isChannelCalibrating(*channel)) { if (calibration::g_editor.isRemarkSet()) { char buffer[128] = { 0 }; - sprintf(buffer, "remark=%s", calibration::g_editor.getRemark()); + snprintf(buffer, sizeof(buffer), "remark=%s", calibration::g_editor.getRemark()); SCPI_ResultText(context, buffer); } printCalibrationValue(context, calibration::g_editor, calibration::g_editor.getVoltage()); @@ -162,7 +162,7 @@ scpi_result_t scpi_cmd_diagnosticInformationCalibrationQ(scpi_t *context) { int year, month, day, hour, minute, second; datetime::breakTime(channel->cal_conf.calibrationDate, year, month, day, hour, minute, second); - sprintf(buffer, "remark=%04d-%02d-%02d %s", year, month, day, channel->cal_conf.calibrationRemark); + snprintf(buffer, sizeof(buffer), "remark=%04d-%02d-%02d %s", year, month, day, channel->cal_conf.calibrationRemark); SCPI_ResultText(context, buffer); printCalibrationParameters(context, UNIT_VOLT, -1, channel->isVoltageCalibrationExists(), channel->cal_conf.u); @@ -183,7 +183,7 @@ scpi_result_t scpi_cmd_diagnosticInformationCalibrationQ(scpi_t *context) { int year, month, day, hour, minute, second; datetime::breakTime(calConf.calibrationDate, year, month, day, hour, minute, second); - sprintf(buffer, "remark=%04d-%02d-%02d %s", year, month, day, calConf.calibrationRemark); + snprintf(buffer, sizeof(buffer), "remark=%04d-%02d-%02d %s", year, month, day, calConf.calibrationRemark); SCPI_ResultText(context, buffer); g_slots[slotAndSubchannelIndex.slotIndex]->isVoltageCalibrationExists(slotAndSubchannelIndex.subchannelIndex); @@ -205,40 +205,40 @@ scpi_result_t scpi_cmd_diagnosticInformationProtectionQ(scpi_t *context) { int channelIndex = channel->channelIndex + 1; // voltage - sprintf(buffer, "CH%d u_tripped=%d", channelIndex, (int)channel->ovp.flags.tripped); + snprintf(buffer, sizeof(buffer), "CH%d u_tripped=%d", channelIndex, (int)channel->ovp.flags.tripped); SCPI_ResultText(context, buffer); - sprintf(buffer, "CH%d u_state=%d", channelIndex, (int)channel->prot_conf.flags.u_state); + snprintf(buffer, sizeof(buffer), "CH%d u_state=%d", channelIndex, (int)channel->prot_conf.flags.u_state); SCPI_ResultText(context, buffer); - sprintf(buffer, "CH%d u_type=%d", channelIndex, (int)channel->prot_conf.flags.u_type); + snprintf(buffer, sizeof(buffer), "CH%d u_type=%d", channelIndex, (int)channel->prot_conf.flags.u_type); SCPI_ResultText(context, buffer); - sprintf(buffer, "CH%d u_delay=", channelIndex); - strcatDuration(buffer, channel->prot_conf.u_delay); + snprintf(buffer, sizeof(buffer), "CH%d u_delay=", channelIndex); + strcatDuration(buffer, sizeof(buffer), channel->prot_conf.u_delay); SCPI_ResultText(context, buffer); - sprintf(buffer, "CH%d u_level=", channelIndex); - strcatVoltage(buffer, channel->prot_conf.u_level); + snprintf(buffer, sizeof(buffer), "CH%d u_level=", channelIndex); + strcatVoltage(buffer, sizeof(buffer), channel->prot_conf.u_level); SCPI_ResultText(context, buffer); // current - sprintf(buffer, "CH%d i_tripped=%d", channelIndex, (int)channel->ocp.flags.tripped); + snprintf(buffer, sizeof(buffer), "CH%d i_tripped=%d", channelIndex, (int)channel->ocp.flags.tripped); SCPI_ResultText(context, buffer); - sprintf(buffer, "CH%d i_state=%d", channelIndex, (int)channel->prot_conf.flags.i_state); + snprintf(buffer, sizeof(buffer), "CH%d i_state=%d", channelIndex, (int)channel->prot_conf.flags.i_state); SCPI_ResultText(context, buffer); - sprintf(buffer, "CH%d i_delay=", channelIndex); - strcatDuration(buffer, channel->prot_conf.i_delay); + snprintf(buffer, sizeof(buffer), "CH%d i_delay=", channelIndex); + strcatDuration(buffer, sizeof(buffer), channel->prot_conf.i_delay); SCPI_ResultText(context, buffer); // power - sprintf(buffer, "CH%d p_tripped=%d", channelIndex, (int)channel->opp.flags.tripped); + snprintf(buffer, sizeof(buffer), "CH%d p_tripped=%d", channelIndex, (int)channel->opp.flags.tripped); SCPI_ResultText(context, buffer); - sprintf(buffer, "CH%d p_state=%d", channelIndex, (int)channel->prot_conf.flags.p_state); + snprintf(buffer, sizeof(buffer), "CH%d p_state=%d", channelIndex, (int)channel->prot_conf.flags.p_state); SCPI_ResultText(context, buffer); - sprintf(buffer, "CH%d p_delay=", channelIndex); - strcatDuration(buffer, channel->prot_conf.p_delay); + snprintf(buffer, sizeof(buffer), "CH%d p_delay=", channelIndex); + strcatDuration(buffer, sizeof(buffer), channel->prot_conf.p_delay); SCPI_ResultText(context, buffer); - sprintf(buffer, "CH%d p_level=", channelIndex); - strcatPower(buffer, channel->prot_conf.p_level); + snprintf(buffer, sizeof(buffer), "CH%d p_level=", channelIndex); + strcatPower(buffer, sizeof(buffer), channel->prot_conf.p_level); SCPI_ResultText(context, buffer); } @@ -247,18 +247,18 @@ scpi_result_t scpi_cmd_diagnosticInformationProtectionQ(scpi_t *context) { if (sensor.isInstalled()) { temperature::TempSensorTemperature &sensorTemperature = temperature::sensors[i]; - sprintf(buffer, "temp_%s_tripped=%d", sensor.name, (int)sensorTemperature.isTripped()); + snprintf(buffer, sizeof(buffer), "temp_%s_tripped=%d", sensor.name, (int)sensorTemperature.isTripped()); SCPI_ResultText(context, buffer); - sprintf(buffer, "temp_%s_state=%d", sensor.name, (int)sensorTemperature.prot_conf.state); + snprintf(buffer, sizeof(buffer), "temp_%s_state=%d", sensor.name, (int)sensorTemperature.prot_conf.state); SCPI_ResultText(context, buffer); - sprintf(buffer, "temp_%s_delay=", sensor.name); - strcatDuration(buffer, sensorTemperature.prot_conf.delay); + snprintf(buffer, sizeof(buffer), "temp_%s_delay=", sensor.name); + strcatDuration(buffer, sizeof(buffer), sensorTemperature.prot_conf.delay); SCPI_ResultText(context, buffer); - sprintf(buffer, "temp_%s_level=", sensor.name); - strcatFloat(buffer, sensorTemperature.prot_conf.level); + snprintf(buffer, sizeof(buffer), "temp_%s_level=", sensor.name); + strcatFloat(buffer, sizeof(buffer), sensorTemperature.prot_conf.level); strcat(buffer, " oC"); SCPI_ResultText(context, buffer); } @@ -279,7 +279,7 @@ scpi_result_t scpi_cmd_diagnosticInformationTestQ(scpi_t *context) { char buffer[128] = { 0 }; devices::Device device; for (int i = 0; devices::getDevice(i, device); ++i) { - sprintf(buffer, "%d, %s, %s, %s", + snprintf(buffer, sizeof(buffer), "%d, %s, %s, %s", device.testResult ? (int)device.testResult : TEST_SKIPPED, device.name, devices::getInstalledString(device.installed), devices::getTestResultString(device.testResult)); @@ -322,37 +322,37 @@ scpi_result_t scpi_cmd_diagnosticInformationRegsQ(scpi_t *context) { for (int i = 0; i < CH_NUM; i++) { Channel& channel = Channel::get(i); if (g_slots[channel.slotIndex]->moduleType == MODULE_TYPE_DCP405) { - sprintf(buffer + strlen(buffer), "CH%d:\n", i + 1); - - sprintf(buffer + strlen(buffer), "\tIOEXP:\n"); - sprintf(buffer + strlen(buffer), "\t\tIODIRA %X\n", (int)g_ioexpRegisters[i][0]); - sprintf(buffer + strlen(buffer), "\t\tIODIRB %X\n", (int)g_ioexpRegisters[i][1]); - sprintf(buffer + strlen(buffer), "\t\tIPOLA %X\n", (int)g_ioexpRegisters[i][2]); - sprintf(buffer + strlen(buffer), "\t\tIPOLB %X\n", (int)g_ioexpRegisters[i][3]); - sprintf(buffer + strlen(buffer), "\t\tGPINTENA %X\n", (int)g_ioexpRegisters[i][4]); - sprintf(buffer + strlen(buffer), "\t\tGPINTENB %X\n", (int)g_ioexpRegisters[i][5]); - sprintf(buffer + strlen(buffer), "\t\tDEFVALA %X\n", (int)g_ioexpRegisters[i][6]); - sprintf(buffer + strlen(buffer), "\t\tDEFVALB %X\n", (int)g_ioexpRegisters[i][7]); - sprintf(buffer + strlen(buffer), "\t\tINTCONA %X\n", (int)g_ioexpRegisters[i][8]); - sprintf(buffer + strlen(buffer), "\t\tINTCONB %X\n", (int)g_ioexpRegisters[i][9]); - sprintf(buffer + strlen(buffer), "\t\tIOCON %X\n", (int)g_ioexpRegisters[i][10]); - sprintf(buffer + strlen(buffer), "\t\tIOCON %X\n", (int)g_ioexpRegisters[i][11]); - sprintf(buffer + strlen(buffer), "\t\tGPPUA %X\n", (int)g_ioexpRegisters[i][12]); - sprintf(buffer + strlen(buffer), "\t\tGPPUB %X\n", (int)g_ioexpRegisters[i][13]); - sprintf(buffer + strlen(buffer), "\t\tINTFA %X\n", (int)g_ioexpRegisters[i][14]); - sprintf(buffer + strlen(buffer), "\t\tINTFB %X\n", (int)g_ioexpRegisters[i][15]); - sprintf(buffer + strlen(buffer), "\t\tINTCAPA %X\n", (int)g_ioexpRegisters[i][16]); - sprintf(buffer + strlen(buffer), "\t\tINTCAPB %X\n", (int)g_ioexpRegisters[i][17]); - sprintf(buffer + strlen(buffer), "\t\tGPIOA %X\n", (int)g_ioexpRegisters[i][18]); - sprintf(buffer + strlen(buffer), "\t\tGPIOB %X\n", (int)g_ioexpRegisters[i][19]); - sprintf(buffer + strlen(buffer), "\t\tOLATA %X\n", (int)g_ioexpRegisters[i][20]); - sprintf(buffer + strlen(buffer), "\t\tOLATB %X\n", (int)g_ioexpRegisters[i][21]); - - sprintf(buffer + strlen(buffer), "\tADC:\n"); - sprintf(buffer + strlen(buffer), "\t\tREG0 %X\n", (int)g_adcRegisters[i][0]); - sprintf(buffer + strlen(buffer), "\t\tREG1 %X\n", (int)g_adcRegisters[i][1]); - sprintf(buffer + strlen(buffer), "\t\tREG2 %X\n", (int)g_adcRegisters[i][2]); - sprintf(buffer + strlen(buffer), "\t\tREG3 %X\n", (int)g_adcRegisters[i][3]); + snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "CH%d:\n", i + 1); + + snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "\tIOEXP:\n"); + snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "\t\tIODIRA %X\n", (int)g_ioexpRegisters[i][0]); + snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "\t\tIODIRB %X\n", (int)g_ioexpRegisters[i][1]); + snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "\t\tIPOLA %X\n", (int)g_ioexpRegisters[i][2]); + snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "\t\tIPOLB %X\n", (int)g_ioexpRegisters[i][3]); + snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "\t\tGPINTENA %X\n", (int)g_ioexpRegisters[i][4]); + snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "\t\tGPINTENB %X\n", (int)g_ioexpRegisters[i][5]); + snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "\t\tDEFVALA %X\n", (int)g_ioexpRegisters[i][6]); + snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "\t\tDEFVALB %X\n", (int)g_ioexpRegisters[i][7]); + snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "\t\tINTCONA %X\n", (int)g_ioexpRegisters[i][8]); + snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "\t\tINTCONB %X\n", (int)g_ioexpRegisters[i][9]); + snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "\t\tIOCON %X\n", (int)g_ioexpRegisters[i][10]); + snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "\t\tIOCON %X\n", (int)g_ioexpRegisters[i][11]); + snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "\t\tGPPUA %X\n", (int)g_ioexpRegisters[i][12]); + snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "\t\tGPPUB %X\n", (int)g_ioexpRegisters[i][13]); + snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "\t\tINTFA %X\n", (int)g_ioexpRegisters[i][14]); + snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "\t\tINTFB %X\n", (int)g_ioexpRegisters[i][15]); + snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "\t\tINTCAPA %X\n", (int)g_ioexpRegisters[i][16]); + snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "\t\tINTCAPB %X\n", (int)g_ioexpRegisters[i][17]); + snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "\t\tGPIOA %X\n", (int)g_ioexpRegisters[i][18]); + snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "\t\tGPIOB %X\n", (int)g_ioexpRegisters[i][19]); + snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "\t\tOLATA %X\n", (int)g_ioexpRegisters[i][20]); + snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "\t\tOLATB %X\n", (int)g_ioexpRegisters[i][21]); + + snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "\tADC:\n"); + snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "\t\tREG0 %X\n", (int)g_adcRegisters[i][0]); + snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "\t\tREG1 %X\n", (int)g_adcRegisters[i][1]); + snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "\t\tREG2 %X\n", (int)g_adcRegisters[i][2]); + snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "\t\tREG3 %X\n", (int)g_adcRegisters[i][3]); } } diff --git a/src/eez/modules/psu/scpi/inst.cpp b/src/eez/modules/psu/scpi/inst.cpp index 417f1eb64..f7db86dc4 100644 --- a/src/eez/modules/psu/scpi/inst.cpp +++ b/src/eez/modules/psu/scpi/inst.cpp @@ -84,7 +84,7 @@ scpi_result_t scpi_cmd_instrumentSelectQ(scpi_t *context) { for (int i = 0; i < psu_context->selectedChannels.numChannels; i++) { char buffer[20]; - sprintf(buffer, "(@%d%02d)", psu_context->selectedChannels.channels[i].slotIndex + 1, psu_context->selectedChannels.channels[i].subchannelIndex + 1); + snprintf(buffer, sizeof(buffer), "(@%d%02d)", psu_context->selectedChannels.channels[i].slotIndex + 1, psu_context->selectedChannels.channels[i].subchannelIndex + 1); SCPI_ResultCharacters(context, buffer, strlen(buffer)); } @@ -382,7 +382,7 @@ void dumpCatalog(scpi_t *context, bool dumpIndex) { if (CH_NUM > 0) { for (int channelIndex = 0; channelIndex < CH_NUM; channelIndex++) { char channelStr[10]; - sprintf(channelStr, "CH%d", channelIndex + 1); + snprintf(channelStr, sizeof(channelStr), "CH%d", channelIndex + 1); SCPI_ResultText(context, channelStr); if (dumpIndex) { SCPI_ResultInt(context, channelIndex + 1); @@ -397,7 +397,7 @@ void dumpCatalog(scpi_t *context, bool dumpIndex) { int subchannelIndex = module->getSubchannelIndexFromRelativeChannelIndex(relativeChannelIndex); char channelStr[10]; - sprintf(channelStr, "(@%d%02d)", slotIndex + 1, subchannelIndex + 1); + snprintf(channelStr, sizeof(channelStr), "(@%d%02d)", slotIndex + 1, subchannelIndex + 1); SCPI_ResultText(context, channelStr); if (dumpIndex) { SCPI_ResultInt(context, 0); diff --git a/src/eez/modules/psu/scpi/meas.cpp b/src/eez/modules/psu/scpi/meas.cpp index 47d62dd88..1ee6158c4 100644 --- a/src/eez/modules/psu/scpi/meas.cpp +++ b/src/eez/modules/psu/scpi/meas.cpp @@ -41,7 +41,7 @@ scpi_result_t scpi_cmd_measureScalarCurrentDcQ(scpi_t *context) { } char buffer[256] = { 0 }; - strcatFloat(buffer, value); + strcatFloat(buffer, sizeof(buffer), value); SCPI_ResultCharacters(context, buffer, strlen(buffer)); return SCPI_RES_OK; @@ -67,7 +67,7 @@ scpi_result_t scpi_cmd_measureScalarPowerDcQ(scpi_t *context) { } char buffer[256] = { 0 }; - strcatFloat(buffer, voltage * current); + strcatFloat(buffer, sizeof(buffer), voltage * current); SCPI_ResultCharacters(context, buffer, strlen(buffer)); return SCPI_RES_OK; @@ -87,7 +87,7 @@ scpi_result_t scpi_cmd_measureScalarVoltageDcQ(scpi_t *context) { } char buffer[256] = { 0 }; - strcatFloat(buffer, value); + strcatFloat(buffer, sizeof(buffer), value); SCPI_ResultCharacters(context, buffer, strlen(buffer)); return SCPI_RES_OK; diff --git a/src/eez/modules/psu/scpi/mmem.cpp b/src/eez/modules/psu/scpi/mmem.cpp index 1dc11f438..d08044190 100644 --- a/src/eez/modules/psu/scpi/mmem.cpp +++ b/src/eez/modules/psu/scpi/mmem.cpp @@ -109,7 +109,7 @@ void catalogCallback(void *param, const char *name, FileType type, size_t size, buffer[position++] = ','; // max. 10 characters (for 4294967296) - sprintf(buffer + position, "%lu", (unsigned long)size); + snprintf(buffer + position, sizeof(buffer) - position, "%lu", (unsigned long)size); SCPI_ResultText(context, buffer); } @@ -486,7 +486,7 @@ scpi_result_t scpi_cmd_mmemoryDateQ(scpi_t *context) { } char buffer[16] = { 0 }; - sprintf(buffer, "%d, %d, %d", (int)(year + 2000), (int)month, (int)day); + snprintf(buffer, sizeof(buffer), "%d, %d, %d", (int)(year + 2000), (int)month, (int)day); SCPI_ResultCharacters(context, buffer, strlen(buffer)); return SCPI_RES_OK; @@ -508,7 +508,7 @@ scpi_result_t scpi_cmd_mmemoryTimeQ(scpi_t *context) { } char buffer[16] = { 0 }; - sprintf(buffer, "%d, %d, %d", (int)hour, (int)minute, (int)second); + snprintf(buffer, sizeof(buffer), "%d, %d, %d", (int)hour, (int)minute, (int)second); SCPI_ResultCharacters(context, buffer, strlen(buffer)); return SCPI_RES_OK; diff --git a/src/eez/modules/psu/scpi/params.cpp b/src/eez/modules/psu/scpi/params.cpp index 1922c968c..c56754c5d 100644 --- a/src/eez/modules/psu/scpi/params.cpp +++ b/src/eez/modules/psu/scpi/params.cpp @@ -765,7 +765,7 @@ bool get_power_limit_from_param(scpi_t *context, const scpi_number_t ¶m, flo scpi_result_t result_float(scpi_t *context, float value, Unit unit) { char buffer[32] = { 0 }; - strcatFloat(buffer, value); + strcatFloat(buffer, sizeof(buffer), value); SCPI_ResultCharacters(context, buffer, strlen(buffer)); return SCPI_RES_OK; } diff --git a/src/eez/modules/psu/scpi/syst.cpp b/src/eez/modules/psu/scpi/syst.cpp index 9498e3761..a644d2c47 100644 --- a/src/eez/modules/psu/scpi/syst.cpp +++ b/src/eez/modules/psu/scpi/syst.cpp @@ -142,7 +142,7 @@ scpi_result_t scpi_cmd_systemDateQ(scpi_t *context) { } char buffer[16] = { 0 }; - sprintf(buffer, "%d, %d, %d", (int)(year + 2000), (int)month, (int)day); + snprintf(buffer, sizeof(buffer), "%d, %d, %d", (int)(year + 2000), (int)month, (int)day); SCPI_ResultCharacters(context, buffer, strlen(buffer)); return SCPI_RES_OK; @@ -190,7 +190,7 @@ scpi_result_t scpi_cmd_systemTimeQ(scpi_t *context) { } char buffer[16] = { 0 }; - sprintf(buffer, "%d, %d, %d", (int)hour, (int)minute, (int)second); + snprintf(buffer, sizeof(buffer), "%d, %d, %d", (int)hour, (int)minute, (int)second); SCPI_ResultCharacters(context, buffer, strlen(buffer)); return SCPI_RES_OK; @@ -438,7 +438,7 @@ scpi_result_t scpi_cmd_systemSlotVersionQ(scpi_t *context) { if (module->moduleType != MODULE_TYPE_NONE) { char text[50]; - sprintf(text, "R%dB%d", (int)(module->moduleRevision >> 8), (int)(module->moduleRevision & 0xFF)); + snprintf(text, sizeof(text), "R%dB%d", (int)(module->moduleRevision >> 8), (int)(module->moduleRevision & 0xFF)); SCPI_ResultText(context, text); } else { SCPI_ResultText(context, ""); @@ -455,7 +455,7 @@ scpi_result_t scpi_cmd_systemSlotFirmwareQ(scpi_t *context) { if (module->moduleType != MODULE_TYPE_NONE) { char text[50]; - sprintf(text, "%d.%d", (int)(module->firmwareMajorVersion), (int)(module->firmwareMinorVersion)); + snprintf(text, sizeof(text), "%d.%d", (int)(module->firmwareMajorVersion), (int)(module->firmwareMinorVersion)); SCPI_ResultText(context, text); } else { SCPI_ResultText(context, ""); @@ -657,7 +657,7 @@ scpi_result_t scpi_cmd_systemChannelVersionQ(scpi_t *context) { auto &slot = *g_slots[channel->slotIndex]; char text[50]; - sprintf(text, "R%dB%d", (int)(slot.moduleRevision >> 8), (int)(slot.moduleRevision & 0xFF)); + snprintf(text, sizeof(text), "R%dB%d", (int)(slot.moduleRevision >> 8), (int)(slot.moduleRevision & 0xFF)); SCPI_ResultText(context, text); return SCPI_RES_OK; @@ -695,7 +695,7 @@ scpi_result_t scpi_cmd_systemCpuModelQ(scpi_t *context) { scpi_result_t scpi_cmd_systemCpuVersionQ(scpi_t *context) { char revision[32]; - snprintf(revision, 32, "R%dB%d", g_mcuRevision >> 8, g_mcuRevision & 0xFF); + snprintf(revision, sizeof(revision), "R%dB%d", g_mcuRevision >> 8, g_mcuRevision & 0xFF); SCPI_ResultText(context, revision); return SCPI_RES_OK; } @@ -1085,9 +1085,9 @@ scpi_result_t scpi_cmd_systemCommunicateEthernetAddressQ(scpi_t *context) { char ipAddressStr[16]; if (persist_conf::devConf.ethernetDhcpEnabled) { - ipAddressToString(ethernet::getIpAddress(), ipAddressStr); + ipAddressToString(ethernet::getIpAddress(), ipAddressStr, sizeof(ipAddressStr)); } else { - ipAddressToString(persist_conf::devConf.ethernetIpAddress, ipAddressStr); + ipAddressToString(persist_conf::devConf.ethernetIpAddress, ipAddressStr, sizeof(ipAddressStr)); } SCPI_ResultText(context, ipAddressStr); return SCPI_RES_OK; @@ -1137,7 +1137,7 @@ scpi_result_t scpi_cmd_systemCommunicateEthernetDnsQ(scpi_t *context) { SCPI_ResultText(context, "unknown"); } else { char ipAddressStr[16]; - ipAddressToString(persist_conf::devConf.ethernetDns, ipAddressStr); + ipAddressToString(persist_conf::devConf.ethernetDns, ipAddressStr, sizeof(ipAddressStr)); SCPI_ResultText(context, ipAddressStr); } return SCPI_RES_OK; @@ -1187,7 +1187,7 @@ scpi_result_t scpi_cmd_systemCommunicateEthernetGatewayQ(scpi_t *context) { SCPI_ResultText(context, "unknown"); } else { char ipAddressStr[16]; - ipAddressToString(persist_conf::devConf.ethernetGateway, ipAddressStr); + ipAddressToString(persist_conf::devConf.ethernetGateway, ipAddressStr, sizeof(ipAddressStr)); SCPI_ResultText(context, ipAddressStr); } return SCPI_RES_OK; @@ -1288,7 +1288,7 @@ scpi_result_t scpi_cmd_systemCommunicateEthernetSmaskQ(scpi_t *context) { SCPI_ResultText(context, "unknown"); } else { char ipAddressStr[16]; - ipAddressToString(persist_conf::devConf.ethernetSubnetMask, ipAddressStr); + ipAddressToString(persist_conf::devConf.ethernetSubnetMask, ipAddressStr, sizeof(ipAddressStr)); SCPI_ResultText(context, ipAddressStr); } return SCPI_RES_OK; @@ -1993,7 +1993,7 @@ scpi_result_t scpi_cmd_systemMeasureScalarTemperatureThermistorDcQ(scpi_t *conte } char buffer[256] = { 0 }; - strcatFloat(buffer, temperature::sensors[sensor].measure()); + strcatFloat(buffer, sizeof(buffer), temperature::sensors[sensor].measure()); SCPI_ResultCharacters(context, buffer, strlen(buffer)); return SCPI_RES_OK; diff --git a/src/eez/modules/psu/sd_card.cpp b/src/eez/modules/psu/sd_card.cpp index 2e33ddf38..a81e906dd 100644 --- a/src/eez/modules/psu/sd_card.cpp +++ b/src/eez/modules/psu/sd_card.cpp @@ -742,7 +742,7 @@ bool BufferedFileWrite::write(const uint8_t *buf, size_t size) { bool BufferedFileWrite::print(float value, int numDecimalDigits) { char buf[32]; - sprintf(buf, "%.*f", numDecimalDigits, value); + snprintf(buf, sizeof(buf), "%.*f", numDecimalDigits, value); int len = strlen(buf); return write((const uint8_t *)buf, len); } diff --git a/src/eez/modules/psu/serial.cpp b/src/eez/modules/psu/serial.cpp index 694970b5b..700cafc68 100644 --- a/src/eez/modules/psu/serial.cpp +++ b/src/eez/modules/psu/serial.cpp @@ -113,7 +113,7 @@ int UARTClass::print(const char *data) { int UARTClass::println(const char *data) { #if defined(EEZ_PLATFORM_STM32) char buffer[1024]; - int size = sprintf(buffer, "%s\n", data); + int size = snprintf(buffer, sizeof(buffer), "%s\n", data); return write(buffer, size); #endif @@ -125,7 +125,7 @@ int UARTClass::println(const char *data) { int UARTClass::print(int value) { #if defined(EEZ_PLATFORM_STM32) char buffer[1024]; - int size = sprintf(buffer, "%d", value); + int size = snprintf(buffer, sizeof(buffer), "%d", value); return write(buffer, size); #endif @@ -137,7 +137,7 @@ int UARTClass::print(int value) { int UARTClass::println(int value) { #if defined(EEZ_PLATFORM_STM32) char buffer[1024]; - int size = sprintf(buffer, "%d\n", value); + int size = snprintf(buffer, sizeof(buffer), "%d\n", value); return write(buffer, size); #endif @@ -150,7 +150,7 @@ int UARTClass::print(float value, int numDigits) { #if defined(EEZ_PLATFORM_STM32) // TODO numDigits char buffer[1024]; - int size = sprintf(buffer, "%f", value); + int size = snprintf(buffer, sizeof(buffer), "%f", value); return write(buffer, size); #endif @@ -163,7 +163,7 @@ int UARTClass::println(float value, int numDigits) { #if defined(EEZ_PLATFORM_STM32) // TODO numDigits char buffer[1024]; - int size = sprintf(buffer, "%f\n", value); + int size = snprintf(buffer, sizeof(buffer), "%f\n", value); return write(buffer, size); #endif diff --git a/src/eez/modules/psu/serial_psu.cpp b/src/eez/modules/psu/serial_psu.cpp index a1871bc45..42ead6a0e 100644 --- a/src/eez/modules/psu/serial_psu.cpp +++ b/src/eez/modules/psu/serial_psu.cpp @@ -67,9 +67,9 @@ scpi_result_t SCPI_Control(scpi_t *context, scpi_ctrl_name_t ctrl, scpi_reg_val_ if (serial::g_testResult == TEST_OK) { char errorOutputBuffer[256]; if (SCPI_CTRL_SRQ == ctrl) { - sprintf(errorOutputBuffer, "**SRQ: 0x%X (%d)\r\n", val, val); + snprintf(errorOutputBuffer, sizeof(errorOutputBuffer), "**SRQ: 0x%X (%d)\r\n", val, val); } else { - sprintf(errorOutputBuffer, "**CTRL %02x: 0x%X (%d)\r\n", ctrl, val, val); + snprintf(errorOutputBuffer, sizeof(errorOutputBuffer), "**CTRL %02x: 0x%X (%d)\r\n", ctrl, val, val); } g_outputBufferWriter.write(errorOutputBuffer, strlen(errorOutputBuffer)); } diff --git a/src/eez/mp.cpp b/src/eez/mp.cpp index ac0ec6956..eb9892850 100644 --- a/src/eez/mp.cpp +++ b/src/eez/mp.cpp @@ -248,7 +248,9 @@ void oneIter() { void startScript(const char *filePath) { if (g_state == STATE_IDLE) { g_state = STATE_EXECUTING; - strcpy(g_scriptPath, filePath); + strncpy(g_scriptPath, filePath, sizeof(g_scriptPath)); + g_scriptPath[sizeof(g_scriptPath) - 1] = 0; + //DebugTrace("T1 %d\n", millis()); sendMessageToLowPriorityThread(MP_LOAD_SCRIPT); diff --git a/src/eez/mqtt.cpp b/src/eez/mqtt.cpp index 8f8cf1c0e..ab69a4e48 100644 --- a/src/eez/mqtt.cpp +++ b/src/eez/mqtt.cpp @@ -426,37 +426,38 @@ bool publish(char *topic, char *payload, bool retain) { bool publish(const char *pubTopic, int value, bool retain) { char topic[MAX_PUB_TOPIC_LENGTH + 1]; - sprintf(topic, pubTopic, persist_conf::devConf.ethernetHostName); + snprintf(topic, sizeof(topic), pubTopic, persist_conf::devConf.ethernetHostName); char payload[MAX_PAYLOAD_LENGTH + 1]; - sprintf(payload, "%d", value); + snprintf(payload, sizeof(payload), "%d", value); return publish(topic, payload, retain); } bool publishOnTimeCounter(const char *pubTopic, uint32_t value, bool retain) { char topic[MAX_PUB_TOPIC_LENGTH + 1]; - sprintf(topic, pubTopic, persist_conf::devConf.ethernetHostName); + snprintf(topic, sizeof(topic), pubTopic, persist_conf::devConf.ethernetHostName); char payload[MAX_PAYLOAD_LENGTH + 1]; ontime::counterToString(payload, MAX_PAYLOAD_LENGTH, value); + payload[MAX_PAYLOAD_LENGTH] = 0; return publish(topic, payload, retain); } bool publish(const char *pubTopic, float value, bool retain) { char topic[MAX_PUB_TOPIC_LENGTH + 1]; - sprintf(topic, pubTopic, persist_conf::devConf.ethernetHostName); + snprintf(topic, sizeof(topic), pubTopic, persist_conf::devConf.ethernetHostName); char payload[MAX_PAYLOAD_LENGTH + 1]; - sprintf(payload, "%g", value); + snprintf(payload, sizeof(payload), "%g", value); return publish(topic, payload, retain); } bool publishEvent(int16_t eventId, bool retain) { char topic[MAX_PUB_TOPIC_LENGTH + 1]; - sprintf(topic, PUB_TOPIC_SYSTEM_EVENT, persist_conf::devConf.ethernetHostName); + snprintf(topic, sizeof(topic), PUB_TOPIC_SYSTEM_EVENT, persist_conf::devConf.ethernetHostName); char payload[MAX_PAYLOAD_LENGTH + 1]; snprintf(payload, MAX_PAYLOAD_LENGTH, "[%d, \"%s\", \"%s\"]", (int)eventId, event_queue::getEventTypeName(eventId), event_queue::getEventMessage(eventId)); @@ -467,18 +468,18 @@ bool publishEvent(int16_t eventId, bool retain) { bool publishFanStatus(const char *pubTopic, TestResult fanTestResult, int rpm, bool retain) { char topic[MAX_PUB_TOPIC_LENGTH + 1]; - sprintf(topic, pubTopic, persist_conf::devConf.ethernetHostName); + snprintf(topic, sizeof(topic), pubTopic, persist_conf::devConf.ethernetHostName); char payload[MAX_PAYLOAD_LENGTH + 1]; if (fanTestResult == TEST_FAILED || fanTestResult == TEST_WARNING) { - strcpy(payload, "Fault"); + snprintf(payload, sizeof(payload), "Fault"); } else if (fanTestResult == TEST_OK) { - snprintf(payload, MAX_PAYLOAD_LENGTH, "%d rpm", rpm); + snprintf(payload, sizeof(payload), "%d rpm", rpm); } else if (fanTestResult == TEST_NONE) { - strcpy(payload, "Testing..."); + snprintf(payload, sizeof(payload), "Testing..."); } else { - strcpy(payload, "Not installed"); + snprintf(payload, sizeof(payload), "Not installed"); } payload[MAX_PAYLOAD_LENGTH] = 0; @@ -488,34 +489,34 @@ bool publishFanStatus(const char *pubTopic, TestResult fanTestResult, int rpm, b bool publish(int channelIndex, const char *pubTopic, int value, bool retain) { char topic[MAX_PUB_TOPIC_LENGTH + 1]; - sprintf(topic, pubTopic, persist_conf::devConf.ethernetHostName, channelIndex + 1); + snprintf(topic, sizeof(topic), pubTopic, persist_conf::devConf.ethernetHostName, channelIndex + 1); char payload[MAX_PAYLOAD_LENGTH + 1]; - sprintf(payload, "%d", value); + snprintf(payload, sizeof(payload), "%d", value); return publish(topic, payload, retain); } bool publish(int channelIndex, const char *pubTopic, float value, bool retain) { char topic[MAX_PUB_TOPIC_LENGTH + 1]; - sprintf(topic, pubTopic, persist_conf::devConf.ethernetHostName, channelIndex + 1); + snprintf(topic, sizeof(topic), pubTopic, persist_conf::devConf.ethernetHostName, channelIndex + 1); char payload[MAX_PAYLOAD_LENGTH + 1]; - sprintf(payload, "%g", value); + snprintf(payload, sizeof(payload), "%g", value); return publish(topic, payload, retain); } bool publish(int channelIndex, const char *pubTopic, char *payload, bool retain) { char topic[MAX_PUB_TOPIC_LENGTH + 1]; - sprintf(topic, pubTopic, persist_conf::devConf.ethernetHostName, channelIndex + 1); + snprintf(topic, sizeof(topic), pubTopic, persist_conf::devConf.ethernetHostName, channelIndex + 1); return publish(topic, payload, retain); } bool publishOnTimeCounter(int channelIndex, const char *pubTopic, uint32_t value, bool retain) { char topic[MAX_PUB_TOPIC_LENGTH + 1]; - sprintf(topic, pubTopic, persist_conf::devConf.ethernetHostName, channelIndex + 1); + snprintf(topic, sizeof(topic), pubTopic, persist_conf::devConf.ethernetHostName, channelIndex + 1); char payload[MAX_PAYLOAD_LENGTH + 1]; ontime::counterToString(payload, MAX_PAYLOAD_LENGTH, value); @@ -528,11 +529,11 @@ const char *getClientId() { if (!g_clientId[0]) { #if defined(EEZ_PLATFORM_STM32) - sprintf(g_clientId, "BB3_STM32_%s", getSerialNumber()); + snprintf(g_clientId, sizeof(g_clientId), "BB3_STM32_%s", getSerialNumber()); #endif #if defined(EEZ_PLATFORM_SIMULATOR) - sprintf(g_clientId, "BB3_Simulator_%s", getSerialNumber()); + snprintf(g_clientId, sizeof(g_clientId), "BB3_Simulator_%s", getSerialNumber()); #endif } @@ -713,7 +714,7 @@ void tick() { if (!g_channelStates[channelIndex].modelPublished) { char moduleInfo[50]; auto &slot = *g_slots[channel.slotIndex]; - sprintf(moduleInfo, "%s_R%dB%d", slot.moduleName, (int)(slot.moduleRevision >> 8), (int)(slot.moduleRevision & 0xFF)); + snprintf(moduleInfo, sizeof(moduleInfo), "%s_R%dB%d", slot.moduleName, (int)(slot.moduleRevision >> 8), (int)(slot.moduleRevision & 0xFF)); if (publish(channelIndex, PUB_TOPIC_DCPSUPPLY_MODEL, moduleInfo, true)) { g_channelStates[channelIndex].modelPublished = true; } @@ -870,7 +871,7 @@ void tick() { #if defined(EEZ_PLATFORM_SIMULATOR) char port[16]; - sprintf(port, "%d", persist_conf::devConf.mqttPort); + snprintf(port, sizeof(port), "%d", persist_conf::devConf.mqttPort); g_sockfd = open_nb_socket(persist_conf::devConf.mqttHost, port); if (g_sockfd != -1) { /* initialize the client */ diff --git a/src/eez/system.cpp b/src/eez/system.cpp index f741fa4ce..a8909b52d 100644 --- a/src/eez/system.cpp +++ b/src/eez/system.cpp @@ -363,9 +363,9 @@ const char *getSerialNumber() { uint32_t idw2 = 0x00000001; #endif - sprintf(g_serialNumber, "%08X", (unsigned int)idw0); - sprintf(g_serialNumber + 8, "%08X", (unsigned int)idw1); - sprintf(g_serialNumber + 16, "%08X", (unsigned int)idw2); + snprintf(g_serialNumber, 9, "%08X", (unsigned int)idw0); + snprintf(g_serialNumber + 8, 9, "%08X", (unsigned int)idw1); + snprintf(g_serialNumber + 16, 9, "%08X", (unsigned int)idw2); g_serialNumber[24] = 0; } diff --git a/src/eez/tasks.cpp b/src/eez/tasks.cpp index dbbb923ad..128412385 100644 --- a/src/eez/tasks.cpp +++ b/src/eez/tasks.cpp @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -#include // sprintf +#include // snprintf #if defined(EEZ_PLATFORM_STM32) #include @@ -327,26 +327,26 @@ void lowPriorityThreadOneIter() { uint8_t year, month, day, hour, minute, second; datetime::getDateTime(year, month, day, hour, minute, second); if (persist_conf::devConf.dateTimeFormat == datetime::FORMAT_DMY_24) { - sprintf(filePath, "%s/%02d_%02d_%02d-%02d_%02d_%02d.jpg", + snprintf(filePath, sizeof(filePath), "%s/%02d_%02d_%02d-%02d_%02d_%02d.jpg", SCREENSHOTS_DIR, (int)day, (int)month, (int)year, (int)hour, (int)minute, (int)second); } else if (persist_conf::devConf.dateTimeFormat == datetime::FORMAT_MDY_24) { - sprintf(filePath, "%s/%02d_%02d_%02d-%02d_%02d_%02d.jpg", + snprintf(filePath, sizeof(filePath), "%s/%02d_%02d_%02d-%02d_%02d_%02d.jpg", SCREENSHOTS_DIR, (int)month, (int)day, (int)year, (int)hour, (int)minute, (int)second); } else if (persist_conf::devConf.dateTimeFormat == datetime::FORMAT_DMY_12) { bool am; datetime::convertTime24to12(hour, am); - sprintf(filePath, "%s/%02d_%02d_%02d-%02d_%02d_%02d_%s.jpg", + snprintf(filePath, sizeof(filePath), "%s/%02d_%02d_%02d-%02d_%02d_%02d_%s.jpg", SCREENSHOTS_DIR, (int)day, (int)month, (int)year, (int)hour, (int)minute, (int)second, am ? "AM" : "PM"); } else if (persist_conf::devConf.dateTimeFormat == datetime::FORMAT_MDY_12) { bool am; datetime::convertTime24to12(hour, am); - sprintf(filePath, "%s/%02d_%02d_%02d-%02d_%02d_%02d_%s.jpg", + snprintf(filePath, sizeof(filePath), "%s/%02d_%02d_%02d-%02d_%02d_%02d_%s.jpg", SCREENSHOTS_DIR, (int)month, (int)day, (int)year, (int)hour, (int)minute, (int)second, am ? "AM" : "PM"); diff --git a/src/eez/util.cpp b/src/eez/util.cpp index 2df15387d..1882b1c59 100644 --- a/src/eez/util.cpp +++ b/src/eez/util.cpp @@ -93,63 +93,60 @@ float clamp(float x, float min, float max) { return x; } -void strcatInt(char *str, int value) { - str = str + strlen(str); - sprintf(str, "%d", value); +void strcatInt(char *str, size_t maxStrLength, int value) { + auto n = strlen(str); + snprintf(str + n, maxStrLength - n, "%d", value); } -void strcatInt32(char *str, int32_t value) { - str = str + strlen(str); - sprintf(str, "%ld", (long)value); +void strcatUInt32(char *str, size_t maxStrLength, uint32_t value) { + auto n = strlen(str); + snprintf(str + n, maxStrLength - n, "%lu", (unsigned long)value); } -void strcatUInt32(char *str, uint32_t value) { - str = str + strlen(str); - sprintf(str, "%lu", (unsigned long)value); +void strcatFloat(char *str, size_t maxStrLength, float value) { + auto n = strlen(str); + snprintf(str + n, maxStrLength - n, "%g", value); } -void strcatFloat(char *str, float value) { - sprintf(str + strlen(str), "%g", value); +void strcatFloat(char *str, size_t maxStrLength, float value, int numDecimalPlaces) { + auto n = strlen(str); + snprintf(str + n, maxStrLength - n, "%.*f", numDecimalPlaces, value); } -void strcatFloat(char *str, float value, int numDecimalPlaces) { - sprintf(str + strlen(str), "%.*f", numDecimalPlaces, value); -} - -void strcatVoltage(char *str, float value) { - strcatFloat(str, value); +void strcatVoltage(char *str, size_t maxStrLength, float value) { + strcatFloat(str, maxStrLength, value); strcat(str, "V"); } -void strcatCurrent(char *str, float value) { - strcatFloat(str, value); +void strcatCurrent(char *str, size_t maxStrLength, float value) { + strcatFloat(str, maxStrLength, value); strcat(str, "A"); } -void strcatPower(char *str, float value) { - strcatFloat(str, value); +void strcatPower(char *str, size_t maxStrLength, float value) { + strcatFloat(str, maxStrLength, value); strcat(str, "W"); } -void strcatDuration(char *str, float value) { +void strcatDuration(char *str, size_t maxStrLength, float value) { if (value > 0.1) { - strcatFloat(str, value); + strcatFloat(str, maxStrLength, value); strcat(str, " s"); } else { - strcatFloat(str, value * 1000); + strcatFloat(str, maxStrLength, value * 1000); strcat(str, " ms"); } } -void strcatLoad(char *str, float value) { +void strcatLoad(char *str, size_t maxStrLength, float value) { if (value < 1000) { - strcatFloat(str, value); + strcatFloat(str, maxStrLength, value); strcat(str, " ohm"); } else if (value < 1000000) { - strcatFloat(str, value / 1000); + strcatFloat(str, maxStrLength, value / 1000); strcat(str, " Kohm"); } else { - strcatFloat(str, value / 1000000); + strcatFloat(str, maxStrLength, value / 1000000); strcat(str, " Mohm"); } } @@ -407,9 +404,10 @@ uint32_t getIpAddress(uint8_t a, uint8_t b, uint8_t c, uint8_t d) { return ipAddress; } -void ipAddressToString(uint32_t ipAddress, char *ipAddressStr) { - sprintf(ipAddressStr, "%d.%d.%d.%d", getIpAddressPartA(ipAddress), getIpAddressPartB(ipAddress), - getIpAddressPartC(ipAddress), getIpAddressPartD(ipAddress)); +void ipAddressToString(uint32_t ipAddress, char *ipAddressStr, size_t maxIpAddressStrLength) { + snprintf(ipAddressStr, maxIpAddressStrLength, "%d.%d.%d.%d", + getIpAddressPartA(ipAddress), getIpAddressPartB(ipAddress), + getIpAddressPartC(ipAddress), getIpAddressPartD(ipAddress)); } void macAddressToString(const uint8_t *macAddress, char *macAddressStr) { diff --git a/src/eez/util.h b/src/eez/util.h index b363248b1..dc6e5e4ff 100644 --- a/src/eez/util.h +++ b/src/eez/util.h @@ -54,17 +54,16 @@ float remapExp(float x, float x1, float y1, float x2, float y2); float remapOutExp(float x, float x1, float y1, float x2, float y2); float clamp(float x, float min, float max); -void strcatInt(char *str, int value); -void strcatInt32(char *str, int32_t value); -void strcatUInt32(char *str, uint32_t value); -void strcatFloat(char *str, float value); -void strcatFloat(char *str, float value, int numDecimalPlaces); - -void strcatVoltage(char *str, float value); -void strcatCurrent(char *str, float value); -void strcatPower(char *str, float value); -void strcatDuration(char *str, float value); -void strcatLoad(char *str, float value); +void strcatInt(char *str, size_t maxStrLength, int value); +void strcatUInt32(char *str, size_t maxStrLength, uint32_t value); +void strcatFloat(char *str, size_t maxStrLength, float value); +void strcatFloat(char *str, size_t maxStrLength, float value, int numDecimalPlaces); + +void strcatVoltage(char *str, size_t maxStrLength, float value); +void strcatCurrent(char *str, size_t maxStrLength, float value); +void strcatPower(char *str, size_t maxStrLength, float value); +void strcatDuration(char *str, size_t maxStrLength, float value); +void strcatLoad(char *str, size_t maxStrLength, float value); uint32_t crc32(const uint8_t *message, size_t size); @@ -109,7 +108,7 @@ uint32_t arrayToIpAddress(uint8_t *ipAddressArray); uint32_t getIpAddress(uint8_t a, uint8_t b, uint8_t c, uint8_t d); bool parseIpAddress(const char *ipAddressStr, size_t ipAddressStrLength, uint32_t &ipAddress); -void ipAddressToString(uint32_t ipAddress, char *ipAddressStr); +void ipAddressToString(uint32_t ipAddress, char *ipAddressStr, size_t maxIpAddressStrLength); void macAddressToString(const uint8_t *macAddress, char *macAddressStr);