getPinNameByAdcChannel: be more safe

Know buffer size
Use snprintf
Expect null from portname()
This commit is contained in:
Andrey Gusakov 2024-07-21 00:02:21 +03:00 committed by rusefillc
parent 4007e0d7f8
commit 42213ac172
5 changed files with 9 additions and 9 deletions

View File

@ -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;
}

View File

@ -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();

View File

@ -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,

View File

@ -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));

View File

@ -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
);
}