git: 9front

Download patch

ref: 8e38accbd2d84b599bd868429fa1a045d07f0fe9
parent: 4261474a37e8114796dcabeacc7ccab79bcbc39b
author: cinap_lenrek <cinap_lenrek@centraldogma>
date: Tue Dec 13 19:22:46 EST 2011

kernel: xalloc error handling

--- a/sys/src/9/pc/devfloppy.c
+++ b/sys/src/9/pc/devfloppy.c
@@ -181,11 +181,14 @@
 	}
 
 	/*
-	 * Should check if this fails. Can do so
-	 * if there is no space <= 16MB for the DMA
+	 * Can fail if there is no space <= 16MB for the DMA
 	 * bounce buffer.
 	 */
-	dmainit(DMAchan, maxtsize);
+	if(dmainit(DMAchan, maxtsize)){
+		print("floppy: dmainit failed\n");
+		fl.ndrive = 0;
+		return;
+	}
 
 	/*
 	 *  allocate the drive storage
@@ -192,6 +195,11 @@
 	 */
 	fl.d = xalloc(fl.ndrive*sizeof(FDrive));
 	fl.selected = fl.d;
+	if(fl.d == nil){
+		print("floppy: can't allocate memory\n");
+		fl.ndrive = 0;
+		return;
+	}
 
 	/*
 	 *  stop the motors
--- a/sys/src/9/pc/devi82365.c
+++ b/sys/src/9/pc/devi82365.c
@@ -484,6 +484,10 @@
 		return 0;		/* no revision number, not possible */
 
 	cp = xalloc(sizeof(I82365));
+	if(cp == nil){
+		print("i82365probe: out of memory\n");
+		return 0;
+	}
 	cp->xreg = x;
 	cp->dreg = d;
 	cp->dev = dev;
@@ -611,12 +615,17 @@
 	if(ncontroller == 0)
 		return;
 
-	_pcmspecial = pcmcia_pcmspecial;
-	_pcmspecialclose = pcmcia_pcmspecialclose;
-
 	for(i = 0; i < ncontroller; i++)
 		nslot += controller[i]->nslot;
 	slot = xalloc(nslot * sizeof(PCMslot));
+	if(slot == nil){
+		print("i82365link: out of memory\n");
+		nslot = 0;
+		return;
+	}
+
+	_pcmspecial = pcmcia_pcmspecial;
+	_pcmspecialclose = pcmcia_pcmspecialclose;
 
 	lastslot = slot;
 	for(i = 0; i < ncontroller; i++){
--- a/sys/src/9/pc/mp.c
+++ b/sys/src/9/pc/mp.c
@@ -87,7 +87,8 @@
 	if(buses[i] == 0)
 		return 0;
 
-	bus = xalloc(sizeof(Bus));
+	if((bus = xalloc(sizeof(Bus))) == nil)
+		panic("mkbus: no memory for Bus");
 	if(mpbus)
 		mpbuslast->next = bus;
 	else
@@ -210,7 +211,8 @@
 	if((bus = mpgetbus(p->busno)) == 0)
 		return 0;
 
-	aintr = xalloc(sizeof(Aintr));
+	if((aintr = xalloc(sizeof(Aintr))) == nil)
+		panic("iointr: no memory for Aintr");
 	aintr->intr = p;
 
 	if(0)
@@ -224,8 +226,7 @@
 	 */
 	if(memcmp(mppcmp->product, "INTEL   X38MLST     ", 20) == 0){
 		if(p->busno == 1 && p->intin == 16 && p->irq == 1){
-			pcmpintr = malloc(sizeof(PCMPintr));
-			if(pcmpintr == nil)
+			if((pcmpintr = xalloc(sizeof(PCMPintr))) == nil)
 				panic("iointr: no memory for PCMPintr");
 			memmove(pcmpintr, p, sizeof(PCMPintr));
 			print("mkiointr: %20.20s bus %d intin %d irq %d\n",
@@ -538,7 +539,7 @@
 	
 	size = atoi(getconf("*mp"));
 	if(size == 0) panic("mpoverride: invalid size in *mp");
-	*newp = p = malloc(size);
+	*newp = p = xalloc(size);
 	if(p == nil) panic("mpoverride: can't allocate memory");
 	*e = p + size;
 	for(i = 0; ; i++){
--- a/sys/src/9/pc/trap.c
+++ b/sys/src/9/pc/trap.c
@@ -40,7 +40,8 @@
 		return;
 	}
 
-	v = xalloc(sizeof(Vctl));
+	if((v = xalloc(sizeof(Vctl))) == nil)
+		panic("intrenable: out of memory");
 	v->isintr = 1;
 	v->irq = irq;
 	v->tbdf = tbdf;
@@ -147,7 +148,8 @@
 
 	if(vno < 0 || vno >= VectorPIC)
 		panic("trapenable: vno %d", vno);
-	v = xalloc(sizeof(Vctl));
+	if((v = xalloc(sizeof(Vctl))) == nil)
+		panic("trapenable: out of memory");
 	v->tbdf = BUSUNKNOWN;
 	v->f = f;
 	v->a = a;
--