Source-Changes-HG archive

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

[src/trunk]: src/sys/kern kobj(9): Fix kobj_read_mem error branches.



details:   https://anonhg.NetBSD.org/src/rev/d397c4dcb432
branches:  trunk
changeset: 371873:d397c4dcb432
user:      riastradh <riastradh%NetBSD.org@localhost>
date:      Sat Oct 15 15:27:20 2022 +0000

description:
kobj(9): Fix kobj_read_mem error branches.

Rewrite positively to simplify logic: Write errors as branches,
normal case as straight-line code.

In the case where allocate=true but arithmetic overflow occurs, this
avoids trying to kmem_free null, which is forbidden.

diffstat:

 sys/kern/subr_kobj.c |  30 +++++++++++-------------------
 1 files changed, 11 insertions(+), 19 deletions(-)

diffs (67 lines):

diff -r 7100a8516680 -r d397c4dcb432 sys/kern/subr_kobj.c
--- a/sys/kern/subr_kobj.c      Sat Oct 15 15:23:24 2022 +0000
+++ b/sys/kern/subr_kobj.c      Sat Oct 15 15:27:20 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: subr_kobj.c,v 1.71 2022/10/15 15:23:24 riastradh Exp $ */
+/*     $NetBSD: subr_kobj.c,v 1.72 2022/10/15 15:27:20 riastradh Exp $ */
 
 /*
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -63,7 +63,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: subr_kobj.c,v 1.71 2022/10/15 15:23:24 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_kobj.c,v 1.72 2022/10/15 15:27:20 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_modular.h"
@@ -1145,7 +1145,7 @@
     bool allocate)
 {
        void *base = *basep;
-       int error;
+       int error = 0;
 
        KASSERT(ko->ko_source != NULL);
 
@@ -1154,31 +1154,23 @@
                    (unsigned long long)off);
                error = EINVAL;
                base = NULL;
+               goto out;
        } else if (ko->ko_memsize != -1 &&
            (size > ko->ko_memsize || off > ko->ko_memsize - size)) {
                kobj_error(ko, "preloaded object short");
                error = EINVAL;
                base = NULL;
-       } else if (allocate) {
-               base = kmem_alloc(size, KM_SLEEP);
-               error = 0;
-       } else {
-               error = 0;
-       }
-
-       if (error == 0) {
-               /* Copy the section */
-               memcpy(base, (uint8_t *)ko->ko_source + off, size);
-       }
-
-       if (allocate && error != 0) {
-               kmem_free(base, size);
-               base = NULL;
+               goto out;
        }
 
        if (allocate)
+               base = kmem_alloc(size, KM_SLEEP);
+
+       /* Copy the section */
+       memcpy(base, (uint8_t *)ko->ko_source + off, size);
+
+out:   if (allocate)
                *basep = base;
-
        return error;
 }
 



Home | Main Index | Thread Index | Old Index