Whamcloud - gitweb
LU-13197 e2fsck: use proper types for variables 24/37424/7
authorAndreas Dilger <adilger@whamcloud.com>
Tue, 4 Feb 2020 06:24:50 +0000 (23:24 -0700)
committerAndreas Dilger <adilger@whamcloud.com>
Fri, 22 May 2020 06:29:53 +0000 (06:29 +0000)
Use ext2_ino_t instead of ino_t for referencing inode numbers.
Use loff_t for offsets instead of off_t to avoid 32-bit overflow.
Use unsigned int for flags so that they are not negative numbers.

Cast int products to ssize_t before multiplication to avoid overflow.

Signed-off-by: Andreas Dilger <adilger@dilger.ca>
Change-Id: Iaeaea8cb8b38902aefd1f4e9919d17fbe23ebbe5
Reviewed-on: https://review.whamcloud.com/37424
Tested-by: jenkins <devops@whamcloud.com>
Tested-by: Maloo <maloo@whamcloud.com>
Reviewed-by: Li Dongyang <dongyangli@ddn.com>
Reviewed-by: Wang Shilong <wshilong@ddn.com>
e2fsck/dx_dirinfo.c
e2fsck/e2fsck.h
e2fsck/pass1.c
e2fsck/rehash.c
e2fsck/super.c
lib/ext2fs/imager.c
misc/create_inode.c
misc/dumpe2fs.c
misc/e2fuzz.c
misc/e2image.c
misc/tune2fs.c

index 89672b7..f0f6084 100644 (file)
@@ -138,7 +138,7 @@ void e2fsck_free_dx_dir_info(e2fsck_t ctx)
 /*
  * Return the count of number of directories in the dx_dir_info structure
  */
-int e2fsck_get_num_dx_dirinfo(e2fsck_t ctx)
+ext2_ino_t e2fsck_get_num_dx_dirinfo(e2fsck_t ctx)
 {
        return ctx->dx_dir_info_count;
 }
@@ -146,10 +146,10 @@ int e2fsck_get_num_dx_dirinfo(e2fsck_t ctx)
 /*
  * A simple interator function
  */
-struct dx_dir_info *e2fsck_dx_dir_info_iter(e2fsck_t ctx, int *control)
+struct dx_dir_info *e2fsck_dx_dir_info_iter(e2fsck_t ctx, ext2_ino_t *control)
 {
        if (*control >= ctx->dx_dir_info_count)
                return 0;
 
-       return(ctx->dx_dir_info + (*control)++);
+       return ctx->dx_dir_info + (*control)++;
 }
index 58b8f9d..b4a5de9 100644 (file)
@@ -264,16 +264,16 @@ struct e2fsck_struct {
        char    *log_fn;
        FILE    *problem_logf;
        char    *problem_log_fn;
-       int     flags;          /* E2fsck internal flags */
-       int     options;
-       int     blocksize;      /* blocksize */
-       blk64_t use_superblock; /* sb requested by user */
-       blk64_t superblock;     /* sb used to open fs */
-       blk64_t num_blocks;     /* Total number of blocks */
-       blk64_t free_blocks;
-       ino_t   free_inodes;
-       int     mount_flags;
-       int     openfs_flags;
+       unsigned        flags;          /* E2fsck internal flags */
+       unsigned        options;
+       unsigned        blocksize;      /* blocksize */
+       blk64_t         use_superblock; /* sb requested by user */
+       blk64_t         superblock;     /* sb used to open fs */
+       blk64_t         num_blocks;     /* Total number of blocks */
+       blk64_t         free_blocks;
+       ext2_ino_t      free_inodes;
+       unsigned        mount_flags;
+       unsigned        openfs_flags;
        blkid_cache blkid;      /* blkid cache */
 
 #ifdef HAVE_SETJMP_H
@@ -521,8 +521,9 @@ extern void e2fsck_add_dx_dir(e2fsck_t ctx, ext2_ino_t ino,
                              struct ext2_inode *inode, int num_blocks);
 extern struct dx_dir_info *e2fsck_get_dx_dir_info(e2fsck_t ctx, ext2_ino_t ino);
 extern void e2fsck_free_dx_dir_info(e2fsck_t ctx);
-extern int e2fsck_get_num_dx_dirinfo(e2fsck_t ctx);
-extern struct dx_dir_info *e2fsck_dx_dir_info_iter(e2fsck_t ctx, int *control);
+extern ext2_ino_t e2fsck_get_num_dx_dirinfo(e2fsck_t ctx);
+extern struct dx_dir_info *e2fsck_dx_dir_info_iter(e2fsck_t ctx,
+                                                  ext2_ino_t *control);
 
 /* ea_refcount.c */
 typedef __u64 ea_key_t;
@@ -587,9 +588,8 @@ extern void e2fsck_clear_inode(e2fsck_t ctx, ext2_ino_t ino,
                               const char *source);
 #define e2fsck_mark_inode_bad(ctx, ino, count) \
                e2fsck_mark_inode_bad_loc(ctx, ino, count, __func__, __LINE__)
-extern void e2fsck_mark_inode_bad_loc(e2fsck_t ctx, ino_t ino, int count,
+extern void e2fsck_mark_inode_bad_loc(e2fsck_t ctx, ext2_ino_t ino, int count,
                                      const char *func, const int line);
-extern int is_inode_bad(e2fsck_t ctx, ino_t ino);
 extern void e2fsck_intercept_block_allocations(e2fsck_t ctx);
 
 /* pass2.c */
index 4d05f49..18a6898 100644 (file)
@@ -79,7 +79,7 @@ static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
 static void mark_table_blocks(e2fsck_t ctx);
 static void alloc_bb_map(e2fsck_t ctx);
 static void alloc_imagic_map(e2fsck_t ctx);
-static void add_encrypted_dir(e2fsck_t ctx, ino_t ino);
+static void add_encrypted_dir(e2fsck_t ctx, ext2_ino_t ino);
 static void handle_fs_bad_blocks(e2fsck_t ctx);
 static void process_inodes(e2fsck_t ctx, char *block_buf);
 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b);
@@ -2413,7 +2413,7 @@ static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b)
 /*
  * Mark an inode as being bad and increment its badness counter.
  */
