git: 9front

Download patch

ref: 4f1e758b105139ec47911442174ef167fd4004cf
parent: b34736db8677bbd3e4e65d3febc4acd7a24442fd
author: cinap_lenrek <cinap_lenrek@centraldogma>
date: Mon Dec 12 11:55:26 EST 2011

kernel: fix inproper use of malloc/smalloc

--- a/sys/src/9/pc/devarch.c
+++ b/sys/src/9/pc/devarch.c
@@ -871,7 +871,7 @@
 	int n;
 	char *buf, *p, *ep;
 
-	p = buf = malloc(READSTR);
+	p = buf = smalloc(READSTR);
 	ep = p + READSTR;
 	p = seprint(p, ep, "cpu %s %lud%s\n",
 		cputype->name, (ulong)(m->cpuhz+999999)/1000000,
--- a/sys/src/9/pc/devi82365.c
+++ b/sys/src/9/pc/devi82365.c
@@ -765,7 +765,7 @@
 	case Qattr:
 		return pcmread(SLOTNO(c), TYPE(c) == Qattr, a, n, off);
 	case Qctl:
-		buf = p = malloc(READSTR);
+		buf = p = smalloc(READSTR);
 		e = p + READSTR;
 		pp = slot + SLOTNO(c);
 
--- a/sys/src/9/pc/devpccard.c
+++ b/sys/src/9/pc/devpccard.c
@@ -1292,7 +1292,7 @@
 		return devdirread(c, a, n, 0, 0, pccardgen);
 
 	case Qctl:
-		buf = p = malloc(READSTR);
+		buf = p = smalloc(READSTR);
 		buf[0] = 0;
 		e = p + READSTR;
 
--- a/sys/src/9/pc/devvga.c
+++ b/sys/src/9/pc/devvga.c
@@ -169,7 +169,7 @@
 	case Qvgactl:
 		scr = &vgascreen[0];
 
-		p = malloc(READSTR);
+		p = smalloc(READSTR);
 		if(waserror()){
 			free(p);
 			nexterror();
--- a/sys/src/9/pc/ether2114x.c
+++ b/sys/src/9/pc/ether2114x.c
@@ -322,7 +322,7 @@
 	if(n == 0)
 		return 0;
 
-	p = malloc(READSTR);
+	p = smalloc(READSTR);
 	l = snprint(p, READSTR, "Overflow: %lud\n", ctlr->of);
 	l += snprint(p+l, READSTR-l, "Ru: %lud\n", ctlr->ru);
 	l += snprint(p+l, READSTR-l, "Rps: %lud\n", ctlr->rps);
--- a/sys/src/9/pc/ether79c970.c
+++ b/sys/src/9/pc/ether79c970.c
@@ -204,7 +204,7 @@
 	if(n == 0)
 		return 0;
 
-	p = malloc(READSTR);
+	p = smalloc(READSTR);
 	len = snprint(p, READSTR, "Rxbuff: %ld\n", ctlr->rxbuff);
 	len += snprint(p+len, READSTR-len, "Crc: %ld\n", ctlr->crc);
 	len += snprint(p+len, READSTR-len, "Oflo: %ld\n", ctlr->oflo);
--- a/sys/src/9/pc/ether8139.c
+++ b/sys/src/9/pc/ether8139.c
@@ -310,7 +310,7 @@
 	Ctlr *ctlr;
 
 	ctlr = edev->ctlr;
-	p = malloc(READSTR);
+	p = smalloc(READSTR);
 	l = snprint(p, READSTR, "rcr %#8.8ux\n", ctlr->rcr);
 	l += snprint(p+l, READSTR-l, "multicast %ud\n", ctlr->mcast);
 	l += snprint(p+l, READSTR-l, "ierrs %d\n", ctlr->ierrs);
--- a/sys/src/9/pc/ether8169.c
+++ b/sys/src/9/pc/ether8169.c
@@ -494,10 +494,11 @@
 	Dtcc *dtcc;
 	int i, l, r, timeo;
 
+	p = smalloc(READSTR);
+
 	ctlr = edev->ctlr;
 	qlock(&ctlr->slock);
 
-	p = nil;
 	if(waserror()){
 		qunlock(&ctlr->slock);
 		free(p);
@@ -524,11 +525,9 @@
 	if(n == 0){
 		qunlock(&ctlr->slock);
 		poperror();
+		free(p);
 		return 0;
 	}
-
-	if((p = malloc(READSTR)) == nil)
-		error(Enomem);
 
 	l = snprint(p, READSTR, "TxOk: %llud\n", dtcc->txok);
 	l += snprint(p+l, READSTR-l, "RxOk: %llud\n", dtcc->rxok);
--- a/sys/src/9/pc/ether82543gc.c
+++ b/sys/src/9/pc/ether82543gc.c
@@ -504,9 +504,10 @@
 	int i, l, r;
 	uvlong tuvl, ruvl;
 
+	p = smalloc(READSTR);
+
 	ctlr = edev->ctlr;
 	lock(&ctlr->slock);
-	p = malloc(READSTR);
 	l = 0;
 	for(i = 0; i < Nstatistics; i++){
 		r = csr32r(ctlr, Statistics+i*4);
--- a/sys/src/9/pc/ether82557.c
+++ b/sys/src/9/pc/ether82557.c
@@ -434,7 +434,7 @@
 	memmove(dump, ctlr->dump, sizeof(dump));
 	unlock(&ctlr->dlock);
 
-	p = malloc(READSTR);
+	p = smalloc(READSTR);
 	len = snprint(p, READSTR, "transmit good frames: %lud\n", dump[0]);
 	len += snprint(p+len, READSTR-len, "transmit maximum collisions errors: %lud\n", dump[1]);
 	len += snprint(p+len, READSTR-len, "transmit late collisions errors: %lud\n", dump[2]);
--- a/sys/src/9/pc/ether82563.c
+++ b/sys/src/9/pc/ether82563.c
@@ -647,10 +647,11 @@
 	Ctlr *ctlr;
 	Rbpool *b;
 
+	p = s = smalloc(READSTR);
+	e = p + READSTR;
+
 	ctlr = edev->ctlr;
 	qlock(&ctlr->slock);
-	p = s = malloc(READSTR);
-	e = p + READSTR;
 
 	for(i = 0; i < Nstatistics; i++){
 		r = csr32r(ctlr, Statistics + i*4);
--- a/sys/src/9/pc/ether82598.c
+++ b/sys/src/9/pc/ether82598.c
@@ -336,10 +336,10 @@
 	char *s, *p, *q;
 	Ctlr *c;
 
-	c = e->ctlr;
-	p = s = malloc(READSTR);
+	p = s = smalloc(READSTR);
 	q = p + READSTR;
 
+	c = e->ctlr;
 	readstats(c);
 	for(i = 0; i < nelem(stattab); i++)
 		if(c->stats[i] > 0)
--- a/sys/src/9/pc/ether83815.c
+++ b/sys/src/9/pc/ether83815.c
@@ -360,7 +360,7 @@
 	if(n == 0)
 		return 0;
 
-	p = malloc(READSTR);
+	p = smalloc(READSTR);
 	l = snprint(p, READSTR, "Rxa: %lud\n", ctlr->rxa);
 	l += snprint(p+l, READSTR-l, "Rxo: %lud\n", ctlr->rxo);
 	l += snprint(p+l, READSTR-l, "Rlong: %lud\n", ctlr->rlong);
--- a/sys/src/9/pc/etherdp83820.c
+++ b/sys/src/9/pc/etherdp83820.c
@@ -920,7 +920,7 @@
 	if(n == 0)
 		return 0;
 
-	p = malloc(READSTR);
+	p = smalloc(READSTR);
 	l = 0;
 	for(i = 0; i < Nmibd; i++){
 		r = csr32r(ctlr, Mibd+(i*sizeof(int)));
--- a/sys/src/9/pc/etherelnk3.c
+++ b/sys/src/9/pc/etherelnk3.c
@@ -1174,7 +1174,7 @@
 	statistics(ether);
 	iunlock(&ctlr->wlock);
 
-	p = malloc(READSTR);
+	p = smalloc(READSTR);
 	len = snprint(p, READSTR, "interrupts: %lud\n", ctlr->interrupts);
 	len += snprint(p+len, READSTR-len, "bogusinterrupts: %lud\n", ctlr->bogusinterrupts);
 	len += snprint(p+len, READSTR-len, "timer: %lud %lud\n",
--- a/sys/src/9/pc/etherga620.c
+++ b/sys/src/9/pc/etherga620.c
@@ -343,7 +343,7 @@
 
 	if(n == 0)
 		return 0;
-	p = malloc(READSTR);
+	p = smalloc(READSTR);
 	l = 0;
 	for(i = 0; i < 256; i++){
 		if((r = ctlr->gib->statistics[i]) == 0)
@@ -1173,6 +1173,10 @@
 		}
 
 		ctlr = malloc(sizeof(Ctlr));
+		if(ctlr == nil){
+			print("ga620: can't allocate memory\n");
+			continue;
+		}
 		ctlr->port = p->mem[0].bar & ~0x0F;
 		ctlr->pcidev = p;
 		ctlr->id = p->did<<16 | p->vid;
--- a/sys/src/9/pc/etherigbe.c
+++ b/sys/src/9/pc/etherigbe.c
@@ -599,10 +599,10 @@
 	int i, l, r;
 	uvlong tuvl, ruvl;
 
+	p = smalloc(READSTR);
+	l = 0;
 	ctlr = edev->ctlr;
 	qlock(&ctlr->slock);
-	p = malloc(READSTR);
-	l = 0;
 	for(i = 0; i < Nstatistics; i++){
 		r = csr32r(ctlr, Statistics+i*4);
 		if((s = statistics[i]) == nil)
@@ -1965,6 +1965,10 @@
 				break;
 		}
 		ctlr = malloc(sizeof(Ctlr));
+		if(ctlr == nil){
+			print("igbe: can't allocate memory\n");
+			continue;
+		}
 		ctlr->port = p->mem[0].bar & ~0x0F;
 		ctlr->pcidev = p;
 		ctlr->id = (p->did<<16)|p->vid;
--- a/sys/src/9/pc/etherm10g.c
+++ b/sys/src/9/pc/etherm10g.c
@@ -1362,19 +1362,15 @@
 static long
 m10gifstat(Ether *e, void *v, long n, ulong off)
 {
-	int l;
 	char *p;
 	Ctlr *c;
 	Stats s;
 
 	c = e->ctlr;
-	p = malloc(READSTR+1);
-	l = 0;
+	p = smalloc(READSTR);
 	/* no point in locking this because this is done via dma. */
 	memmove(&s, c->stats, sizeof s);
-
-	// l +=
-	snprint(p+l, READSTR,
+	snprint(p, READSTR,
 		"txcnt = %lud\n"  "linkstat = %lud\n" 	"dlink = %lud\n"
 		"derror = %lud\n" "drunt = %lud\n" 	"doverrun = %lud\n"
 		"dnosm = %lud\n"  "dnobg = %lud\n"	"nrdma = %lud\n"
--- a/sys/src/9/pc/ethersmc.c
+++ b/sys/src/9/pc/ethersmc.c
@@ -644,7 +644,6 @@
 		return 0;
 
 	ctlr = ether->ctlr;
-	p = malloc(READSTR);
 
 	s = 0;
 	if (ctlr->rev > 0) {
@@ -660,6 +659,7 @@
 		}
 	}
 
+	p = smalloc(READSTR);
 	len = snprint(p, READSTR, "rev: 91c%s\n", (s) ? s : "???");
 	len += snprint(p + len, READSTR - len, "rxovrn: %uld\n", ctlr->rovrn);
 	len += snprint(p + len, READSTR - len, "lcar: %uld\n", ctlr->lcar);
--- a/sys/src/9/pc/ethervgbe.c
+++ b/sys/src/9/pc/ethervgbe.c
@@ -401,9 +401,8 @@
 
 	ctlr = edev->ctlr;
 
-	p = malloc(READSTR);
-	l = 0;
-	l += snprint(p+l, READSTR-l, "tx: %uld\n", ctlr->stats.tx);
+	p = smalloc(READSTR);
+	l = snprint(p+l, READSTR-l, "tx: %uld\n", ctlr->stats.tx);
 	l += snprint(p+l, READSTR-l, "tx [errs]: %uld\n", ctlr->stats.txe);
 	l += snprint(p+l, READSTR-l, "rx: %uld\n", ctlr->stats.rx);
 	l += snprint(p+l, READSTR-l, "intr: %uld\n", ctlr->stats.intr);
--- a/sys/src/9/pc/ethervt6102.c
+++ b/sys/src/9/pc/ethervt6102.c
@@ -346,7 +346,7 @@
 
 	ctlr = edev->ctlr;
 
-	p = malloc(READSTR);
+	p = smalloc(READSTR);
 	l = 0;
 	for(i = 0; i < Nrxstats; i++){
 		l += snprint(p+l, READSTR-l, "%s: %ud\n",
--- a/sys/src/9/pc/main.c
+++ b/sys/src/9/pc/main.c
@@ -603,7 +603,7 @@
 
 	/* copy local descriptor table */
 	if(up->ldt != nil && up->nldt > 0){
-		p->ldt = malloc(sizeof(Segdesc) * up->nldt);
+		p->ldt = smalloc(sizeof(Segdesc) * up->nldt);
 		memmove(p->ldt, up->ldt, sizeof(Segdesc) * up->nldt);
 		p->nldt = up->nldt;
 	}
--- a/sys/src/9/pc/mmu.c
+++ b/sys/src/9/pc/mmu.c
@@ -88,6 +88,8 @@
 	m->pdb[PDX(VPT)] = PADDR(m->pdb)|PTEWRITE|PTEVALID;
 	
 	m->tss = malloc(sizeof(Tss));
+	if(m->tss == nil)
+		panic("mmuinit: no memory for Tss");
 	memset(m->tss, 0, sizeof(Tss));
 	m->tss->iomap = 0xDFFF<<16;
 
--- a/sys/src/9/pc/mp.c
+++ b/sys/src/9/pc/mp.c
@@ -225,6 +225,8 @@
 	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)
+				panic("iointr: no memory for PCMPintr");
 			memmove(pcmpintr, p, sizeof(PCMPintr));
 			print("mkiointr: %20.20s bus %d intin %d irq %d\n",
 				(char*)mppcmp->product,
--- a/sys/src/9/pc/piix4smbus.c
+++ b/sys/src/9/pc/piix4smbus.c
@@ -181,7 +181,9 @@
 	if(p == nil)
 		return nil;
 
-	s = smalloc(sizeof(*s));
+	s = malloc(sizeof(*s));
+	if(s == nil)
+		panic("piix4smbus: no memory for SMBus");
 	memmove(s, &smbusproto, sizeof(*s));
 	s->arg = p;
 
--- a/sys/src/9/pc/sdodin.c
+++ b/sys/src/9/pc/sdodin.c
@@ -2551,7 +2551,11 @@
 static void
 initmem(Ctlr *c)
 {
-	c->fis = smalloc(0x800 + 0x100*16);	/* §6.1.9.3 */
+	c->fis = malloc(0x800 + 0x100*16);	/* §6.1.9.3 */
+	c->cl = malloc(nelem(c->cq)*sizeof *c->cl);
+	c->cmdtab = malloc(Nctlrdrv*sizeof *c->cmdtab);
+	if(c->fis == nil || c->cl == nil || c->cmdtab == nil)
+		panic("sdodin: no memory");
 	c->reg[Fisbase + 0] = PCIWADDR(c->fis);
 	c->reg[Fisbase + 1] = Pciwaddrh(c->fis);
 	c->reg[Cqbase + 0] = PCIWADDR(c->cq);
@@ -2560,10 +2564,8 @@
 	c->reg[Dqbase + 0] = PCIWADDR(c->dq);
 	c->reg[Dqbase + 1] = Pciwaddrh(c->dq);
 	c->reg[Dqcfg] = Dqen | nelem(c->dq);
-	c->cl = smalloc(nelem(c->cq)*sizeof *c->cl);
 	c->reg[Clbase + 0] = PCIWADDR(c->cl);
 	c->reg[Clbase + 1] = Pciwaddrh(c->cl);
-	c->cmdtab = smalloc(Nctlrdrv*sizeof *c->cmdtab);
 }
 
 /* §5.1.2 */
--- a/sys/src/9/pc/uartaxp.c
+++ b/sys/src/9/pc/uartaxp.c
@@ -262,7 +262,7 @@
 
 	ccb = ((Cc*)(uart->regs))->ccb;
 
-	p = malloc(READSTR);
+	p = smalloc(READSTR);
 	bs = ccb->bs;
 	fstat = ccb->df;
 	ms = ccb->ms;
--- a/sys/src/9/pc/uarti8250.c
+++ b/sys/src/9/pc/uarti8250.c
@@ -171,7 +171,7 @@
 	uchar ier, lcr, mcr, msr;
 
 	ctlr = uart->regs;
-	p = malloc(READSTR);
+	p = smalloc(READSTR);
 	mcr = ctlr->sticky[Mcr];
 	msr = csr8r(ctlr, Msr);
 	ier = ctlr->sticky[Ier];
--- a/sys/src/9/pc/usbehcipc.c
+++ b/sys/src/9/pc/usbehcipc.c
@@ -180,7 +180,11 @@
 		dprint("usbehci: %#x %#x: port %#lux size %#x irq %d\n",
 			p->vid, p->did, io, p->mem[0].size, p->intl);
 
-		ctlr = smalloc(sizeof(Ctlr));
+		ctlr = malloc(sizeof(Ctlr));
+		if(ctlr == nil){
+			print("usbehci: no memory\n");
+			continue;
+		}
 		ctlr->pcidev = p;
 		capio = ctlr->capio = vmap(io, p->mem[0].size);
 		ctlr->opio = (Eopio*)((uintptr)capio + (capio->cap & 0xff));
--- a/sys/src/9/pc/usbohci.c
+++ b/sys/src/9/pc/usbohci.c
@@ -2383,15 +2383,19 @@
 		dprint("ohci: %x/%x port 0x%lux size 0x%x irq %d\n",
 			p->vid, p->did, mem, p->mem[0].size, p->intl);
 		if(mem == 0){
-			print("ohci: failed to map registers\n");
+			print("usbohci: failed to map registers\n");
 			continue;
 		}
 		if(p->intl == 0xFF || p->intl == 0) {
-			print("ohci: no irq assigned for port %#lux\n", mem);
+			print("usbohci: no irq assigned for port %#lux\n", mem);
 			continue;
 		}
 
-		ctlr = smalloc(sizeof(Ctlr));
+		ctlr = malloc(sizeof(Ctlr));
+		if(ctlr == nil){
+			print("usbohci: no memory\n");
+			continue;
+		}
 		ctlr->pcidev = p;
 		ctlr->ohci = vmap(mem, p->mem[0].size);
 		dprint("scanpci: ctlr %#p, ohci %#p\n", ctlr, ctlr->ohci);
@@ -2403,7 +2407,7 @@
 				break;
 			}
 		if(i == Nhcis)
-			print("ohci: bug: no more controllers\n");
+			print("usbohci: bug: no more controllers\n");
 	}
 }
 
--- a/sys/src/9/pc/usbuhci.c
+++ b/sys/src/9/pc/usbuhci.c
@@ -2139,7 +2139,12 @@
 		dprint("uhci: %#x %#x: port %#ux size %#x irq %d\n",
 			p->vid, p->did, io, p->mem[4].size, p->intl);
 
-		ctlr = smalloc(sizeof(Ctlr));
+		ctlr = malloc(sizeof(Ctlr));
+		if(ctlr == nil){
+			iofree(io);
+			print("usbuhci: no memory\n");
+			continue;
+		}
 		ctlr->pcidev = p;
 		ctlr->port = io;
 		for(i = 0; i < Nhcis; i++)
@@ -2148,7 +2153,7 @@
 				break;
 			}
 		if(i == Nhcis)
-			print("uhci: bug: no more controllers\n");
+			print("usbuhci: bug: no more controllers\n");
 	}
 }
 
--- a/sys/src/9/pc/wavelan.c
+++ b/sys/src/9/pc/wavelan.c
@@ -169,7 +169,7 @@
 	static ushort off[] = { WR_Off0, WR_Off1 };
 
 	if(chan != 0 && chan != 1)
-		panic("wavelan: bad chan\n");
+		panic("wavelan: bad chan");
 	csr_outs(ctlr, sel[chan], id);
 	csr_outs(ctlr, off[chan], offset);
 	for (i=0; i<WTmOut; i++){
@@ -846,7 +846,7 @@
 	if(n == 0 || offset != 0)
 		return 0;
 
-	p = malloc(READSTR);
+	p = smalloc(READSTR);
 	l = 0;
 
 	PRINTSTAT("Signal: %d\n", ctlr->signal-149);
--- a/sys/src/9/port/devuart.c
+++ b/sys/src/9/port/devuart.c
@@ -509,7 +509,7 @@
 		poperror();
 		break;
 	case Nctlqid:
-		cmd = malloc(n+1);
+		cmd = smalloc(n+1);
 		memmove(cmd, buf, n);
 		cmd[n] = 0;
 		qlock(p);
--- a/sys/src/9/port/portclock.c
+++ b/sys/src/9/port/portclock.c
@@ -214,6 +214,8 @@
 	 */
 	todinit();
 	t = malloc(sizeof(*t));
+	if(t == nil)
+		panic("timersinit: no memory for Timer");
 	t->tmode = Tperiodic;
 	t->tt = nil;
 	t->tns = 1000000000/HZ;
@@ -229,6 +231,8 @@
 
 	/* Synchronize to hztimer if ms is 0 */
 	nt = malloc(sizeof(Timer));
+	if(nt == nil)
+		panic("addclock0link: no memory for Timer");
 	if(ms == 0)
 		ms = 1000/HZ;
 	nt->tns = (vlong)ms*1000000LL;
--