From 23faf88cab10a216eb24a30b4f33edd17183522c Mon Sep 17 00:00:00 2001
From: Damien George <damien.p.george@gmail.com>
Date: Wed, 4 Oct 2017 18:07:32 +1100
Subject: [PATCH] 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.
---
 py/mpprint.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/py/mpprint.c b/py/mpprint.c
index 6c02d7cef..a569ef793 100644
--- a/py/mpprint.c
+++ b/py/mpprint.c
@@ -485,14 +485,17 @@ int mp_vprintf(const mp_print_t *print, const char *fmt, va_list args) {
             case 's':
             {
                 const char *str = va_arg(args, const char*);
-                if (str) {
-                    if (prec < 0) {
-                        prec = strlen(str);
-                    }
-                    chrs += mp_print_strn(print, str, prec, flags, fill, width);
-                } else {
+                #ifndef NDEBUG
+                // With debugging enabled, catch printing of null string pointers
+                if (prec != 0 && str == NULL) {
                     chrs += mp_print_strn(print, "(null)", 6, flags, fill, width);
+                    break;
                 }
+                #endif
+                if (prec < 0) {
+                    prec = strlen(str);
+                }
+                chrs += mp_print_strn(print, str, prec, flags, fill, width);
                 break;
             }
             case 'u':
-- 
GitLab