diff --git a/esphome/components/esp32/printf_stubs.cpp b/esphome/components/esp32/printf_stubs.cpp index 8fc303cfe93..33098ea5169 100644 --- a/esphome/components/esp32/printf_stubs.cpp +++ b/esphome/components/esp32/printf_stubs.cpp @@ -38,12 +38,16 @@ static int write_printf_buffer_(FILE *stream, char *buf, int len) { if (len < 0) { return len; } - if (static_cast(len) >= PRINTF_BUFFER_SIZE) { - // Output was truncated — this should not happen in normal operation. - // Abort to make the issue visible rather than silently losing output. + size_t write_len = len; + if (write_len >= PRINTF_BUFFER_SIZE) { + // Output was truncated — flush what we have before aborting + // so the user sees context leading up to the overflow. + fwrite(buf, 1, PRINTF_BUFFER_SIZE - 1, stream); esp_system_abort("printf buffer overflow; set enable_full_printf: true in esp32 advanced config"); } - fwrite(buf, 1, len, stream); + if (fwrite(buf, 1, write_len, stream) < write_len || ferror(stream)) { + return -1; + } return len; } diff --git a/tests/components/esp32/test.esp32-idf.yaml b/tests/components/esp32/test.esp32-idf.yaml index f80c854de55..da85aa3b0f2 100644 --- a/tests/components/esp32/test.esp32-idf.yaml +++ b/tests/components/esp32/test.esp32-idf.yaml @@ -10,6 +10,7 @@ esp32: use_full_certificate_bundle: false # Test CMN bundle (default) include_builtin_idf_components: - freertos # Test escape hatch (freertos is always included anyway) + enable_full_printf: false disable_debug_stubs: true disable_ocd_aware: true disable_usb_serial_jtag_secondary: true