From 9310f981b0a0c1b854d86d5b0e59bb90eb4beedc Mon Sep 17 00:00:00 2001 From: cinap_lenrek Date: Wed, 14 Dec 2011 00:22:46 +0100 Subject: [PATCH] kernel: xalloc error handling --- sys/src/9/pc/devfloppy.c | 14 +++++++++++--- sys/src/9/pc/devi82365.c | 15 ++++++++++++--- sys/src/9/pc/mp.c | 11 ++++++----- sys/src/9/pc/trap.c | 6 ++++-- 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/sys/src/9/pc/devfloppy.c b/sys/src/9/pc/devfloppy.c index 00323073e..9ff866529 100644 --- a/sys/src/9/pc/devfloppy.c +++ b/sys/src/9/pc/devfloppy.c @@ -181,17 +181,25 @@ floppyreset(void) } /* - * 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 */ 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 diff --git a/sys/src/9/pc/devi82365.c b/sys/src/9/pc/devi82365.c index 8cdb6ef0f..0e05a6102 100644 --- a/sys/src/9/pc/devi82365.c +++ b/sys/src/9/pc/devi82365.c @@ -484,6 +484,10 @@ i82365probe(int x, int d, int dev) 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 @@ devi82365link(void) 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++){ diff --git a/sys/src/9/pc/mp.c b/sys/src/9/pc/mp.c index e10e6d9d1..a83cb1913 100644 --- a/sys/src/9/pc/mp.c +++ b/sys/src/9/pc/mp.c @@ -87,7 +87,8 @@ mkbus(PCMPbus* p) 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 @@ mkiointr(PCMPintr* p) 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 @@ mkiointr(PCMPintr* p) */ 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 @@ mpoverride(uchar** newp, uchar** e) 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++){ diff --git a/sys/src/9/pc/trap.c b/sys/src/9/pc/trap.c index 2b13b7399..7b0ebb847 100644 --- a/sys/src/9/pc/trap.c +++ b/sys/src/9/pc/trap.c @@ -40,7 +40,8 @@ intrenable(int irq, void (*f)(Ureg*, void*), void* a, int tbdf, char *name) 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 @@ trapenable(int vno, void (*f)(Ureg*, void*), void* a, char *name) 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;