On Sat, Apr 12, 2014 at 08:42:22PM +0100, sin wrote:
> On Sat, Apr 12, 2014 at 09:02:05PM +0200, Silvan Jegen wrote:
> > True. I would suggest just adding checks and bailing out when the return
> > code is <0. Maybe something like the following?
> 
> Looks good.  Can you resend with an attachment?

Can do!

From: Silvan Jegen <s.je...@gmail.com>
Date: Sat, 12 Apr 2014 20:50:51 +0200
Subject: [PATCH] Wrap mbtowc to check for errors

---
 tr.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/tr.c b/tr.c
index aad2245..7579b6a 100644
--- a/tr.c
+++ b/tr.c
@@ -46,6 +46,17 @@ handleescapes(char *s)
 	}
 }
 
+static int
+xmbtowc(wchar_t *unicodep, const char *s)
+{
+	int rv;
+
+	rv = mbtowc(unicodep, s, 4);
+	if (rv < 0)
+			eprintf("mbtowc:");
+	return rv;
+}
+
 static void
 parsemapping(const char *set1, const char *set2, wchar_t *mappings)
 {
@@ -64,12 +75,12 @@ parsemapping(const char *set1, const char *set2, wchar_t *mappings)
 	while(*s1) {
 		if(*s1 == '\\')
 			handleescapes(++s1);
-		leftbytes = mbtowc(&runeleft, s1, 4);
+		leftbytes = xmbtowc(&runeleft, s1);
 		s1 += leftbytes;
 		if(*s2 == '\\')
 			handleescapes(++s2);
 		if(*s2 != '\0') {
-			rightbytes = mbtowc(&runeright, s2, 4);
+			rightbytes = xmbtowc(&runeright, s2);
 			s2 += rightbytes;
 		}
 		mappings[runeleft] = runeright;
@@ -85,7 +96,7 @@ maptonull(const wchar_t *mappings, char *in)
 
 	s = in;
 	while(*s) {
-		leftbytes = mbtowc(&runeleft, s, 4);
+		leftbytes = xmbtowc(&runeleft, s);
 		if(!mappings[runeleft])
 			putwchar(runeleft);
 		s += leftbytes;
@@ -101,7 +112,7 @@ maptoset(const wchar_t *mappings, char *in)
 
 	s = in;
 	while(*s) {
-		leftbytes = mbtowc(&runeleft, s, 4);
+		leftbytes = xmbtowc(&runeleft, s);
 		if(!mappings[runeleft])
 			putwchar(runeleft);
 		else
-- 
1.9.2

Reply via email to