devsd: check return value of ifc->enable(), don't leak unit name/user strings

This commit is contained in:
cinap_lenrek 2017-03-26 16:45:34 +02:00
parent 1889aa50c3
commit 019bb580da

View file

@ -293,12 +293,12 @@ sdgetunit(SDev* sdev, int subno)
qunlock(&sdev->unitlock); qunlock(&sdev->unitlock);
return nil; return nil;
} }
if((unit = malloc(sizeof(SDunit))) == nil){ if((unit = malloc(sizeof(SDunit))) == nil){
qunlock(&sdev->unitlock); qunlock(&sdev->unitlock);
return nil; return nil;
} }
sdev->unitflg[subno] = 1; sdev->unitflg[subno] = 1;
snprint(buf, sizeof buf, "%s%x", sdev->name, subno); snprint(buf, sizeof buf, "%s%x", sdev->name, subno);
kstrdup(&unit->name, buf); kstrdup(&unit->name, buf);
kstrdup(&unit->user, eve); kstrdup(&unit->user, eve);
@ -307,16 +307,17 @@ sdgetunit(SDev* sdev, int subno)
unit->dev = sdev; unit->dev = sdev;
if(sdev->enabled == 0 && sdev->ifc->enable) if(sdev->enabled == 0 && sdev->ifc->enable)
sdev->ifc->enable(sdev); sdev->enabled = sdev->ifc->enable(sdev);
sdev->enabled = 1;
/* /*
* No need to lock anything here as this is only * No need to lock anything here as this is only
* called before the unit is made available in the * called before the unit is made available in the
* sdunit[] array. * sdunit[] array.
*/ */
if(unit->dev->ifc->verify(unit) == 0){ if(sdev->enabled == 0 || unit->dev->ifc->verify(unit) == 0){
qunlock(&sdev->unitlock); qunlock(&sdev->unitlock);
free(unit->name);
free(unit->user);
free(unit); free(unit);
return nil; return nil;
} }