aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrooks Davis <brooks@FreeBSD.org>2024-11-27 17:38:42 +0000
committerBrooks Davis <brooks@FreeBSD.org>2024-12-13 21:05:19 +0000
commit3e9f61464ee36a01d60fa21572e3d44475f4cade (patch)
tree4b717a38f2ce0b99a4b0ec51eefb732804a18eef
parentf69b6410a632655896a4c31280604c4af5f1a4ed (diff)
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 (cherry picked from commit 56ef9c872bc5b086d73fed6317159e40be32d40e)
-rw-r--r--contrib/nvi/common/log.c22
1 files 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));