Function overwrites value of passed argument (memory corruption?)

Hello.

I have this function trace that takes in a variable number of arguments and a formatted string, and eiher publishes the result to an MQTT log topic or prints to Serial depending if using ArduinoOTA or not.

For some unknown reason yet to me, the function overwrites one of the passed arguments, which is the MQTT received topic in the callback. I know how to patch the issue, basically by saving the value of topic to another variable, but I would like to understand why is this corruption happening.

Any pointers are appreciated! Thank you.

void trace( PGM_P format, ... ) {
    va_list args;
    va_start( args, format );
    int len = vsnprintf_P(NULL, 0, format, args) + 1;
    char logMsg[len];
    vsnprintf_P( logMsg, len, format, args );
    va_end( args );
    
    #ifdef USE_ARDUINO_OTA
      mqtt.publish(logTopic, logMsg);
    #else
      Serial.printf_P(logMsg);
    #endif
}

trace.ino (4.07 KB)

This shoud be vsnprinf_P, too.

    int len = vsnprintf(NULL, 0, format, args) + 1;

oqibidipo:
This shoud be vsnprinf_P, too.

    int len = vsnprintf(NULL, 0, format, args) + 1;

Thanks, that was a small test I did and forgot to switch it back. But not the cause of the corruption/issue. I have updated the OP.