shithub: riscv

Download patch

ref: 8cb8043d0ebeadb0d424fb1051a49573430322a3
parent: 304ee3b2b55971d9f5f820511fb9cdadcb77b818
author: cinap_lenrek <cinap_lenrek@centraldogma>
date: Mon Dec 12 14:17:58 EST 2011

kernel: fix more malloc/smalloc errors

--- a/sys/src/9/port/devaoe.c
+++ b/sys/src/9/port/devaoe.c
@@ -248,7 +248,7 @@
 {
 	Srb *srb;
 
-	srb = malloc(sizeof *srb+sz);
+	srb = smalloc(sizeof *srb+sz);
 	srb->state = Alloc;
 	srb->dp = srb->data = srb+1;
 	srb->ticksent = Ticks;
@@ -260,7 +260,7 @@
 {
 	Srb *srb;
 
-	srb = malloc(sizeof *srb);
+	srb = smalloc(sizeof *srb);
 	srb->state = Alloc;
 	srb->dp = srb->data = db;
 	srb->ticksent = Ticks;
@@ -1454,7 +1454,7 @@
 	if(len > sizeof d->config)
 		error(Etoobig);
 	srb = srballoc(len);
-	s = malloc(len);
+	s = smalloc(len);
 	memmove(s, db, len);
 	if(waserror()){
 		srbfree(srb);
--- a/sys/src/9/port/devaudio.c
+++ b/sys/src/9/port/devaudio.c
@@ -78,6 +78,10 @@
 		probe = &audioprobes[i];
 
 		for(;;){
+			if(*pp == nil){
+				print("audio: no memory\n");
+				break;
+			}
 			memset(*pp, 0, sizeof(Audio));
 			(*pp)->ctlrno = ctlrno;
 			(*pp)->name = probe->name;
--- a/sys/src/9/port/devbridge.c
+++ b/sys/src/9/port/devbridge.c
@@ -780,9 +780,11 @@
 	Centry *ce;
 	char c;
 
+	buf = smalloc(n);
 	qlock(b);
 	if(waserror()) {
 		qunlock(b);
+		free(buf);
 		nexterror();
 	}
 	sec = TK2SEC(m->ticks);
@@ -793,7 +795,6 @@
 	
 	n *= 51;	// change if print format is changed
 	n += 10;	// some slop at the end
-	buf = malloc(n);
 	p = buf;
 	ep = buf + n;
 	ce = b->cache;
@@ -808,7 +809,6 @@
 	*p = 0;
 	poperror();
 	qunlock(b);
-
 	return buf;
 }
 
--- a/sys/src/9/port/devcons.c
+++ b/sys/src/9/port/devcons.c
@@ -629,9 +629,7 @@
 		return randomread(buf, n);
 
 	case Qdrivers:
-		b = malloc(READSTR);
-		if(b == nil)
-			error(Enomem);
+		b = smalloc(READSTR);
 		k = 0;
 		for(i = 0; devtab[i] != nil; i++)
 			k += snprint(b+k, READSTR-k, "#%C %s\n",
@@ -641,8 +639,8 @@
 			nexterror();
 		}
 		n = readstr((ulong)offset, buf, n, b);
-		free(b);
 		poperror();
+		free(b);
 		return n;
 
 	case Qzero:
--- a/sys/src/9/port/devflash.c
+++ b/sys/src/9/port/devflash.c
@@ -240,7 +240,7 @@
 			error(Eio);
 		return n;
 	case Qctl:
-		s = malloc(READSTR);
+		s = smalloc(READSTR);
 		if(waserror()){
 			free(s);
 			nexterror();
@@ -475,7 +475,11 @@
 {
 	Flashtype *f, **l;
 
-	f = (Flashtype*)malloc(sizeof(*f));
+	f = malloc(sizeof(*f));
+	if(f == nil){
+		print("addflashcard: no memory for Flashtype\n");
+		return;
+	}
 	f->name = name;
 	f->reset = reset;
 	f->next = nil;
--- a/sys/src/9/port/devpnp.c
+++ b/sys/src/9/port/devpnp.c
@@ -193,7 +193,9 @@
 		l = &c->next;
 	}
 	if(create) {
-		*l = nc = malloc(sizeof(Card));
+		if((nc = malloc(sizeof(Card))) == nil)
+			panic("pnp: no memory for Card");
+		*l = nc;
 		nc->next = c;
 		nc->csn = csn;
 		c = nc;
--- a/sys/src/9/port/devproc.c
+++ b/sys/src/9/port/devproc.c
@@ -430,7 +430,7 @@
 	case Qns:
 		if(omode != OREAD)
 			error(Eperm);
-		c->aux = malloc(sizeof(Mntwalk));
+		c->aux = smalloc(sizeof(Mntwalk));
 		break;
 
 	case Qnotepg:
--- a/sys/src/9/port/devsd.c
+++ b/sys/src/9/port/devsd.c
@@ -1226,8 +1226,7 @@
 		error(Eperm);
 	case Qtopctl:
 		m = 64*1024;	/* room for register dumps */
-		p = buf = malloc(m);
-		assert(p);
+		p = buf = smalloc(m);
 		e = p + m;
 		qlock(&devslock);
 		for(i = 0; i < nelem(devs); i++){
@@ -1253,7 +1252,7 @@
 
 		unit = sdev->unit[UNIT(c->qid)];
 		m = 16*1024;	/* room for register dumps */
-		p = malloc(m);
+		p = smalloc(m);
 		l = snprint(p, m, "inquiry %.48s\n",
 			(char*)unit->inquiry+8);
 		qlock(&unit->ctl);
@@ -1790,6 +1789,8 @@
 	Devport *p;
 
 	p = malloc((dc->nports + 1) * sizeof(Devport));
+	if(p == nil)
+		panic("sd: no memory for Devport");
 	if(dc->nports > 0){
 		memmove(p, dc->ports, dc->nports * sizeof(Devport));
 		free(dc->ports);
--- a/sys/src/9/port/devwd.c
+++ b/sys/src/9/port/devwd.c
@@ -76,7 +76,7 @@
 		if(wd == nil || wd->stat == nil)
 			return 0;
 
-		p = malloc(READSTR);
+		p = smalloc(READSTR);
 		if(waserror()){
 			free(p);
 			nexterror();
--- a/sys/src/9/port/log.c
+++ b/sys/src/9/port/log.c
@@ -21,7 +21,7 @@
 		if(alog->minread == 0)
 			alog->minread = 1;
 		if(alog->buf == nil)
-			alog->buf = malloc(alog->nlog);
+			alog->buf = smalloc(alog->nlog);
 		alog->rptr = alog->buf;
 		alog->end = alog->buf + alog->nlog;
 		alog->len = 0;
--- a/sys/src/9/port/netif.c
+++ b/sys/src/9/port/netif.c
@@ -212,7 +212,7 @@
 	case Nctlqid:
 		return readnum(offset, a, n, NETID(c->qid.path), NUMSIZE);
 	case Nstatqid:
-		p = malloc(READSTR);
+		p = smalloc(READSTR);
 		j = snprint(p, READSTR, "in: %llud\n", nif->inpackets);
 		j += snprint(p+j, READSTR-j, "link: %d\n", nif->link);
 		j += snprint(p+j, READSTR-j, "out: %llud\n", nif->outpackets);
@@ -232,7 +232,7 @@
 		free(p);
 		return n;
 	case Naddrqid:
-		p = malloc(READSTR);
+		p = smalloc(READSTR);
 		j = 0;
 		for(i = 0; i < nif->alen; i++)
 			j += snprint(p+j, READSTR-j, "%2.2ux", nif->addr[i]);
--- a/sys/src/9/port/rebootcmd.c
+++ b/sys/src/9/port/rebootcmd.c
@@ -38,10 +38,7 @@
 	char *buf, *p, *ep;
 	int i;
 
-	buf = malloc(1024);
-	if(buf == nil)
-		error(Enomem);
-	p = buf;
+	p = buf = smalloc(1024);
 	ep = buf + 1024;
 	for(i=0; i<argc; i++)
 		p = seprint(p, ep, "%q ", argv[i]);
--- a/sys/src/9/port/segment.c
+++ b/sys/src/9/port/segment.c
@@ -45,13 +45,15 @@
 	Image *i, *ie;
 
 	imagealloc.free = xalloc(conf.nimage*sizeof(Image));
-	if (imagealloc.free == nil)
-		panic("initseg: no memory");
+	if(imagealloc.free == nil)
+		panic("initseg: no memory for Image");
 	ie = &imagealloc.free[conf.nimage-1];
 	for(i = imagealloc.free; i < ie; i++)
 		i->next = i+1;
 	i->next = 0;
 	imagealloc.freechan = malloc(NFREECHAN * sizeof(Chan*));
+	if(imagealloc.freechan == nil)
+		panic("initseg: no memory for Chan");
 	imagealloc.szfreechan = NFREECHAN;
 }