Skip to content

Commit 6240335

Browse files
authored
Merge pull request #11729 from hugueskamba/hk-fix-minimal-printf-percentage-printing
minimal-printf: Fix handling of the two character sequence %%
2 parents bb48dd3 + ceffb6d commit 6240335

File tree

2 files changed

+53
-12
lines changed

2 files changed

+53
-12
lines changed

TESTS/mbed_platform/minimal-printf/compliance/main.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,21 @@ static control_t test_printf_x(const size_t call_count)
404404
return CaseNext;
405405
}
406406

407+
static control_t test_printf_percent(const size_t call_count)
408+
{
409+
int result_baseline;
410+
int result_minimal;
411+
int result_file;
412+
413+
result_minimal = mbed_printf("%% \r\n");
414+
result_file = mbed_fprintf(stderr, "%% \r\n");
415+
result_baseline = printf("%% \r\n");
416+
TEST_ASSERT_EQUAL_INT(result_baseline, result_minimal);
417+
TEST_ASSERT_EQUAL_INT(result_baseline, result_file);
418+
419+
return CaseNext;
420+
}
421+
407422
/******************************************************************************/
408423
/* */
409424
/* SNPRINTF */
@@ -721,6 +736,34 @@ static control_t test_snprintf_x(const size_t call_count)
721736
return CaseNext;
722737
}
723738

739+
static control_t test_snprintf_percent(const size_t call_count)
740+
{
741+
char buffer_baseline[100];
742+
char buffer_minimal[100];
743+
int result_baseline;
744+
int result_minimal;
745+
746+
result_minimal = mbed_snprintf(buffer_minimal, sizeof(buffer_minimal), "%% \r\n");
747+
result_baseline = snprintf(buffer_baseline, sizeof(buffer_baseline), "%% \r\n");
748+
TEST_ASSERT_EQUAL_STRING(buffer_baseline, buffer_minimal);
749+
TEST_ASSERT_EQUAL_INT(result_baseline, result_minimal);
750+
751+
return CaseNext;
752+
}
753+
754+
static control_t test_snprintf_unsupported_specifier(const size_t call_count)
755+
{
756+
char buffer_minimal[100];
757+
758+
TEST_ASSERT_NOT_EQUAL(
759+
0,
760+
mbed_snprintf(buffer_minimal, sizeof(buffer_minimal), "%a \r\n", 5)
761+
);
762+
TEST_ASSERT_EQUAL_STRING("%a \r\n", buffer_minimal);
763+
764+
return CaseNext;
765+
}
766+
724767
#if MBED_CONF_PLATFORM_MINIMAL_PRINTF_ENABLE_FLOATING_POINT
725768
static control_t test_printf_f(const size_t call_count)
726769
{
@@ -902,6 +945,9 @@ Case cases[] = {
902945
Case("snprintf %u", test_snprintf_u),
903946
Case("printf %x", test_printf_x),
904947
Case("snprintf %x", test_snprintf_x),
948+
Case("printf %%", test_printf_percent),
949+
Case("snprintf %%", test_snprintf_percent),
950+
Case("snprintf unsupported specifier", test_snprintf_unsupported_specifier),
905951
#if MBED_CONF_PLATFORM_MINIMAL_PRINTF_ENABLE_FLOATING_POINT
906952
Case("printf %f", test_printf_f),
907953
Case("snprintf %f", test_snprintf_f),

platform/source/minimal-printf/mbed_printf_implementation.c

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -626,19 +626,14 @@ int mbed_minimal_formatted_string(char *buffer, size_t length, const char *forma
626626

627627
mbed_minimal_formatted_string_void_pointer(buffer, length, &result, value, stream);
628628
} else {
629-
/* write all characters between format beginning and unrecognied modifier */
630-
while (index < next_index) {
631-
mbed_minimal_formatted_string_character(buffer, length, &result, format[index], stream);
632-
index++;
633-
}
634-
635-
/* if this is not the end of the string, write unrecognized modifier */
636-
if (next != '\0') {
637-
mbed_minimal_formatted_string_character(buffer, length, &result, format[index], stream);
638-
} else {
639-
/* break out of for loop */
640-
break;
629+
// Unrecognised, or `%%`. Print the `%` that led us in.
630+
mbed_minimal_formatted_string_character(buffer, length, &result, '%', stream);
631+
if (next == '%') {
632+
// Continue printing loop after `%%`
633+
index = next_index;
641634
}
635+
// Otherwise we continue the printing loop after the leading `%`, so an
636+
// unrecognised thing like "Blah = %a" will just come out as "Blah = %a"
642637
}
643638
} else
644639
/* not a format specifier */

0 commit comments

Comments
 (0)