]> git.ipfire.org Git - thirdparty/e2fsprogs.git/commitdiff
mmp: abstract out repeated 'sizeof(buf), buf' usage
authorAndreas Dilger <adilger@dilger.ca>
Tue, 14 Jan 2020 21:42:18 +0000 (14:42 -0700)
committerTheodore Ts'o <tytso@mit.edu>
Sat, 25 Jan 2020 04:06:58 +0000 (23:06 -0500)
The printf("%.*s") format requires both the buffer size and buffer
pointer to be specified for each use.  Since this is repeatedly given
as "(int)sizeof(buf), (char *)buf" for mmp_nodename and mmp_bdevname
fields, with typecasts to avoid compiler warnings.

Add a helper macro EXT2_LEN_STR() to avoid repeated boilerplate code.

This can also be used for other superblock buffer fields that may not
have NUL-terminated strings (e.g. s_volume_name, s_last_mounted,
s_{first,last}_error_func, s_mount_opts) to simplify code and avoid
the need for temporary buffers for NUL-termination.

Annotate the superblock string fields that may not be NUL-terminated.

Signed-off-by: Andreas Dilger <adilger@dilger.ca>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
13 files changed:
debugfs/debugfs.c
e2fsck/unix.c
e2fsck/util.c
lib/blkid/probe.c
lib/e2p/ls.c
lib/ext2fs/ext2_fs.h
lib/support/plausible.c
misc/dumpe2fs.c
misc/e2label.c
misc/findsuper.c
misc/mke2fs.c
misc/tune2fs.c
misc/util.c

index 23bc3c3c9768b806da663201cfde7dbc0606070b..755ec808511bc2bcb6335838bd886545f3a9b364 100644 (file)
@@ -2447,9 +2447,9 @@ void do_dump_mmp(int argc EXT2FS_ATTR((unused)), char *argv[],
        fprintf(stdout, "sequence: %08x\n", mmp_s->mmp_seq);
        fprintf(stdout, "time: %lld -- %s", mmp_s->mmp_time, ctime(&t));
        fprintf(stdout, "node_name: %.*s\n",
-               (int)sizeof(mmp_s->mmp_nodename), (char *)mmp_s->mmp_nodename);
+               EXT2_LEN_STR(mmp_s->mmp_nodename));
        fprintf(stdout, "device_name: %.*s\n",
-               (int)sizeof(mmp_s->mmp_bdevname), (char *)mmp_s->mmp_bdevname);
+               EXT2_LEN_STR(mmp_s->mmp_bdevname));
        fprintf(stdout, "magic: 0x%x\n", mmp_s->mmp_magic);
        fprintf(stdout, "checksum: 0x%08x\n", mmp_s->mmp_checksum);
 }
index 68f4987398624d171e93a82de0797dc9457f96fc..e71d7833ae5900e2d0e971b6bb4d6df529dc0d0a 100644 (file)
@@ -1692,11 +1692,10 @@ failure:
         * Set the device name, which is used whenever we print error
         * or informational messages to the user.
         */
-       if (ctx->device_name == 0 &&
-           (sb->s_volume_name[0] != 0)) {
+       if (ctx->device_name == 0 && sb->s_volume_name[0])
                ctx->device_name = string_copy(ctx, sb->s_volume_name,
                                               sizeof(sb->s_volume_name));
-       }
+
        if (ctx->device_name == 0)
                ctx->device_name = string_copy(ctx, ctx->filesystem_name, 0);
        for (cp = ctx->device_name; *cp; cp++)
@@ -1704,19 +1703,19 @@ failure:
                        *cp = '_';
 
        if (ctx->problem_logf) {
-               char buf[48];
 
                fprintf(ctx->problem_logf, "<filesystem dev=\"%s\"",
                        ctx->filesystem_name);
                if (!uuid_is_null(sb->s_uuid)) {
+                       char buf[48];
+
                        uuid_unparse(sb->s_uuid, buf);
                        fprintf(ctx->problem_logf, " uuid=\"%s\"", buf);
                }
-               if (sb->s_volume_name[0]) {
-                       memset(buf, 0, sizeof(buf));
-                       strncpy(buf, sb->s_volume_name, sizeof(buf));
-                       fprintf(ctx->problem_logf, " label=\"%s\"", buf);
-               }
+               if (sb->s_volume_name[0])
+                       fprintf(ctx->problem_logf, " label=\"%.*s\"",
+                               EXT2_LEN_STR(sb->s_volume_name));
+
                fputs("/>\n", ctx->problem_logf);
        }
 
