OSDN Git Service

Avoid underflow on an unsigned int when computering bg overrun
authorKen Sumrall <ksumrall@android.com>
Fri, 16 Aug 2013 02:06:29 +0000 (19:06 -0700)
committerKen Sumrall <ksumrall@android.com>
Fri, 16 Aug 2013 02:06:29 +0000 (19:06 -0700)
A computation for when a block group overruns the end of an image
was consistently storing a negative number in an unsigned var,
and then checking for >0, which is always true for negative numbers.
So first check if the number will be positive before computing it.

Change-Id: Ic8cff8f9ab9f4ea8c5a4dc42143c2430fa87ba12

ext4_utils/allocate.c

index 5c60e92..c0b2c7e 100644 (file)
@@ -312,9 +312,10 @@ static void init_bg(struct block_group_info *bg, unsigned int i)
        if (reserve_blocks(bg, bg->first_free_block, bg->header_blocks) < 0)
                error("failed to reserve %u blocks in block group %u\n", bg->header_blocks, i);
 
-       u32 overrun = bg->first_block + info.blocks_per_group - aux_info.len_blocks;
-       if (overrun > 0)
+       if (bg->first_block + info.blocks_per_group > aux_info.len_blocks) {
+               u32 overrun = bg->first_block + info.blocks_per_group - aux_info.len_blocks;
                reserve_blocks(bg, info.blocks_per_group - overrun, overrun);
+       }
 }
 
 void block_allocator_init()