aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSverker Eriksson <[email protected]>2012-08-21 17:08:08 +0200
committerSverker Eriksson <[email protected]>2012-08-21 17:08:08 +0200
commit7fc31186ee1afbc1fb5b47b02e412a7546711cb0 (patch)
treeef37d826dcb8d350d6d265b277051bb0fb00cf6e
parent064b42237d891d5fdcb6c1a351980b8291437618 (diff)
downloadotp-7fc31186ee1afbc1fb5b47b02e412a7546711cb0.tar.gz
otp-7fc31186ee1afbc1fb5b47b02e412a7546711cb0.tar.bz2
otp-7fc31186ee1afbc1fb5b47b02e412a7546711cb0.zip
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).
-rw-r--r--erts/lib_src/common/erl_printf_format.c13
1 files changed, 10 insertions, 3 deletions
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);