index 07885ab09a0a0e34af232e62522bc207897494fc..314c9894724fad841bb197c63dfea605431ad47f 100644 (file)
@@ -778,11 +778,9 @@ void dump_mmp_msg(struct mmp_struct *mmp, const char *fmt, ...)
                printf("    mmp_update_date: %s", ctime(&t));
                printf("    mmp_update_time: %lld\n", mmp->mmp_time);
                printf("    mmp_node_name: %.*s\n",
-                      (int)sizeof(mmp->mmp_nodename),
-                      (char *)mmp->mmp_nodename);
+                      EXT2_LEN_STR(mmp->mmp_nodename));
                printf("    mmp_device_name: %.*s\n",
-                      (int)sizeof(mmp->mmp_bdevname),
-                      (char *)mmp->mmp_bdevname);
+                      EXT2_LEN_STR(mmp->mmp_bdevname));
        }
 }
 
index d72014489e2cbd8b7409acfd158d27641549c39c..f8687cdeb6cd8d435f0f69d1f592a1d4ded60956 100644 (file)
@@ -144,7 +144,7 @@ static void get_ext2_info(blkid_dev dev, struct blkid_magic *id,
                   blkid_le32(es->s_feature_incompat),
                   blkid_le32(es->s_feature_ro_compat)));
 
-       if (strlen(es->s_volume_name))
+       if (es->s_volume_name[0])
                label = es->s_volume_name;
        blkid_set_tag(dev, "LABEL", label, sizeof(es->s_volume_name));
 
index 5a4461781e6b382072438adbceb3b876dccfb9b8..1521099953da98fcaad08ca3b3fe3d85e9378817 100644 (file)
@@ -224,7 +224,7 @@ static const char *quota_type2prefix(enum quota_type qtype)
 void list_super2(struct ext2_super_block * sb, FILE *f)
 {
        int inode_blocks_per_group;
-       char buf[80], *str;
+       char *str;
        time_t  tm;
        enum quota_type qtype;
 
@@ -232,18 +232,16 @@ void list_super2(struct ext2_super_block * sb, FILE *f)
                                    EXT2_INODE_SIZE(sb)) +
                                   EXT2_BLOCK_SIZE(sb) - 1) /
                                  EXT2_BLOCK_SIZE(sb));
-       if (sb->s_volume_name[0]) {
-               memset(buf, 0, sizeof(buf));
-               strncpy(buf, sb->s_volume_name, sizeof(sb->s_volume_name));
-       } else
-               strcpy(buf, "<none>");
-       fprintf(f, "Filesystem volume name:   %s\n", buf);
-       if (sb->s_last_mounted[0]) {
-               memset(buf, 0, sizeof(buf));
-               strncpy(buf, sb->s_last_mounted, sizeof(sb->s_last_mounted));
-       } else
-               strcpy(buf, "<not available>");
-       fprintf(f, "Last mounted on:          %s\n", buf);
+       if (sb->s_volume_name[0])
+               fprintf(f, "Filesystem volume name:   %.*s\n",
+                       EXT2_LEN_STR(sb->s_volume_name));
+       else
+               fprintf(f, "Filesystem volume name:   <none>\n");
+       if (sb->s_last_mounted[0])
+               fprintf(f, "Last mounted on:          %.*s\n",
+                       EXT2_LEN_STR(sb->s_last_mounted));
+       else
+               fprintf(f, "Last mounted on:          <not available>\n");
        fprintf(f, "Filesystem UUID:          %s\n", e2p_uuid2str(sb->s_uuid));
        fprintf(f, "Filesystem magic number:  0x%04X\n", sb->s_magic);
        fprintf(f, "Filesystem revision #:    %d", sb->s_rev_level);
