Tue Jan 7 21:46:47 2014 UTC ()
handle unaligned accesses


(christos)
diff -r1.2 -r1.3 src/external/bsd/nvi/dist/common/conv.c

cvs diff -r1.2 -r1.3 src/external/bsd/nvi/dist/common/conv.c (expand / switch to context diff)
--- src/external/bsd/nvi/dist/common/conv.c 2013/11/22 15:52:05 1.2
+++ src/external/bsd/nvi/dist/common/conv.c 2014/01/07 21:46:47 1.3
@@ -1,4 +1,4 @@
-/*	$NetBSD: conv.c,v 1.2 2013/11/22 15:52:05 christos Exp $ */
+/*	$NetBSD: conv.c,v 1.3 2014/01/07 21:46:47 christos Exp $ */
 /*-
  * Copyright (c) 1993, 1994
  *	The Regents of the University of California.  All rights reserved.
@@ -53,8 +53,10 @@
     BINC_RETW(NULL, *tostr, *blen, len);
 
     *tolen = len;
-    for (i = 0; i < len; ++i)
-	(*tostr)[i] = (u_char) str[i];
+    for (i = 0; i < len; ++i) {
+	CHAR_T w = (u_char)str[i];
+	memcpy((*tostr) + i, &w, sizeof(**tostr));
+    }
 
     *dst = cw->bp1;
 
@@ -131,11 +133,15 @@
 #endif
 
     for (i = 0, j = 0; j < len; ) {
-	n = mbrtowc((*tostr)+i, src+j, len-j, &mbs);
+	CHAR_T w;
+	n = mbrtowc(&w, src + j, len - j, &mbs);
+	memcpy((*tostr) + i, &w, sizeof(**tostr));
 	/* NULL character converted */
-	if (n == (size_t)-2) error = -(len-j);
-	if (n == (size_t)-1 || n == (size_t)-2)
-	    HANDLE_MBR_ERROR(n, mbs, (*tostr)[i], src[j]); 
+	if (n == (size_t)-2) error = -(len - j);
+	if (n == (size_t)-1 || n == (size_t)-2) {
+	    HANDLE_MBR_ERROR(n, mbs, w, src[j]); 
+	    memcpy((*tostr) + i, &w, sizeof(**tostr));
+	}
 	if (n == 0) n = 1;
 	j += n;
 	if (++i >= *blen) {
@@ -216,8 +222,11 @@
     BINC_RETC(NULL, *tostr, *blen, len);
 
     *tolen = len;
-    for (i = 0; i < len; ++i)
-	(*tostr)[i] = str[i];
+    for (i = 0; i < len; ++i) {
+	CHAR_T w;
+	memcpy(&w, str + i, sizeof(w));
+	(*tostr)[i] = w;
+    }
 
     *dst = cw->bp1;
 
@@ -282,9 +291,11 @@
 #endif
 
     for (i = 0, j = 0; i < (size_t)len; ++i) {
-	n = wcrtomb(dst+j, str[i], &mbs);
+	CHAR_T w;
+	memcpy(&w, str + i, sizeof(w));
+	n = wcrtomb(dst + j, w, &mbs);
 	if (n == (size_t)-1) 
-	   HANDLE_MBR_ERROR(n, mbs, dst[j], str[i]);
+	   HANDLE_MBR_ERROR(n, mbs, dst[j], w);
 	j += n;
 	if (buflen < j + MB_CUR_MAX) {
 	    if (id != (iconv_t)-1) {
@@ -297,7 +308,7 @@
 	}
     }
 
-    n = wcrtomb(dst+j, L'\0', &mbs);
+    n = wcrtomb(dst + j, L'\0', &mbs);
     j += n - 1;				/* don't count NUL at the end */
     *tolen = j;