Fix utf16le_strlen() and recursive error messages
authorEric Biggers <ebiggers3@gmail.com>
Wed, 20 Mar 2013 05:51:07 +0000 (00:51 -0500)
committerEric Biggers <ebiggers3@gmail.com>
Wed, 20 Mar 2013 05:51:07 +0000 (00:51 -0500)
src/encoding.c
src/util.c

index e0f4e2d..980e4cf 100644 (file)
@@ -101,6 +101,11 @@ put_iconv(iconv_t *cd)
        errno = errno_save;
 }
 
+/* Prevent printing an error message if a character conversion error occurs
+ * while printing an error message.  (This variable is not per-thread but it
+ * doesn't matter too much since it's just the error messages.) */
+static bool error_message_being_printed = false;
+
 #define DEFINE_CHAR_CONVERSION_FUNCTIONS(varname1, longname1, chartype1,\
                                         varname2, longname2, chartype2,\
                                         worst_case_len_expr,           \
@@ -128,7 +133,11 @@ varname1##_to_##varname2##_nbytes(const chartype1 *in, size_t in_nbytes,\
                                                                        \
        len = iconv(*cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft); \
        if (len == (size_t)-1) {                                        \
-               err_msg;                                                \
+               if (!error_message_being_printed) {                     \
+                       error_message_being_printed = true;             \
+                       err_msg;                                        \
+                       error_message_being_printed = false;            \
+               }                                                       \
                ret = err_return;                                       \
        } else {                                                        \
                *out_nbytes_ret = sizeof(buf) - outbytesleft;           \
index eb0ea9a..3c682ce 100644 (file)
@@ -53,7 +53,7 @@ static size_t utf16le_strlen(const utf16lechar *s)
        const utf16lechar *p = s;
        while (*p)
                p++;
-       return (p - s) / sizeof(utf16lechar);
+       return (p - s) * sizeof(utf16lechar);
 }
 
 /* Handle %W for UTF16-LE printing and %U for UTF-8 printing.