@@ -259,7 +257,8 @@ void list_super2(struct ext2_super_block * sb, FILE *f)
        print_super_flags(sb, f);
        print_mntopts(sb, f);
        if (sb->s_mount_opts[0])
-               fprintf(f, "Mount options:            %s\n", sb->s_mount_opts);
+               fprintf(f, "Mount options:            %.*s\n",
+                       EXT2_LEN_STR(sb->s_mount_opts));
        fprintf(f, "Filesystem state:        ");
        print_fs_state (f, sb->s_state);
        fprintf(f, "\n");
@@ -419,10 +418,8 @@ void list_super2(struct ext2_super_block * sb, FILE *f)
        if (sb->s_first_error_time) {
                tm = sb->s_first_error_time;
                fprintf(f, "First error time:         %s", ctime(&tm));
-               memset(buf, 0, sizeof(buf));
-               strncpy(buf, (char *)sb->s_first_error_func,
-                       sizeof(sb->s_first_error_func));
-               fprintf(f, "First error function:     %s\n", buf);
+               fprintf(f, "First error function:     %.*s\n",
+                       EXT2_LEN_STR(sb->s_first_error_func));
                fprintf(f, "First error line #:       %u\n",
                        sb->s_first_error_line);
                fprintf(f, "First error inode #:      %u\n",
@@ -433,10 +430,8 @@ void list_super2(struct ext2_super_block * sb, FILE *f)
        if (sb->s_last_error_time) {
                tm = sb->s_last_error_time;
                fprintf(f, "Last error time:          %s", ctime(&tm));
-               memset(buf, 0, sizeof(buf));
-               strncpy(buf, (char *)sb->s_last_error_func,
-                       sizeof(sb->s_last_error_func));
-               fprintf(f, "Last error function:      %s\n", buf);
+               fprintf(f, "Last error function:      %.*s\n",
+                       EXT2_LEN_STR(sb->s_last_error_func));
                fprintf(f, "Last error line #:        %u\n",
                        sb->s_last_error_line);
                fprintf(f, "Last error inode #:       %u\n",
index 67c44e9dcd6efbc8391c9af4be92ff1a2771945a..d31891c448a30d32c8dad9712eb2fbb58f0ca513 100644 (file)
@@ -682,8 +682,8 @@ struct ext2_super_block {
 /*060*/        __u32   s_feature_incompat;     /* incompatible feature set */
        __u32   s_feature_ro_compat;    /* readonly-compatible feature set */
 /*068*/        __u8    s_uuid[16];             /* 128-bit uuid for volume */
-/*078*/        char    s_volume_name[EXT2_LABEL_LEN];  /* volume name */
-/*088*/        char    s_last_mounted[64];     /* directory where last mounted */
+/*078*/        __u8    s_volume_name[EXT2_LABEL_LEN];  /* volume name, no NUL? */
+/*088*/        __u8    s_last_mounted[64];     /* directory last mounted on, no NUL? */
 /*0c8*/        __u32   s_algorithm_usage_bitmap; /* For compression */
        /*
         * Performance hints.  Directory preallocation should only
@@ -731,15 +731,15 @@ struct ext2_super_block {
        __u32   s_first_error_time;     /* first time an error happened */
        __u32   s_first_error_ino;      /* inode involved in first error */
 /*1a0*/        __u64   s_first_error_block;    /* block involved in first error */
-       __u8    s_first_error_func[32]; /* function where the error happened */
+       __u8    s_first_error_func[32]; /* function where error hit, no NUL? */
 /*1c8*/        __u32   s_first_error_line;     /* line number where error happened */
        __u32   s_last_error_time;      /* most recent time of an error */
 /*1d0*/        __u32   s_last_error_ino;       /* inode involved in last error */
        __u32   s_last_error_line;      /* line number where error happened */
        __u64   s_last_error_block;     /* block involved of last error */
-/*1e0*/        __u8    s_last_error_func[32];  /* function where the error happened */
+/*1e0*/        __u8    s_last_error_func[32];  /* function where error hit, no NUL? */
 #define EXT4_S_ERR_END ext4_offsetof(struct ext2_super_block, s_mount_opts)
-/*200*/        __u8    s_mount_opts[64];
+/*200*/        __u8    s_mount_opts[64];       /* default mount options, no NUL? */
 /*240*/        __u32   s_usr_quota_inum;       /* inode number of user quota file */
        __u32   s_grp_quota_inum;       /* inode number of group quota file */
        __u32   s_overhead_blocks;      /* overhead blocks/clusters in fs */
@@ -763,6 +763,7 @@ struct ext2_super_block {
 };
 
 #define EXT4_S_ERR_LEN (EXT4_S_ERR_END - EXT4_S_ERR_START)
+#define EXT2_LEN_STR(buf) (int)sizeof(buf), (char *)buf
 
 /*
  * Codes for operating systems
index a7268980e9b18337e2d12d9de779345d87f2c348..024f205e8cd4f0370b0b0715b1d9714200730312 100644 (file)
@@ -101,7 +101,6 @@ static void print_ext2_info(const char *device)
        ext2_filsys             fs;
        errcode_t               retval;
        time_t                  tm;
-       char                    buf[80];
 
        retval = ext2fs_open2(device, 0, EXT2_FLAG_64BITS, 0, 0,
                              unix_io_manager, &fs);
@@ -111,13 +110,10 @@ static void print_ext2_info(const char *device)
 
        if (sb->s_mtime) {
                tm = sb->s_mtime;
-               if (sb->s_last_mounted[0]) {
-                       memset(buf, 0, sizeof(buf));
-                       strncpy(buf, sb->s_last_mounted,
-                               sizeof(sb->s_last_mounted));
-                       printf(_("\tlast mounted on %s on %s"), buf,
-                              ctime(&tm));
-               } else
+               if (sb->s_last_mounted[0])
+                       printf(_("\tlast mounted on %.*s on %s"),
+                              EXT2_LEN_STR(sb->s_last_mounted), ctime(&tm));
+               else
                        printf(_("\tlast mounted on %s"), ctime(&tm));
        } else if (sb->s_mkfs_time) {
                tm = sb->s_mkfs_time;
index 403cd4f6fb6ba1a7ec0d9d31d921624776a3f294..a6e135034ce3c3b23c3b7c7326350cd52d75b875 100644 (file)
@@ -441,10 +441,8 @@ static int check_mmp(ext2_filsys fs)
                                fprintf(stderr,
                                        "%s: MMP update by '%.*s%.*s' at %s",
                                        program_name,
-                                       (int)sizeof(mmp->mmp_nodename),
-                                       (char *)mmp->mmp_nodename,
-                                       (int)sizeof(mmp->mmp_bdevname),
-                                       (char *)mmp->mmp_bdevname,
+                                       EXT2_LEN_STR(mmp->mmp_nodename),
+                                       EXT2_LEN_STR(mmp->mmp_bdevname),
                                        ctime(&mmp_time));
                        }
                        retval = 1;
@@ -494,9 +492,9 @@ static void print_mmp_block(ext2_filsys fs)
        printf("    mmp_update_date: %s", ctime(&mmp_time));
        printf("    mmp_update_time: %lld\n", mmp->mmp_time);
        printf("    mmp_node_name: %.*s\n",
-              (int)sizeof(mmp->mmp_nodename), (char *)mmp->mmp_nodename);
+              EXT2_LEN_STR(mmp->mmp_nodename));
        printf("    mmp_device_name: %.*s\n",
-              (int)sizeof(mmp->mmp_bdevname), (char *)mmp->mmp_bdevname);
+              EXT2_LEN_STR(mmp->mmp_bdevname));
 }
 
 static void parse_extended_opts(const char *opts, blk64_t *superblock,
index 526a62c359ab0bba87c78da8c138efad0dcbc82b..910ccc77221a41d9f1d1be4b100d989a8d2c57e3 100644 (file)
@@ -81,7 +81,7 @@ static void print_label (char *dev)
        char label[VOLNAMSZ+1];
 
        open_e2fs (dev, O_RDONLY);
-       strncpy(label, sb.s_volume_name, VOLNAMSZ);
+       snprintf(label, sizeof(label), "%.*s", EXT2_LEN_STR(sb.s_volume_name));
        label[VOLNAMSZ] = 0;
        printf("%s\n", label);
 }
index ff20b98853776ba436372dac2bf9499e43444df5..765295c3b4b69bd5dce8780643df1b3787f9ce4d 100644 (file)
@@ -251,7 +251,7 @@ int main(int argc, char *argv[])
                        sb_offset = 0;
                if (jnl_copy && !print_jnl_copies)
                        continue;
-               printf("\r%11Lu %11Lu%s %11Lu%s %9u %5Lu %4u%s %s %02x%02x%02x%02x %s\n",
+               printf("\r%11Lu %11Lu%s %11Lu%s %9u %5Lu %4u%s %s %02x%02x%02x%02x %.*s\n",
                       sk, sk - ext2.s_block_group_nr * grpsize - sb_offset,
                       jnl_copy ? "*":" ",
                       sk + ext2fs_blocks_count(&ext2) * bsize -
@@ -259,7 +259,8 @@ int main(int argc, char *argv[])
                       jnl_copy ? "*" : " ", ext2fs_blocks_count(&ext2), bsize,
                       ext2.s_block_group_nr, jnl_copy ? "*" : " ", s,
                       ext2.s_uuid[0], ext2.s_uuid[1],
-                      ext2.s_uuid[2], ext2.s_uuid[3], ext2.s_volume_name);
+                      ext2.s_uuid[2], ext2.s_uuid[3],
+                      EXT2_LEN_STR(ext2.s_volume_name));
        }
        printf(_("\n%11Lu: finished with errno %d\n"), sk, errno);
        close(fd);
index 30e353d335c3e018c8dec94c0f36ca14f7fbc7f9..27e7d174e24b8432af4f30adf12e7e0575db2b56 100644 (file)
@@ -655,7 +655,6 @@ write_superblock:
 static void show_stats(ext2_filsys fs)
 {
        struct ext2_super_block *s = fs->super;
-       char                    buf[80];
         char                    *os;
        blk64_t                 group_block;
        dgrp_t                  i;
@@ -673,9 +672,8 @@ static void show_stats(ext2_filsys fs)
                fprintf(stderr, _("warning: %llu blocks unused.\n\n"),
                       ext2fs_blocks_count(&fs_param) - ext2fs_blocks_count(s));
 
-       memset(buf, 0, sizeof(buf));
-       strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
-       printf(_("Filesystem label=%s\n"), buf);
+       printf(_("Filesystem label=%.*s\n"), EXT2_LEN_STR(s->s_volume_name));
+
        os = e2p_os2string(fs->super->s_creator_os);
        if (os)
                printf(_("OS type: %s\n"), os);
index 301cf3880724458b382a391921a91c6fd25d82ec..923fbb7a1d886c4356f93e7562872308f6d183fe 100644 (file)
@@ -2986,8 +2986,7 @@ retry_open:
 
        if (print_label) {
                /* For e2label emulation */
-               printf("%.*s\n", (int) sizeof(sb->s_volume_name),
-                      sb->s_volume_name);
+               printf("%.*s\n", EXT2_LEN_STR(sb->s_volume_name));
                remove_error_table(&et_ext2_error_table);
                goto closefs;
        }
index 6239b360daff7b33ff4d523502495babf6d0e326..dcd2f0a7872c343252a73be3654e619ac11a87d0 100644 (file)
@@ -289,9 +289,7 @@ void dump_mmp_msg(struct mmp_struct *mmp, const char *msg)
                time_t t = mmp->mmp_time;
 
                printf("MMP error info: node: %.*s, device: %.*s, updated: %s",
-                      (int)sizeof(mmp->mmp_nodename),
-                      (char *)mmp->mmp_nodename,
-                      (int)sizeof(mmp->mmp_bdevname),
-                      (char *)mmp->mmp_bdevname, ctime(&t));
+                      EXT2_LEN_STR(mmp->mmp_nodename),
+                      EXT2_LEN_STR(mmp->mmp_bdevname), ctime(&t));
        }
 }