Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/kern Do not cast memcpy arguments when the intention is ...



details:   https://anonhg.NetBSD.org/src/rev/ec1d0807622d
branches:  trunk
changeset: 1018522:ec1d0807622d
user:      joerg <joerg%NetBSD.org@localhost>
date:      Sat Feb 06 13:54:48 2021 +0000

description:
Do not cast memcpy arguments when the intention is unaligned access.
The standard is pretty explicit that misaligned pointers is UB and LLVM
does exploit the promised alignment on SPARC, resulting in kernel
crashes during early boot.

diffstat:

 sys/kern/subr_kmem.c |  10 +++++-----
 1 files changed, 5 insertions(+), 5 deletions(-)

diffs (43 lines):

diff -r d48e684a0430 -r ec1d0807622d sys/kern/subr_kmem.c
--- a/sys/kern/subr_kmem.c      Sat Feb 06 13:02:28 2021 +0000
+++ b/sys/kern/subr_kmem.c      Sat Feb 06 13:54:48 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: subr_kmem.c,v 1.81 2021/01/24 17:29:11 thorpej Exp $   */
+/*     $NetBSD: subr_kmem.c,v 1.82 2021/02/06 13:54:48 joerg Exp $     */
 
 /*
  * Copyright (c) 2009-2020 The NetBSD Foundation, Inc.
@@ -78,7 +78,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: subr_kmem.c,v 1.81 2021/01/24 17:29:11 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_kmem.c,v 1.82 2021/02/06 13:54:48 joerg Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_kmem.h"
@@ -505,7 +505,7 @@
 static void
 kmem_size_set(void *p, size_t sz)
 {
-       memcpy((size_t *)((uintptr_t)p + sz), &sz, sizeof(size_t));
+       memcpy((char *)p + sz, &sz, sizeof(size_t));
 }
 
 static void
@@ -513,13 +513,13 @@
 {
        size_t hsz;
 
-       memcpy(&hsz, (size_t *)((uintptr_t)p + sz), sizeof(size_t));
+       memcpy(&hsz, (char *)p + sz, sizeof(size_t));
 
        if (hsz != sz) {
                panic("kmem_free(%p, %zu) != allocated size %zu; overwrote?",
                    p, sz, hsz);
        }
 
-       memset((size_t *)((uintptr_t)p + sz), 0xff, sizeof(size_t));
+       memset((char *)p + sz, 0xff, sizeof(size_t));
 }
 #endif /* defined(KMEM_SIZE) */



Home | Main Index | Thread Index | Old Index