From f74d743345c1293482d683ab0e57af4525c2e0d6 Mon Sep 17 00:00:00 2001 From: Micah Cowan Date: Sun, 3 Feb 2008 01:28:05 -0800 Subject: [PATCH] Handle error codes from mbtowc, wcwidth. --- src/progress.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/progress.c b/src/progress.c index d77f99b6..2db82fda 100644 --- a/src/progress.c +++ b/src/progress.c @@ -778,13 +778,21 @@ count_cols (const char *mbs) int bytes; int remaining = strlen(mbs); int cols = 0; + int wccols; while (*mbs != '\0') { bytes = mbtowc (&wc, mbs, remaining); + assert (bytes != 0); /* Only happens when *mbs == '\0' */ + if (bytes == -1) + { + /* Invalid sequence. We'll just have to fudge it. */ + return cols + remaining; + } mbs += bytes; remaining -= bytes; - cols += wcwidth(wc); + wccols = wcwidth(wc); + cols += (wccols == -1? 1 : wccols); } return cols; }