Permalink
Browse files

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).
  • Loading branch information...
1 parent 064b422 commit 7fc31186ee1afbc1fb5b47b02e412a7546711cb0 @sverker sverker committed Aug 21, 2012
Showing with 10 additions and 3 deletions.
  1. +10 −3 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);

0 comments on commit 7fc3118

Please sign in to comment.