Skip to content

Commit a29776a

Browse files
committed
minimal-printf: Fix handling of the two character sequence %%
The two character sequence %% is used in standard implementations of printf to print a single %. This is because % is essentially printf's escape character for format specifiers and as \% cannot work printf uses %%. Therefore to be compatible with string buffers containing %%, minimal-printf also needs to only print a single %.
1 parent 5d330bf commit a29776a

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

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)