Skip to content

Fix snprintf in minimal-printf library #12632

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 2 commits into from
Mar 25, 2020
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
5 changes: 5 additions & 0 deletions TESTS/mbed_platform/minimal-printf/compliance/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,11 @@ static control_t test_snprintf_d(const size_t call_count)
TEST_ASSERT_EQUAL_INT(result_baseline, result_minimal);
#endif

int a = 2;
int b = 3;
result_minimal = mbed_snprintf(0, 0, "%d + %d = %d\n", a, b, a + b);
TEST_ASSERT_EQUAL_INT(10, result_minimal);

return CaseNext;
}

Expand Down
77 changes: 19 additions & 58 deletions platform/source/minimal-printf/mbed_printf_implementation.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,7 @@
#include <stdint.h>
#include <stddef.h>

/***************************/
/* MBED */
/***************************/
#if TARGET_LIKE_MBED

#if MBED_CONF_PLATFORM_STDIO_CONVERT_NEWLINES
static char mbed_stdio_out_prev = 0;
#endif


/***************************/
/* Linux */
/***************************/
#else
#if !TARGET_LIKE_MBED
/* Linux implementation is for debug only */
#define MBED_CONF_PLATFORM_MINIMAL_PRINTF_ENABLE_FLOATING_POINT 1
#define MBED_CONF_PLATFORM_MINIMAL_PRINTF_SET_FLOATING_POINT_MAX_DECIMALS 6
Expand Down Expand Up @@ -106,7 +93,6 @@ static void mbed_minimal_formatted_string_signed(char *buffer, size_t length, in
static void mbed_minimal_formatted_string_unsigned(char *buffer, size_t length, int *result, MBED_UNSIGNED_STORAGE value, FILE *stream);
static void mbed_minimal_formatted_string_hexadecimal(char *buffer, size_t length, int *result, MBED_UNSIGNED_STORAGE value, FILE *stream, bool upper);
static void mbed_minimal_formatted_string_void_pointer(char *buffer, size_t length, int *result, const void *value, FILE *stream);
static void mbed_minimal_formatted_string_character(char *buffer, size_t length, int *result, char character, FILE *stream);
static void mbed_minimal_formatted_string_string(char *buffer, size_t length, int *result, const char *string, size_t precision, FILE *stream);


Expand All @@ -122,21 +108,23 @@ static void mbed_minimal_putchar(char *buffer, size_t length, int *result, char
{
/* only continue if 'result' doesn't overflow */
if ((*result >= 0) && (*result <= INT_MAX - 1)) {
if (buffer) {
/* write data only if there's enough space */
if ((size_t)*result < length) {
buffer[*result] = data;
}

/* increment 'result' even if data was not written. This ensures that
'mbed_minimal_formatted_string' returns the correct value. */
*result += 1;
} else {
if (stream) {
if (fputc(data, stream) == EOF) {
*result = EOF;
} else {
*result += 1;
}
} else {
if (buffer) {
/* write data only if there's enough space */
if ((size_t)*result < length) {
buffer[*result] = data;
}
}

/* increment 'result' even if data was not written. This ensures that
'mbed_minimal_formatted_string' returns the correct value. */
*result += 1;
}
}
}
Expand Down Expand Up @@ -280,7 +268,7 @@ static void mbed_minimal_formatted_string_double(char *buffer, size_t length, in
mbed_minimal_formatted_string_signed(buffer, length, result, integer, stream);

/* write decimal point */
mbed_minimal_formatted_string_character(buffer, length, result, '.', stream);
mbed_minimal_putchar(buffer, length, result, '.', stream);

/* get decimal part */
double precision = 1.0;
Expand Down Expand Up @@ -325,33 +313,6 @@ static void mbed_minimal_formatted_string_double(char *buffer, size_t length, in
}
#endif

/**
* @brief Print character.
*
* @param buffer The buffer to store output (NULL for stdout).
* @param[in] length The length of the buffer.
* @param result The current output location.
* @param[in] value The character to be printed.
*/
static void mbed_minimal_formatted_string_character(char *buffer, size_t length, int *result, char character, FILE *stream)
{
/* write character */
if (buffer) {
mbed_minimal_putchar(buffer, length, result, character, stream);
} else {
/* convert \n to \r\n if enabled in platform configuration */
#if MBED_CONF_PLATFORM_STDIO_CONVERT_NEWLINES
if (character == '\n' && mbed_stdio_out_prev != '\r') {
mbed_minimal_putchar(buffer, length, result, '\r', stream);
}

/* cache character */
mbed_stdio_out_prev = character;
#endif
mbed_minimal_putchar(buffer, length, result, character, stream);
}
}

/**
* @brief Print string with precision.
*
Expand Down Expand Up @@ -511,7 +472,7 @@ int mbed_minimal_formatted_string(char *buffer, size_t length, const char *forma
#else
/* If 64 bit is not enabled, print %ll[di] rather than truncated value */
if (length_modifier == LENGTH_LL) {
mbed_minimal_formatted_string_character(buffer, length, &result, '%', stream);
mbed_minimal_putchar(buffer, length, &result, '%', stream);
if (next == '%') {
// Continue printing loop after `%`
index = next_index;
Expand Down Expand Up @@ -570,7 +531,7 @@ int mbed_minimal_formatted_string(char *buffer, size_t length, const char *forma
#else
/* If 64 bit is not enabled, print %ll[uxX] rather than truncated value */
if (length_modifier == LENGTH_LL) {
mbed_minimal_formatted_string_character(buffer, length, &result, '%', stream);
mbed_minimal_putchar(buffer, length, &result, '%', stream);
if (next == '%') {
// Continue printing loop after `%`
index = next_index;
Expand Down Expand Up @@ -636,7 +597,7 @@ int mbed_minimal_formatted_string(char *buffer, size_t length, const char *forma
char value = va_arg(arguments, MBED_SIGNED_NATIVE_TYPE);
index = next_index;

mbed_minimal_formatted_string_character(buffer, length, &result, value, stream);
mbed_minimal_putchar(buffer, length, &result, value, stream);
}
/* string */
else if (next == 's') {
Expand All @@ -653,7 +614,7 @@ int mbed_minimal_formatted_string(char *buffer, size_t length, const char *forma
mbed_minimal_formatted_string_void_pointer(buffer, length, &result, value, stream);
} else {
// Unrecognised, or `%%`. Print the `%` that led us in.
mbed_minimal_formatted_string_character(buffer, length, &result, '%', stream);
mbed_minimal_putchar(buffer, length, &result, '%', stream);
if (next == '%') {
// Continue printing loop after `%%`
index = next_index;
Expand All @@ -665,7 +626,7 @@ int mbed_minimal_formatted_string(char *buffer, size_t length, const char *forma
/* not a format specifier */
{
/* write normal character */
mbed_minimal_formatted_string_character(buffer, length, &result, format[index], stream);
mbed_minimal_putchar(buffer, length, &result, format[index], stream);
}
}

Expand Down