Re: Explanation on Uninitialized Variable bio in blk_rq_prep_clone

From: Keith Busch
Date: Fri Oct 04 2024 - 10:39:23 EST


On Fri, Oct 04, 2024 at 07:40:37PM +0530, SurajSonawane2415 wrote:
> In the function blk_rq_prep_clone, the variable bio is declared but can remain uninitialized
> if the allocation with bio_alloc_clone fails. This can lead to undefined behavior when the
> function attempts to free bio in the error handling section using bio_put(bio).
> By initializing bio to NULL at declaration, we ensure that the cleanup code will only
> interact with bio if it has been successfully allocated.

I don't think your explanation makes sense. The line where
bio_alloc_clone happens:

bio = bio_alloc_clone(rq->q->disk->part0, bio_src, gfp_mask, bs);

If it fails, then bio is initialized to NULL.