shithub: riscv

Download patch

ref: 8db5af02d80f906e5bb81db43b010ace1e03f2d1
parent: 47bb311d39f0cdd38067a31b51ec619af7584e56
author: cinap_lenrek <[email protected]>
date: Wed Jul 22 15:15:51 EDT 2015

kernel: make sure the swap device has a reasonable capacity in setswapchan()

--- a/sys/src/9/port/swap.c
+++ b/sys/src/9/port/swap.c
@@ -396,11 +396,13 @@
 	Dir d;
 	int n;
 
+	if(waserror()){
+		cclose(c);
+		nexterror();
+	}
 	if(swapimage.c != nil) {
-		if(swapalloc.free != conf.nswap){
-			cclose(c);
+		if(swapalloc.free != conf.nswap)
 			error(Einuse);
-		}
 		cclose(swapimage.c);
 		swapimage.c = nil;
 	}
@@ -411,11 +413,10 @@
 	 */
 	if(devtab[c->type]->dc != L'M'){
 		n = devtab[c->type]->stat(c, dirbuf, sizeof dirbuf);
-		if(n <= 0){
-			cclose(c);
+		if(n <= 0 || convM2D(dirbuf, n, &d, nil) == 0)
 			error("stat failed in setswapchan");
-		}
-		convM2D(dirbuf, n, &d, nil);
+		if(d.length < conf.nswppo*BY2PG)
+			error("swap device too small");
 		if(d.length < conf.nswap*BY2PG){
 			conf.nswap = d.length/BY2PG;
 			swapalloc.top = &swapalloc.swmap[conf.nswap];
@@ -424,4 +425,5 @@
 	}
 	c->flag &= ~CCACHE;
 	swapimage.c = c;
+	poperror();
 }