git: 56ef9c872bc5 - main - nvi: use memmove to realign buffers

From: Brooks Davis <brooks_at_FreeBSD.org>
Date: Wed, 27 Nov 2024 18:20:27 UTC
The branch main has been updated by brooks:

URL: https://cgit.FreeBSD.org/src/commit/?id=56ef9c872bc5b086d73fed6317159e40be32d40e

commit 56ef9c872bc5b086d73fed6317159e40be32d40e
Author:     Brooks Davis <brooks@FreeBSD.org>
AuthorDate: 2024-11-27 17:38:42 +0000
Commit:     Brooks Davis <brooks@FreeBSD.org>
CommitDate: 2024-11-27 18:20:04 +0000

    nvi: use memmove to realign buffers
    
    Replace a rather convoluted realignment algorithm with memmove().  In
    addition to being hard to understand, the code would read beyond the end
    of the input buffer in certain conditions (found on CheriBSD).
    
    Sponsored by:   DARPA
    Pull Request:   https://github.com/lichray/nvi2/pull/122
---
 contrib/nvi/common/log.c | 22 +++++-----------------
 1 file changed, 5 insertions(+), 17 deletions(-)

diff --git a/contrib/nvi/common/log.c b/contrib/nvi/common/log.c
index 96b246efad02..d9b142b16d01 100644
--- a/contrib/nvi/common/log.c
+++ b/contrib/nvi/common/log.c
@@ -706,30 +706,18 @@ apply_with(int (*db_func)(SCR *, recno_t, CHAR_T *, size_t), SCR *sp,
     recno_t lno, u_char *p, size_t len)
 {
 #ifdef USE_WIDECHAR
-	typedef unsigned long nword;
-
 	static size_t blen;
-	static nword *bp;
-	nword *lp = (nword *)((uintptr_t)p / sizeof(nword) * sizeof(nword));
-
-	if (lp != (nword *)p) {
-		int offl = ((uintptr_t)p - (uintptr_t)lp) << 3;
-		int offr = (sizeof(nword) << 3) - offl;
-		size_t i, cnt = (len + sizeof(nword) / 2) / sizeof(nword);
+	static u_char *bp;
 
+	if (!__builtin_is_aligned(p, sizeof(unsigned long))) {
 		if (len > blen) {
 			blen = p2roundup(MAX(len, 512));
-			REALLOC(sp, bp, nword *, blen);
+			REALLOC(sp, bp, u_char *, blen);
 			if (bp == NULL)
 				return (1);
 		}
-		for (i = 0; i < cnt; ++i)
-#if BYTE_ORDER == BIG_ENDIAN
-			bp[i] = (lp[i] << offl) ^ (lp[i+1] >> offr);
-#else
-			bp[i] = (lp[i] >> offl) ^ (lp[i+1] << offr);
-#endif
-		p = (u_char *)bp;
+		memmove(bp, p, len);
+		p = bp;
 	}
 #endif
 	return db_func(sp, lno, (CHAR_T *)p, len / sizeof(CHAR_T));