Module Name:    src
Committed By:   maxv
Date:           Mon Nov 19 21:45:37 UTC 2018

Modified Files:
        src/lib/libnvmm: libnvmm.c

Log Message:
Fix error handling of realloc, and use memmove because the areas overlap;
noted by agc@. These _nvmm_area_add/delete functions don't make a lot of
sense right now and will likely be rewritten to match the behavior
expected by Qemu; but still fix for the time being.

Also fix a collision check while here.


To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 src/lib/libnvmm/libnvmm.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/lib/libnvmm/libnvmm.c
diff -u src/lib/libnvmm/libnvmm.c:1.1 src/lib/libnvmm/libnvmm.c:1.2
--- src/lib/libnvmm/libnvmm.c:1.1	Sat Nov 10 09:28:56 2018
+++ src/lib/libnvmm/libnvmm.c	Mon Nov 19 21:45:37 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: libnvmm.c,v 1.1 2018/11/10 09:28:56 maxv Exp $	*/
+/*	$NetBSD: libnvmm.c,v 1.2 2018/11/19 21:45:37 maxv Exp $	*/
 
 /*
  * Copyright (c) 2018 The NetBSD Foundation, Inc.
@@ -60,8 +60,8 @@ _nvmm_area_add(struct nvmm_machine *mach
 		    gpa < mach->areas[i].gpa + mach->areas[i].size) {
 			goto error;
 		}
-		if (gpa + size >= mach->areas[i].gpa &&
-		    gpa + size < mach->areas[i].gpa + mach->areas[i].size) {
+		if (gpa + size > mach->areas[i].gpa &&
+		    gpa + size <= mach->areas[i].gpa + mach->areas[i].size) {
 			goto error;
 		}
 		if (gpa < mach->areas[i].gpa &&
@@ -70,13 +70,13 @@ _nvmm_area_add(struct nvmm_machine *mach
 		}
 	}
 
-	mach->nareas++;
-	ptr = realloc(mach->areas, mach->nareas * sizeof(struct nvmm_area));
+	ptr = realloc(mach->areas, (mach->nareas + 1) *
+	    sizeof(struct nvmm_area));
 	if (ptr == NULL)
 		return -1;
 	mach->areas = ptr;
 
-	area = &mach->areas[mach->nareas-1];
+	area = &mach->areas[mach->nareas++];
 	area->gpa = gpa;
 	area->hva = hva;
 	area->size = size;
@@ -106,7 +106,7 @@ _nvmm_area_delete(struct nvmm_machine *m
 		return -1;
 	}
 
-	memcpy(&mach->areas[i], &mach->areas[i+1],
+	memmove(&mach->areas[i], &mach->areas[i+1],
 	    (mach->nareas - i - 1) * sizeof(struct nvmm_area));
 	mach->nareas--;
 

Reply via email to