-void e2fsck_mark_inode_bad_loc(e2fsck_t ctx, ino_t ino, int count,
+void e2fsck_mark_inode_bad_loc(e2fsck_t ctx, ext2_ino_t ino, int count,
                               const char *func, const int line)
 {
        struct          problem_context pctx;
@@ -2438,7 +2438,7 @@ void e2fsck_mark_inode_bad_loc(e2fsck_t ctx, ino_t ino, int count,
                        func, line, (unsigned long)ino, result, count + result);
 }
 
-static void add_encrypted_dir(e2fsck_t ctx, ino_t ino)
+static void add_encrypted_dir(e2fsck_t ctx, ext2_ino_t ino)
 {
        struct          problem_context pctx;
 
index 925dc51..3483656 100644 (file)
@@ -83,7 +83,7 @@ struct fill_dir_struct {
        int max_array, num_array;
        unsigned int dir_size;
        int compress;
-       ino_t parent;
+       ext2_ino_t parent;
        ext2_ino_t dir;
        struct ext2_dir_entry *dot_de;
        struct ext2_dir_entry *dotdot_de;
index 0a5f337..f2f79de 100644 (file)
@@ -595,7 +595,7 @@ void check_super_block(e2fsck_t ctx)
        blk64_t should_be;
        struct problem_context  pctx;
        blk64_t free_blocks = 0;
-       ino_t   free_inodes = 0;
+       ext2_ino_t free_inodes = 0;
        int     csum_flag, clear_test_fs_flag;
 
        inodes_per_block = EXT2_INODES_PER_BLOCK(fs->super);
index 7fd06f7..3dc6f08 100644 (file)
@@ -61,19 +61,20 @@ static int check_zero_block(char *buf, int blocksize)
 
 errcode_t ext2fs_image_inode_write(ext2_filsys fs, int fd, int flags)
 {
-       unsigned int    group, left, c, d;
+       dgrp_t          group;
+       ssize_t         left, c, d;
        char            *buf, *cp;
        blk64_t         blk;
        ssize_t         actual;
        errcode_t       retval;
-       off_t           r;
+       loff_t          r;
 
        buf = malloc(fs->blocksize * BUF_BLOCKS);
        if (!buf)
                return ENOMEM;
 
        for (group = 0; group < fs->group_desc_count; group++) {
-               blk = ext2fs_inode_table_loc(fs, (unsigned)group);
+               blk = ext2fs_inode_table_loc(fs, group);
                if (!blk) {
                        retval = EXT2_ET_MISSING_INODE_TABLE;
                        goto errout;
@@ -117,7 +118,7 @@ errcode_t ext2fs_image_inode_write(ext2_filsys fs, int fd, int flags)
                                        retval = errno;
                                        goto errout;
                                }
-                               if (actual != (ssize_t) (fs->blocksize * d)) {
+                               if (actual != fs->blocksize * d) {
                                        retval = EXT2_ET_SHORT_WRITE;
                                        goto errout;
                                }
@@ -141,7 +142,8 @@ errout:
 errcode_t ext2fs_image_inode_read(ext2_filsys fs, int fd,
                                  int flags EXT2FS_ATTR((unused)))
 {
-       unsigned int    group, c, left;
+       dgrp_t          group;
+       ssize_t         c, left;
        char            *buf;
        blk64_t         blk;
        ssize_t         actual;
@@ -152,7 +154,7 @@ errcode_t ext2fs_image_inode_read(ext2_filsys fs, int fd,
                return ENOMEM;
 
        for (group = 0; group < fs->group_desc_count; group++) {
-               blk = ext2fs_inode_table_loc(fs, (unsigned)group);
+               blk = ext2fs_inode_table_loc(fs, group);
                if (!blk) {
                        retval = EXT2_ET_MISSING_INODE_TABLE;
                        goto errout;
@@ -167,7 +169,7 @@ errcode_t ext2fs_image_inode_read(ext2_filsys fs, int fd,
                                retval = errno;
                                goto errout;
                        }
-                       if (actual != (ssize_t) (fs->blocksize * c)) {
+                       if (actual != fs->blocksize * c) {
                                retval = EXT2_ET_SHORT_READ;
                                goto errout;
                        }
@@ -249,7 +251,7 @@ errcode_t ext2fs_image_super_write(ext2_filsys fs, int fd,
        }
 #endif
 
-       actual = write(fd, cp, fs->blocksize * fs->desc_blocks);
+       actual = write(fd, cp, (ssize_t)fs->blocksize * fs->desc_blocks);
 
 
 #ifdef WORDS_BIGENDIAN
@@ -265,7 +267,7 @@ errcode_t ext2fs_image_super_write(ext2_filsys fs, int fd,
                retval = errno;
                goto errout;
        }
-       if (actual != (ssize_t) (fs->blocksize * fs->desc_blocks)) {
+       if (actual != (ssize_t)fs->blocksize * fs->desc_blocks) {
                retval = EXT2_ET_SHORT_WRITE;
                goto errout;
        }
@@ -287,7 +289,7 @@ errcode_t ext2fs_image_super_read(ext2_filsys fs, int fd,
        ssize_t         actual, size;
        errcode_t       retval;
 
-       size = fs->blocksize * (fs->group_desc_count + 1);
+       size = (ssize_t)fs->blocksize * (fs->group_desc_count + 1);
        buf = malloc(size);
        if (!buf)
                return ENOMEM;
@@ -311,7 +313,7 @@ errcode_t ext2fs_image_super_read(ext2_filsys fs, int fd,
        memcpy(fs->super, buf, SUPERBLOCK_SIZE);
 
        memcpy(fs->group_desc, buf + fs->blocksize,
-              fs->blocksize * fs->group_desc_count);
+              (ssize_t)fs->blocksize * fs->group_desc_count);
 
        retval = 0;
 
index 0091b72..6387425 100644 (file)
@@ -599,7 +599,7 @@ out:
        return err;
 }
 
-static int is_hardlink(struct hdlinks_s *hdlinks, dev_t dev, ino_t ino)
+static int is_hardlink(struct hdlinks_s *hdlinks, dev_t dev, ext2_ino_t ino)
 {
        int i;
 
index 384ce92..b812c93 100644 (file)
@@ -362,7 +362,7 @@ static void print_inline_journal_information(ext2_filsys fs)
        struct ext2_inode       inode;
        ext2_file_t             journal_file;
        errcode_t               retval;
-       ino_t                   ino = fs->super->s_journal_inum;
+       ext2_ino_t              ino = fs->super->s_journal_inum;
        char                    buf[1024];
 
        if (fs->flags & EXT2_FLAG_IMAGE_FILE)
index 8576d4e..7c0f776 100644 (file)
@@ -181,9 +181,9 @@ static int process_fs(const char *fsname)
        int flags, fd;
        ext2_filsys fs = NULL;
        ext2fs_block_bitmap corrupt_map;
-       off_t hsize, count, off, offset, corrupt_bytes;
+       loff_t hsize, count, off, offset, corrupt_bytes;
        unsigned char c;
-       off_t i;
+       loff_t i;
 
        /* If mounted rw, force dryrun mode */
        ret = ext2fs_check_if_mounted(fsname, &flags);
@@ -277,8 +277,8 @@ static int process_fs(const char *fsname)
                        c |= 0x80;
                if (verbose)
                        printf("Corrupting byte %lld in block %lld to 0x%x\n",
-                              (long long) off % fs->blocksize,
-                              (long long) off / fs->blocksize, c);
+                              off % fs->blocksize,
+                              off / fs->blocksize, c);
                if (dryrun)
                        continue;
 #ifdef HAVE_PWRITE64
index 3c881fe..cdbb28c 100644 (file)
@@ -312,7 +312,7 @@ struct process_block_struct {
  * structure, so there's no point in letting the ext2fs library read
  * the inode again.
  */
-static ino_t stashed_ino = 0;
+static ext2_ino_t stashed_ino = 0;
 static struct ext2_inode *stashed_inode;
 
 static errcode_t meta_get_blocks(ext2_filsys fs EXT2FS_ATTR((unused)),
index 578c4a8..8a977b7 100644 (file)
@@ -367,7 +367,7 @@ static errcode_t remove_journal_inode(ext2_filsys fs)
 {
        struct ext2_inode       inode;
        errcode_t               retval;
-       ino_t                   ino = fs->super->s_journal_inum;
+       ext2_ino_t              ino = fs->super->s_journal_inum;
 
        retval = ext2fs_read_inode(fs, ino,  &inode);
        if (retval) {