Skip to content

Commit 3918800

Browse files
committed
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 <[email protected]>
1 parent 0b9a591 commit 3918800

File tree

3 files changed

+33
-23
lines changed

3 files changed

+33
-23
lines changed

cores/arduino/Print.cpp

+29-11
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
*/
2222

2323
#include <stdlib.h>
24-
#include <stdio.h>
2524
#include <string.h>
25+
#include <unistd.h>
2626
#include <math.h>
2727
#include "Arduino.h"
2828

@@ -196,24 +196,42 @@ size_t Print::println(const Printable &x)
196196
return n;
197197
}
198198

199-
void Print::printf(const char *format, ...)
199+
extern "C" {
200+
__attribute__((weak))
201+
int _write(int file, char *ptr, int len)
202+
{
203+
#ifdef HAL_UART_MODULE_ENABLED
204+
switch (file) {
205+
case STDOUT_FILENO:
206+
case STDERR_FILENO:
207+
uart_debug_write((uint8_t *)ptr, (uint32_t)len);
208+
break;
209+
case STDIN_FILENO:
210+
break;
211+
default:
212+
((class Print *)file)->write((uint8_t *)ptr, len);
213+
break;
214+
}
215+
#else
216+
(void)file;
217+
(void)ptr;
218+
#endif
219+
return len;
220+
}
221+
}
222+
223+
int Print::printf(const char *format, ...)
200224
{
201-
char buf[PRINTF_BUFFER];
202225
va_list ap;
203226
va_start(ap, format);
204-
vsnprintf(buf, sizeof(buf), format, ap);
205-
write(buf);
206-
va_end(ap);
227+
return vdprintf((int)this, format, ap);
207228
}
208229

209-
void Print::printf(const __FlashStringHelper *format, ...)
230+
int Print::printf(const __FlashStringHelper *format, ...)
210231
{
211-
char buf[PRINTF_BUFFER];
212232
va_list ap;
213233
va_start(ap, format);
214-
vsnprintf_P(buf, sizeof(buf), (const char *)format, ap);
215-
write(buf);
216-
va_end(ap);
234+
return vdprintf((int)this, (const char *)format, ap);
217235
}
218236

219237
// Private Methods /////////////////////////////////////////////////////////////

cores/arduino/Print.h

+2-6
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@
3232
#define OCT 8
3333
#define BIN 2
3434

35-
#ifndef PRINTF_BUFFER
36-
#define PRINTF_BUFFER 80
37-
#endif
38-
3935
// uncomment next line to support printing of 64 bit ints.
4036
#define SUPPORT_LONGLONG
4137

@@ -109,8 +105,8 @@ class Print {
109105
void print(uint64_t, uint8_t = DEC);
110106
#endif
111107

112-
void printf(const char *format, ...);
113-
void printf(const __FlashStringHelper *format, ...);
108+
int printf(const char *format, ...);
109+
int printf(const __FlashStringHelper *format, ...);
114110
};
115111

116112
#endif

libraries/SrcWrapper/src/syscalls.c

+2-6
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,12 @@ int _read(UNUSED(int file), UNUSED(char *ptr), UNUSED(int len))
8181
return 0;
8282
}
8383

84+
/* Moved to Print.cpp to support Print::printf()
8485
__attribute__((weak))
8586
int _write(UNUSED(int file), char *ptr, int len)
8687
{
87-
#ifdef HAL_UART_MODULE_ENABLED
88-
return uart_debug_write((uint8_t *)ptr, (uint32_t)len);
89-
#else
90-
(void)ptr;
91-
return len;
92-
#endif
9388
}
89+
*/
9490

9591
__attribute__((weak))
9692
void _exit(UNUSED(int status))

0 commit comments

Comments
 (0)