From 3d3b29be95244eb6657e471aa12d202b42466c1a Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Tue, 26 Nov 2019 14:34:09 +0100 Subject: [PATCH] Harden Print::printf() method based on @PaulStoffregen works for Teensyduino: https://github.com/PaulStoffregen/cores Remove PRINTF_BUFFER usage. Aligned declaration with `printf()` one by returning the number of characters printed (excluding the null byte used to end output to strings). Signed-off-by: Frederic Pillon --- cores/arduino/Print.cpp | 40 +++++++++++++++++++++-------- cores/arduino/Print.h | 8 ++---- libraries/SrcWrapper/src/syscalls.c | 8 ++---- 3 files changed, 33 insertions(+), 23 deletions(-) diff --git a/cores/arduino/Print.cpp b/cores/arduino/Print.cpp index 633f24eedd..456927f296 100644 --- a/cores/arduino/Print.cpp +++ b/cores/arduino/Print.cpp @@ -21,8 +21,8 @@ */ #include -#include #include +#include #include #include "Arduino.h" @@ -196,24 +196,42 @@ size_t Print::println(const Printable &x) return n; } -void Print::printf(const char *format, ...) +extern "C" { + __attribute__((weak)) + int _write(int file, char *ptr, int len) + { +#ifdef HAL_UART_MODULE_ENABLED + switch (file) { + case STDOUT_FILENO: + case STDERR_FILENO: + uart_debug_write((uint8_t *)ptr, (uint32_t)len); + break; + case STDIN_FILENO: + break; + default: + ((class Print *)file)->write((uint8_t *)ptr, len); + break; + } +#else + (void)file; + (void)ptr; +#endif + return len; + } +} + +int Print::printf(const char *format, ...) { - char buf[PRINTF_BUFFER]; va_list ap; va_start(ap, format); - vsnprintf(buf, sizeof(buf), format, ap); - write(buf); - va_end(ap); + return vdprintf((int)this, format, ap); } -void Print::printf(const __FlashStringHelper *format, ...) +int Print::printf(const __FlashStringHelper *format, ...) { - char buf[PRINTF_BUFFER]; va_list ap; va_start(ap, format); - vsnprintf_P(buf, sizeof(buf), (const char *)format, ap); - write(buf); - va_end(ap); + return vdprintf((int)this, (const char *)format, ap); } // Private Methods ///////////////////////////////////////////////////////////// diff --git a/cores/arduino/Print.h b/cores/arduino/Print.h index 19988adb07..0e37080abc 100644 --- a/cores/arduino/Print.h +++ b/cores/arduino/Print.h @@ -32,10 +32,6 @@ #define OCT 8 #define BIN 2 -#ifndef PRINTF_BUFFER -#define PRINTF_BUFFER 80 -#endif - // uncomment next line to support printing of 64 bit ints. #define SUPPORT_LONGLONG @@ -109,8 +105,8 @@ class Print { void print(uint64_t, uint8_t = DEC); #endif - void printf(const char *format, ...); - void printf(const __FlashStringHelper *format, ...); + int printf(const char *format, ...); + int printf(const __FlashStringHelper *format, ...); }; #endif diff --git a/libraries/SrcWrapper/src/syscalls.c b/libraries/SrcWrapper/src/syscalls.c index a477795d6b..27b556a1bf 100644 --- a/libraries/SrcWrapper/src/syscalls.c +++ b/libraries/SrcWrapper/src/syscalls.c @@ -81,16 +81,12 @@ int _read(UNUSED(int file), UNUSED(char *ptr), UNUSED(int len)) return 0; } +/* Moved to Print.cpp to support Print::printf() __attribute__((weak)) int _write(UNUSED(int file), char *ptr, int len) { -#ifdef HAL_UART_MODULE_ENABLED - return uart_debug_write((uint8_t *)ptr, (uint32_t)len); -#else - (void)ptr; - return len; -#endif } +*/ __attribute__((weak)) void _exit(UNUSED(int status))