Skip to content

Commit 23faf88

Browse files
committed
py/mpprint: Only check for null string printing when NDEBUG not defined.
Printing "(null)" when a NULL string pointer is passed to %s is a debugging feature and not a feature that's relied upon by the code. So it only needs to be compiled in when debugging (such as assert) is enabled, and saves roughy 30 bytes of code when disabled. This patch also fixes this NULL check to not do the check if the precision is specified as zero.
1 parent dfa563c commit 23faf88

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

py/mpprint.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -485,14 +485,17 @@ int mp_vprintf(const mp_print_t *print, const char *fmt, va_list args) {
485485
case 's':
486486
{
487487
const char *str = va_arg(args, const char*);
488-
if (str) {
489-
if (prec < 0) {
490-
prec = strlen(str);
491-
}
492-
chrs += mp_print_strn(print, str, prec, flags, fill, width);
493-
} else {
488+
#ifndef NDEBUG
489+
// With debugging enabled, catch printing of null string pointers
490+
if (prec != 0 && str == NULL) {
494491
chrs += mp_print_strn(print, "(null)", 6, flags, fill, width);
492+
break;
495493
}
494+
#endif
495+
if (prec < 0) {
496+
prec = strlen(str);
497+
}
498+
chrs += mp_print_strn(print, str, prec, flags, fill, width);
496499
break;
497500
}
498501
case 'u':

0 commit comments

Comments
 (0)