From 42213ac17272a44ca3d524d31d4ba99948f28ca0 Mon Sep 17 00:00:00 2001 From: Andrey Gusakov Date: Sun, 21 Jul 2024 00:02:21 +0300 Subject: [PATCH] getPinNameByAdcChannel: be more safe Know buffer size Use snprintf Expect null from portname() --- firmware/controllers/engine_controller.cpp | 10 +++++----- firmware/controllers/engine_controller.h | 2 +- firmware/controllers/sensors/impl/map.cpp | 2 +- firmware/controllers/settings.cpp | 2 +- firmware/hw_layer/adc/adc_subscription.cpp | 2 +- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/firmware/controllers/engine_controller.cpp b/firmware/controllers/engine_controller.cpp index debebde6e9..f93452cd6d 100644 --- a/firmware/controllers/engine_controller.cpp +++ b/firmware/controllers/engine_controller.cpp @@ -237,16 +237,16 @@ void initPeriodicEvents() { fastController.start(); } -char * getPinNameByAdcChannel(const char *msg, adc_channel_e hwChannel, char *buffer) { +char * getPinNameByAdcChannel(const char *msg, adc_channel_e hwChannel, char *buffer, size_t bufferSize) { #if HAL_USE_ADC if (!isAdcChannelValid(hwChannel)) { - strcpy(buffer, "NONE"); + snprintf(buffer, bufferSize, "NONE"); } else { - strcpy(buffer, portname(getAdcChannelPort(msg, hwChannel))); - itoa10(&buffer[2], getAdcChannelPin(hwChannel)); + const char *name = portname(getAdcChannelPort(msg, hwChannel)); + snprintf(buffer, bufferSize, "%s%d", name ? name : "null", getAdcChannelPin(hwChannel)); } #else - strcpy(buffer, "NONE"); + snprintf(buffer, bufferSize, "NONE"); #endif /* HAL_USE_ADC */ return buffer; } diff --git a/firmware/controllers/engine_controller.h b/firmware/controllers/engine_controller.h index 6cef6b5e87..a7a9416587 100644 --- a/firmware/controllers/engine_controller.h +++ b/firmware/controllers/engine_controller.h @@ -12,7 +12,7 @@ #define SLOW_CALLBACK_PERIOD_MS 50 bool validateConfigOnStartUpOrBurn(); -char * getPinNameByAdcChannel(const char *msg, adc_channel_e hwChannel, char *buffer); +char * getPinNameByAdcChannel(const char *msg, adc_channel_e hwChannel, char *buffer, size_t bufferSize); void initPeriodicEvents(); // see also applyNewHardwareSettings void initRealHardwareEngineController(); diff --git a/firmware/controllers/sensors/impl/map.cpp b/firmware/controllers/sensors/impl/map.cpp index 5f201ccb03..47aed8c26b 100644 --- a/firmware/controllers/sensors/impl/map.cpp +++ b/firmware/controllers/sensors/impl/map.cpp @@ -39,7 +39,7 @@ static void printMAPInfo() { char pinNameBuffer[16]; efiPrintf("MAP %.2fv @%s", getVoltage("mapinfo", mapAdc), - getPinNameByAdcChannel("map", mapAdc, pinNameBuffer)); + getPinNameByAdcChannel("map", mapAdc, pinNameBuffer, sizeof(pinNameBuffer))); if (engineConfiguration->map.sensor.type == MT_CUSTOM) { efiPrintf("at %.2fv=%.2f at %.2fv=%.2f", engineConfiguration->mapLowValueVoltage, diff --git a/firmware/controllers/settings.cpp b/firmware/controllers/settings.cpp index 535190f5cf..5030416b98 100644 --- a/firmware/controllers/settings.cpp +++ b/firmware/controllers/settings.cpp @@ -183,7 +183,7 @@ static void printTpsSenser(const char *msg, SensorType sensor, int16_t min, int1 char pinNameBuffer[16]; efiPrintf("tps min (closed) %d/max (full) %d v=%.2f @%s", min, max, - raw, getPinNameByAdcChannel(msg, channel, pinNameBuffer)); + raw, getPinNameByAdcChannel(msg, channel, pinNameBuffer, sizeof(pinNameBuffer))); efiPrintf("current 10bit=%d value=%.2f", convertVoltageTo10bitADC(raw), tps.value_or(0)); diff --git a/firmware/hw_layer/adc/adc_subscription.cpp b/firmware/hw_layer/adc/adc_subscription.cpp index bac30bb1c5..c305d4b18a 100644 --- a/firmware/hw_layer/adc/adc_subscription.cpp +++ b/firmware/hw_layer/adc/adc_subscription.cpp @@ -187,7 +187,7 @@ void AdcSubscription::PrintInfo() { name, channel, getAdcMode(channel), - getPinNameByAdcChannel(name, channel, pinNameBuffer), + getPinNameByAdcChannel(name, channel, pinNameBuffer, sizeof(pinNameBuffer)), mcuVolts, sensorVolts, entry.VoltsPerAdcVolt ); }