Skip to content

Harden Print::printf() method #795

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions cores/arduino/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include "Arduino.h"

Expand Down Expand Up @@ -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 /////////////////////////////////////////////////////////////
Expand Down
8 changes: 2 additions & 6 deletions cores/arduino/Print.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
8 changes: 2 additions & 6 deletions libraries/SrcWrapper/src/syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down