Whamcloud - gitweb
[VALGRIND] Fix memory leak in libblkid (blkid_get_devname)
[tools/e2fsprogs.git] / resize / resize2fs.c
index f6c3ede..4b1ca22 100644 (file)
@@ -186,6 +186,7 @@ errcode_t adjust_fs_info(ext2_filsys fs, ext2_filsys old_fs, blk_t new_size)
        unsigned long   i, j, old_desc_blocks, max_group;
        unsigned int    meta_bg, meta_bg_size;
        int             has_super;
+       __u64           new_inodes;     /* u64 to check for overflow */
 
        fs->super->s_blocks_count = new_size;
 
@@ -226,6 +227,12 @@ retry:
        /*
         * Adjust the number of inodes
         */
+       new_inodes =(__u64)fs->super->s_inodes_per_group * fs->group_desc_count;
+       if (new_inodes > ~0U) {
+               fprintf(stderr, _("inodes (%llu) must be less than %u"),
+                                  new_inodes, ~0U);
+               return EXT2_ET_TOO_MANY_INODES;
+       }
        fs->super->s_inodes_count = fs->super->s_inodes_per_group *
                fs->group_desc_count;
 
@@ -243,10 +250,10 @@ retry:
        /*
         * Adjust the number of reserved blocks
         */
-       blk = old_fs->super->s_r_blocks_count * 100 /
+       blk = (__u64)old_fs->super->s_r_blocks_count * 100 /
                old_fs->super->s_blocks_count;
-       fs->super->s_r_blocks_count = ((fs->super->s_blocks_count * blk)
-                                      / 100);
+       fs->super->s_r_blocks_count = e2p_percent(blk, 
+                                                 fs->super->s_blocks_count);
 
        /*
         * Adjust the bitmaps for size
@@ -536,14 +543,13 @@ errout:
 static errcode_t mark_table_blocks(ext2_filsys fs,
                                   ext2fs_block_bitmap bmap)
 {
-       blk_t                   block, b;
+       blk_t                   b;
        unsigned int            j;
        dgrp_t                  i;
        unsigned long           meta_bg_size;
        unsigned int            old_desc_blocks;
 
        meta_bg_size = (fs->blocksize / sizeof (struct ext2_group_desc));
-       block = fs->super->s_first_data_block;
        if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
                old_desc_blocks = fs->super->s_first_meta_bg;
        else
@@ -571,7 +577,6 @@ static errcode_t mark_table_blocks(ext2_filsys fs,
                 */
                ext2fs_mark_block_bitmap(bmap,
                                         fs->group_desc[i].bg_inode_bitmap);
-               block += fs->super->s_blocks_per_group;
        }
        return 0;
 }
@@ -1301,7 +1306,9 @@ static int check_and_change_inodes(ext2_ino_t dir,
        retval = ext2fs_read_inode(is->rfs->old_fs, dir, &inode);
        if (retval == 0) {
                inode.i_mtime = inode.i_ctime = time(0);
-               ext2fs_write_inode(is->rfs->old_fs, dir, &inode);
+               is->err = ext2fs_write_inode(is->rfs->old_fs, dir, &inode);
+               if (is->err)
+                       return DIRENT_ABORT;
        }
 
        return DIRENT_CHANGED;
@@ -1582,7 +1589,9 @@ static errcode_t ext2fs_calculate_summary_stats(ext2_filsys fs)
        total_free = 0;
        count = 0;
        group = 0;
-       for (ino = 1; ino <= fs->super->s_inodes_count; ino++) {
+
+       /* Protect loop from wrap-around if s_inodes_count maxed */
+       for (ino = 1; ino <= fs->super->s_inodes_count && ino > 0; ino++) {
                if (!ext2fs_fast_test_inode_bitmap(fs->inode_map, ino)) {
                        group_free++;
                        total_free++;