kernel: fix more malloc/smalloc errors

This commit is contained in:
cinap_lenrek 2011-12-12 19:17:58 +01:00
parent 304ee3b2b5
commit 8cb8043d0e
13 changed files with 34 additions and 26 deletions

View file

@ -248,7 +248,7 @@ srballoc(ulong sz)
{ {
Srb *srb; Srb *srb;
srb = malloc(sizeof *srb+sz); srb = smalloc(sizeof *srb+sz);
srb->state = Alloc; srb->state = Alloc;
srb->dp = srb->data = srb+1; srb->dp = srb->data = srb+1;
srb->ticksent = Ticks; srb->ticksent = Ticks;
@ -260,7 +260,7 @@ srbkalloc(void *db, ulong)
{ {
Srb *srb; Srb *srb;
srb = malloc(sizeof *srb); srb = smalloc(sizeof *srb);
srb->state = Alloc; srb->state = Alloc;
srb->dp = srb->data = db; srb->dp = srb->data = db;
srb->ticksent = Ticks; srb->ticksent = Ticks;
@ -1454,7 +1454,7 @@ configwrite(Aoedev *d, void *db, long len)
if(len > sizeof d->config) if(len > sizeof d->config)
error(Etoobig); error(Etoobig);
srb = srballoc(len); srb = srballoc(len);
s = malloc(len); s = smalloc(len);
memmove(s, db, len); memmove(s, db, len);
if(waserror()){ if(waserror()){
srbfree(srb); srbfree(srb);

View file

@ -78,6 +78,10 @@ audioreset(void)
probe = &audioprobes[i]; probe = &audioprobes[i];
for(;;){ for(;;){
if(*pp == nil){
print("audio: no memory\n");
break;
}
memset(*pp, 0, sizeof(Audio)); memset(*pp, 0, sizeof(Audio));
(*pp)->ctlrno = ctlrno; (*pp)->ctlrno = ctlrno;
(*pp)->name = probe->name; (*pp)->name = probe->name;

View file

@ -780,9 +780,11 @@ cachedump(Bridge *b)
Centry *ce; Centry *ce;
char c; char c;
buf = smalloc(n);
qlock(b); qlock(b);
if(waserror()) { if(waserror()) {
qunlock(b); qunlock(b);
free(buf);
nexterror(); nexterror();
} }
sec = TK2SEC(m->ticks); sec = TK2SEC(m->ticks);
@ -793,7 +795,6 @@ cachedump(Bridge *b)
n *= 51; // change if print format is changed n *= 51; // change if print format is changed
n += 10; // some slop at the end n += 10; // some slop at the end
buf = malloc(n);
p = buf; p = buf;
ep = buf + n; ep = buf + n;
ce = b->cache; ce = b->cache;
@ -808,7 +809,6 @@ cachedump(Bridge *b)
*p = 0; *p = 0;
poperror(); poperror();
qunlock(b); qunlock(b);
return buf; return buf;
} }

View file

@ -629,9 +629,7 @@ consread(Chan *c, void *buf, long n, vlong off)
return randomread(buf, n); return randomread(buf, n);
case Qdrivers: case Qdrivers:
b = malloc(READSTR); b = smalloc(READSTR);
if(b == nil)
error(Enomem);
k = 0; k = 0;
for(i = 0; devtab[i] != nil; i++) for(i = 0; devtab[i] != nil; i++)
k += snprint(b+k, READSTR-k, "#%C %s\n", k += snprint(b+k, READSTR-k, "#%C %s\n",
@ -641,8 +639,8 @@ consread(Chan *c, void *buf, long n, vlong off)
nexterror(); nexterror();
} }
n = readstr((ulong)offset, buf, n, b); n = readstr((ulong)offset, buf, n, b);
free(b);
poperror(); poperror();
free(b);
return n; return n;
case Qzero: case Qzero:

View file

@ -240,7 +240,7 @@ flashread(Chan *c, void *buf, long n, vlong offset)
error(Eio); error(Eio);
return n; return n;
case Qctl: case Qctl:
s = malloc(READSTR); s = smalloc(READSTR);
if(waserror()){ if(waserror()){
free(s); free(s);
nexterror(); nexterror();
@ -475,7 +475,11 @@ addflashcard(char *name, int (*reset)(Flash*))
{ {
Flashtype *f, **l; 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->name = name;
f->reset = reset; f->reset = reset;
f->next = nil; f->next = nil;

View file

@ -193,7 +193,9 @@ findcsn(int csn, int create, int dolock)
l = &c->next; l = &c->next;
} }
if(create) { 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->next = c;
nc->csn = csn; nc->csn = csn;
c = nc; c = nc;

View file

@ -430,7 +430,7 @@ procopen(Chan *c, int omode)
case Qns: case Qns:
if(omode != OREAD) if(omode != OREAD)
error(Eperm); error(Eperm);
c->aux = malloc(sizeof(Mntwalk)); c->aux = smalloc(sizeof(Mntwalk));
break; break;
case Qnotepg: case Qnotepg:

View file

@ -1226,8 +1226,7 @@ sdread(Chan *c, void *a, long n, vlong off)
error(Eperm); error(Eperm);
case Qtopctl: case Qtopctl:
m = 64*1024; /* room for register dumps */ m = 64*1024; /* room for register dumps */
p = buf = malloc(m); p = buf = smalloc(m);
assert(p);
e = p + m; e = p + m;
qlock(&devslock); qlock(&devslock);
for(i = 0; i < nelem(devs); i++){ for(i = 0; i < nelem(devs); i++){
@ -1253,7 +1252,7 @@ sdread(Chan *c, void *a, long n, vlong off)
unit = sdev->unit[UNIT(c->qid)]; unit = sdev->unit[UNIT(c->qid)];
m = 16*1024; /* room for register dumps */ m = 16*1024; /* room for register dumps */
p = malloc(m); p = smalloc(m);
l = snprint(p, m, "inquiry %.48s\n", l = snprint(p, m, "inquiry %.48s\n",
(char*)unit->inquiry+8); (char*)unit->inquiry+8);
qlock(&unit->ctl); qlock(&unit->ctl);
@ -1790,6 +1789,8 @@ getnewport(DevConf* dc)
Devport *p; Devport *p;
p = malloc((dc->nports + 1) * sizeof(Devport)); p = malloc((dc->nports + 1) * sizeof(Devport));
if(p == nil)
panic("sd: no memory for Devport");
if(dc->nports > 0){ if(dc->nports > 0){
memmove(p, dc->ports, dc->nports * sizeof(Devport)); memmove(p, dc->ports, dc->nports * sizeof(Devport));
free(dc->ports); free(dc->ports);

View file

@ -76,7 +76,7 @@ wdread(Chan* c, void* a, long n, vlong off)
if(wd == nil || wd->stat == nil) if(wd == nil || wd->stat == nil)
return 0; return 0;
p = malloc(READSTR); p = smalloc(READSTR);
if(waserror()){ if(waserror()){
free(p); free(p);
nexterror(); nexterror();

View file

@ -21,7 +21,7 @@ logopen(Log *alog)
if(alog->minread == 0) if(alog->minread == 0)
alog->minread = 1; alog->minread = 1;
if(alog->buf == nil) if(alog->buf == nil)
alog->buf = malloc(alog->nlog); alog->buf = smalloc(alog->nlog);
alog->rptr = alog->buf; alog->rptr = alog->buf;
alog->end = alog->buf + alog->nlog; alog->end = alog->buf + alog->nlog;
alog->len = 0; alog->len = 0;

View file

@ -212,7 +212,7 @@ netifread(Netif *nif, Chan *c, void *a, long n, ulong offset)
case Nctlqid: case Nctlqid:
return readnum(offset, a, n, NETID(c->qid.path), NUMSIZE); return readnum(offset, a, n, NETID(c->qid.path), NUMSIZE);
case Nstatqid: case Nstatqid:
p = malloc(READSTR); p = smalloc(READSTR);
j = snprint(p, READSTR, "in: %llud\n", nif->inpackets); 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, "link: %d\n", nif->link);
j += snprint(p+j, READSTR-j, "out: %llud\n", nif->outpackets); j += snprint(p+j, READSTR-j, "out: %llud\n", nif->outpackets);
@ -232,7 +232,7 @@ netifread(Netif *nif, Chan *c, void *a, long n, ulong offset)
free(p); free(p);
return n; return n;
case Naddrqid: case Naddrqid:
p = malloc(READSTR); p = smalloc(READSTR);
j = 0; j = 0;
for(i = 0; i < nif->alen; i++) for(i = 0; i < nif->alen; i++)
j += snprint(p+j, READSTR-j, "%2.2ux", nif->addr[i]); j += snprint(p+j, READSTR-j, "%2.2ux", nif->addr[i]);

View file

@ -38,10 +38,7 @@ setbootcmd(int argc, char *argv[])
char *buf, *p, *ep; char *buf, *p, *ep;
int i; int i;
buf = malloc(1024); p = buf = smalloc(1024);
if(buf == nil)
error(Enomem);
p = buf;
ep = buf + 1024; ep = buf + 1024;
for(i=0; i<argc; i++) for(i=0; i<argc; i++)
p = seprint(p, ep, "%q ", argv[i]); p = seprint(p, ep, "%q ", argv[i]);

View file

@ -45,13 +45,15 @@ initseg(void)
Image *i, *ie; Image *i, *ie;
imagealloc.free = xalloc(conf.nimage*sizeof(Image)); imagealloc.free = xalloc(conf.nimage*sizeof(Image));
if (imagealloc.free == nil) if(imagealloc.free == nil)
panic("initseg: no memory"); panic("initseg: no memory for Image");
ie = &imagealloc.free[conf.nimage-1]; ie = &imagealloc.free[conf.nimage-1];
for(i = imagealloc.free; i < ie; i++) for(i = imagealloc.free; i < ie; i++)
i->next = i+1; i->next = i+1;
i->next = 0; i->next = 0;
imagealloc.freechan = malloc(NFREECHAN * sizeof(Chan*)); imagealloc.freechan = malloc(NFREECHAN * sizeof(Chan*));
if(imagealloc.freechan == nil)
panic("initseg: no memory for Chan");
imagealloc.szfreechan = NFREECHAN; imagealloc.szfreechan = NFREECHAN;
} }