From 7fc31186ee1afbc1fb5b47b02e412a7546711cb0 Mon Sep 17 00:00:00 2001 From: Sverker Eriksson Date: Tue, 21 Aug 2012 17:08:08 +0200 Subject: erts: Fix bug in erts_printf for %s with precision Valgrind complains "Conditional jump or move depends on uninitialised value" when strlen steps past given string maxlen (precision). --- erts/lib_src/common/erl_printf_format.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'erts/lib_src/common') diff --git a/erts/lib_src/common/erl_printf_format.c b/erts/lib_src/common/erl_printf_format.c index 473791dce4..71d2aa35d0 100644 --- a/erts/lib_src/common/erl_printf_format.c +++ b/erts/lib_src/common/erl_printf_format.c @@ -457,6 +457,15 @@ static int fmt_double(fmtfn_t fn,void*arg,double val, return res; } +/* strnlen doesn't exist everywhere */ +static size_t my_strnlen(const char *s, size_t maxlen) +{ + size_t i = 0; + while (i < maxlen && s[i] != '\0') + i++; + return i; +} + int erts_printf_format(fmtfn_t fn, void* arg, char* fmt, va_list ap) { char* ptr0 = fmt; @@ -771,9 +780,7 @@ int erts_printf_format(fmtfn_t fn, void* arg, char* fmt, va_list ap) case FMTC_s: { char* str = va_arg(ap,char*); - int len = strlen(str); - if (precision >= 0 && precision < len) - len = precision; + int len = (precision >= 0) ? my_strnlen(str,precision) : strlen(str); if (width > 0 && !(fmt & FMTF_adj)) { if (width > len) BLANKS(fn, arg, width - len, count); -- cgit v1.2.3