summaryrefslogtreecommitdiffstatshomepage
path: root/py/mpprint.c
diff options
context:
space:
mode:
authorJoris Peeraer <jorispeeraer@gmail.com>2020-10-22 10:38:03 +0200
committerDamien George <damien@micropython.org>2020-12-07 23:32:06 +1100
commit5020b14d5419065f1a5ef5aed1be7badee28c9bf (patch)
tree3a5ecda9ef9027206a3dbb4c3df5fab99803a32d /py/mpprint.c
parentdde0735ac1629aca4f7d41334f25b75dd8d35010 (diff)
downloadmicropython-5020b14d5419065f1a5ef5aed1be7badee28c9bf.tar.gz
micropython-5020b14d5419065f1a5ef5aed1be7badee28c9bf.zip
py/mpprint: Fix length calculation for strings with precision-modifier.
Two issues are tackled: 1. The calculation of the correct length to print is fixed to treat the precision as a maximum length instead as the exact length. This is done for both qstr (%q) and for regular str (%s). 2. Fix the incorrect use of mp_printf("%.*s") to mp_print_strn(). Because of the fix of above issue, some testcases that would print an embedded null-byte (^@ in test-output) would now fail. The bug here is that "%s" was used to print null-bytes. Instead, mp_print_strn is used to make sure all bytes are outputted and the exact length is respected. Test-cases are added for both %s and %q with a combination of precision and padding specifiers.
Diffstat (limited to 'py/mpprint.c')
-rw-r--r--py/mpprint.c13
1 files changed, 7 insertions, 6 deletions
diff --git a/py/mpprint.c b/py/mpprint.c
index 31cf73bb58..3218bd2f4d 100644
--- a/py/mpprint.c
+++ b/py/mpprint.c
@@ -484,10 +484,10 @@ int mp_vprintf(const mp_print_t *print, const char *fmt, va_list args) {
qstr qst = va_arg(args, qstr);
size_t len;
const char *str = (const char *)qstr_data(qst, &len);
- if (prec < 0) {
- prec = len;
+ if (prec >= 0 && (size_t)prec < len) {
+ len = prec;
}
- chrs += mp_print_strn(print, str, prec, flags, fill, width);
+ chrs += mp_print_strn(print, str, len, flags, fill, width);
break;
}
case 's': {
@@ -499,10 +499,11 @@ int mp_vprintf(const mp_print_t *print, const char *fmt, va_list args) {
break;
}
#endif
- if (prec < 0) {
- prec = strlen(str);
+ size_t len = strlen(str);
+ if (prec >= 0 && (size_t)prec < len) {
+ len = prec;
}
- chrs += mp_print_strn(print, str, prec, flags, fill, width);
+ chrs += mp_print_strn(print, str, len, flags, fill, width);
break;
}
case 'd': {