Dan Carpenter
2012-Sep-05 12:32 UTC
[patch] virtio-blk: fix NULL checking in virtblk_alloc_req()
Smatch complains about the inconsistent NULL checking here. Fix it to return NULL on failure. Signed-off-by: Dan Carpenter <dan.carpenter at oracle.com> --- This is only needed in linux-next. diff --git a/drivers/block/virtio_blk.c b/drivers/block/virtio_blk.c index 2edfb5c..457db0c 100644 --- a/drivers/block/virtio_blk.c +++ b/drivers/block/virtio_blk.c @@ -90,10 +90,11 @@ static inline struct virtblk_req *virtblk_alloc_req(struct virtio_blk *vblk, struct virtblk_req *vbr; vbr = mempool_alloc(vblk->pool, gfp_mask); - if (vbr && use_bio) - sg_init_table(vbr->sg, vblk->sg_elems); + if (!vbr) + return NULL; - vbr->vblk = vblk; + if (use_bio) + sg_init_table(vbr->sg, vblk->sg_elems); return vbr; }
Michael S. Tsirkin
2012-Sep-05 13:11 UTC
[patch] virtio-blk: fix NULL checking in virtblk_alloc_req()
On Wed, Sep 05, 2012 at 03:32:53PM +0300, Dan Carpenter wrote:> Smatch complains about the inconsistent NULL checking here. Fix it to > return NULL on failure. > > Signed-off-by: Dan Carpenter <dan.carpenter at oracle.com>ACK> --- > This is only needed in linux-next.Yes upstream is OK. linux-next picks up stuff from rusty's patch queue so presumably this can be folded into patch that triggered this.> > diff --git a/drivers/block/virtio_blk.c b/drivers/block/virtio_blk.c > index 2edfb5c..457db0c 100644 > --- a/drivers/block/virtio_blk.c > +++ b/drivers/block/virtio_blk.c > @@ -90,10 +90,11 @@ static inline struct virtblk_req *virtblk_alloc_req(struct virtio_blk *vblk, > struct virtblk_req *vbr; > > vbr = mempool_alloc(vblk->pool, gfp_mask); > - if (vbr && use_bio) > - sg_init_table(vbr->sg, vblk->sg_elems); > + if (!vbr) > + return NULL; > > - vbr->vblk = vblk;Smatch is right to complain: on memory allocation failure this will dereference NULL.> + if (use_bio) > + sg_init_table(vbr->sg, vblk->sg_elems); > > return vbr; > }
Rusty Russell
2012-Sep-06 02:55 UTC
[patch] virtio-blk: fix NULL checking in virtblk_alloc_req()
Dan Carpenter <dan.carpenter at oracle.com> writes:> Smatch complains about the inconsistent NULL checking here. Fix it to > return NULL on failure. > > Signed-off-by: Dan Carpenter <dan.carpenter at oracle.com> > --- > This is only needed in linux-next.Nice!> diff --git a/drivers/block/virtio_blk.c b/drivers/block/virtio_blk.c > index 2edfb5c..457db0c 100644 > --- a/drivers/block/virtio_blk.c > +++ b/drivers/block/virtio_blk.c > @@ -90,10 +90,11 @@ static inline struct virtblk_req *virtblk_alloc_req(struct virtio_blk *vblk, > struct virtblk_req *vbr; > > vbr = mempool_alloc(vblk->pool, gfp_mask); > - if (vbr && use_bio) > - sg_init_table(vbr->sg, vblk->sg_elems); > + if (!vbr) > + return NULL; > > - vbr->vblk = vblk; > + if (use_bio) > + sg_init_table(vbr->sg, vblk->sg_elems); > > return vbr; > }But it turns out that "vbr->vblk = vblk;" assignment is important :) Fixed and applied, Rusty.
Asias He
2012-Sep-06 03:02 UTC
[patch] virtio-blk: fix NULL checking in virtblk_alloc_req()
Hello Dan, On 09/05/2012 08:32 PM, Dan Carpenter wrote:> Smatch complains about the inconsistent NULL checking here. Fix it to > return NULL on failure. > > Signed-off-by: Dan Carpenter <dan.carpenter at oracle.com>Thanks for catching this.> --- > This is only needed in linux-next. > > diff --git a/drivers/block/virtio_blk.c b/drivers/block/virtio_blk.c > index 2edfb5c..457db0c 100644 > --- a/drivers/block/virtio_blk.c > +++ b/drivers/block/virtio_blk.c > @@ -90,10 +90,11 @@ static inline struct virtblk_req *virtblk_alloc_req(struct virtio_blk *vblk, > struct virtblk_req *vbr; > > vbr = mempool_alloc(vblk->pool, gfp_mask); > - if (vbr && use_bio) > - sg_init_table(vbr->sg, vblk->sg_elems); > + if (!vbr) > + return NULL; > > - vbr->vblk = vblk;The assignment of vbr->vblk is needed.> + if (use_bio) > + sg_init_table(vbr->sg, vblk->sg_elems); > > return vbr; > }-- Asias
Asias He
2012-Sep-06 03:04 UTC
[patch] virtio-blk: fix NULL checking in virtblk_alloc_req()
On 09/06/2012 10:55 AM, Rusty Russell wrote:> Dan Carpenter <dan.carpenter at oracle.com> writes: > >> Smatch complains about the inconsistent NULL checking here. Fix it to >> return NULL on failure. >> >> Signed-off-by: Dan Carpenter <dan.carpenter at oracle.com> >> --- >> This is only needed in linux-next. > > Nice! > >> diff --git a/drivers/block/virtio_blk.c b/drivers/block/virtio_blk.c >> index 2edfb5c..457db0c 100644 >> --- a/drivers/block/virtio_blk.c >> +++ b/drivers/block/virtio_blk.c >> @@ -90,10 +90,11 @@ static inline struct virtblk_req *virtblk_alloc_req(struct virtio_blk *vblk, >> struct virtblk_req *vbr; >> >> vbr = mempool_alloc(vblk->pool, gfp_mask); >> - if (vbr && use_bio) >> - sg_init_table(vbr->sg, vblk->sg_elems); >> + if (!vbr) >> + return NULL; >> >> - vbr->vblk = vblk; >> + if (use_bio) >> + sg_init_table(vbr->sg, vblk->sg_elems); >> >> return vbr; >> } > > But it turns out that "vbr->vblk = vblk;" assignment is important :)I was just replying ;-)> Fixed and applied,Thanks Rusty! -- Asias