Skip to content

Commit 2a01a54

Browse files
author
Bogdan Marinescu
committed
Address code review issues
- Improved comments to explain the checks on 'result'. - Check for non-NULL format specifier.
1 parent a5f0716 commit 2a01a54

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

features/minimal-printf/mbed_printf_implementation.c

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ static void mbed_minimal_formatted_string_string(char* buffer, size_t length, in
141141
*/
142142
static void mbed_minimal_formatted_string_signed(char* buffer, size_t length, int* result, MBED_SIGNED_STORAGE value)
143143
{
144-
/* only continue if buffer can fit at least 1 characters */
144+
/* only continue if buffer can fit at least 1 character and if
145+
'result' doesn't overflow */
145146
if ((*result >= 0) && (*result <= INT_MAX - 1) && ((size_t)*result + 1 <= length))
146147
{
147148
MBED_UNSIGNED_STORAGE new_value = 0;
@@ -184,7 +185,8 @@ static void mbed_minimal_formatted_string_signed(char* buffer, size_t length, in
184185
*/
185186
static void mbed_minimal_formatted_string_unsigned(char* buffer, size_t length, int* result, MBED_UNSIGNED_STORAGE value)
186187
{
187-
/* only continue if buffer can fit at least 1 characters */
188+
/* only continue if buffer can fit at least 1 character and if
189+
'result' doesn't overflow */
188190
if ((*result >= 0) && (*result <= INT_MAX - 1) && ((size_t)*result + 1 <= length))
189191
{
190192
/* treat 0 as a corner case */
@@ -248,7 +250,8 @@ static void mbed_minimal_formatted_string_hexadecimal(char* buffer, size_t lengt
248250
{
249251
bool print_leading_zero = false;
250252

251-
/* only continue each loop if buffer can fit at least 2 characters */
253+
/* only continue each loop if buffer can fit at least 2 characters
254+
and if 'result' doesn't overflow */
252255
for (int index = 7; (*result >= 0) && (*result <= INT_MAX - 2) && ((size_t)*result + 2 <= length) && (index >= 0); index--)
253256
{
254257
/* get most significant byte */
@@ -293,7 +296,8 @@ static void mbed_minimal_formatted_string_hexadecimal(char* buffer, size_t lengt
293296
*/
294297
static void mbed_minimal_formatted_string_void_pointer(char* buffer, size_t length, int* result, const void* value)
295298
{
296-
/* only continue if buffer can fit '0x' and twice the size of a void* */
299+
/* only continue if buffer can fit '0x' and twice the size of a void*
300+
and if 'result' doesn't overflow */
297301
size_t needed = 2 + 2 * sizeof(void*);
298302
if ((*result >= 0) && ((size_t)*result <= INT_MAX - needed) && ((size_t)*result + needed <= length))
299303
{
@@ -327,7 +331,8 @@ static void mbed_minimal_formatted_string_void_pointer(char* buffer, size_t leng
327331
*/
328332
static void mbed_minimal_formatted_string_double(char* buffer, size_t length, int* result, double value)
329333
{
330-
/* only continue if buffer can fit at least 1 characters */
334+
/* only continue if buffer can fit at least 1 character and if
335+
'result' doesn't overflow */
331336
if ((*result >= 0) && (*result <= INT_MAX - 1) && ((size_t)*result + 1 <= length))
332337
{
333338
/* get integer part */
@@ -386,7 +391,8 @@ static void mbed_minimal_formatted_string_double(char* buffer, size_t length, in
386391
*/
387392
static void mbed_minimal_formatted_string_character(char* buffer, size_t length, int* result, char character)
388393
{
389-
/* only continue if the buffer can fit 1 character */
394+
/* only continue if the buffer can fit 1 character and if
395+
'result' doesn't overflow */
390396
if ((*result >= 0) && (*result <= INT_MAX - 1) && ((size_t)*result + 1 <= length))
391397
{
392398
/* write character */
@@ -426,7 +432,8 @@ static void mbed_minimal_formatted_string_character(char* buffer, size_t length,
426432
*/
427433
static void mbed_minimal_formatted_string_string(char* buffer, size_t length, int* result, const char* string)
428434
{
429-
/* only continue if the buffer can fit at least 1 character */
435+
/* only continue if the buffer can fit at least 1 character and if
436+
'result' doesn't overflow */
430437
if ((*result >= 0) && (*result <= INT_MAX - 1) && ((size_t)*result + 1 <= length))
431438
{
432439
/* count characters in string */
@@ -483,9 +490,10 @@ int mbed_minimal_formatted_string(char* buffer, size_t length, const char* forma
483490

484491
int result = 0;
485492

486-
/* ensure that function wasn't called with an empty buffer, or with
487-
a buffer size that is larger than the maximum 'int' value */
488-
if (length > 0 && length <= INT_MAX)
493+
/* ensure that function wasn't called with an empty buffer, or with or with
494+
a buffer size that is larger than the maximum 'int' value, or with
495+
a NULL format specifier */
496+
if (format && length > 0 && length <= INT_MAX)
489497
{
490498
/* parse string */
491499
for (size_t index = 0; format[index] != '\0'; index++)

0 commit comments

Comments
 (0)