From 52162700bb59663add809a6465ce2769d80b3664 Mon Sep 17 00:00:00 2001 From: Zach Brown Date: Thu, 17 Jan 2013 11:54:47 -0800 Subject: [PATCH] btrfs-progs: treat super.magic as an le64 The super block magic is a le64 whose value looks like an unterminated string in memory. The lack of null termination leads to clumsy use of string functions and causes static analysis tools to warn that the string will be unterminated. So let's just treat it as the le64 that it is. Endian wrappers are used on the constant so that they're compiled into run-time constants. Signed-off-by: Zach Brown --- btrfs-show-super.c | 2 +- ctree.h | 2 +- disk-io.c | 6 ++---- utils.c | 5 ++--- 4 files changed, 6 insertions(+), 9 deletions(-) diff --git a/btrfs-show-super.c b/btrfs-show-super.c index 45eb102..3614c52 100644 --- a/btrfs-show-super.c +++ b/btrfs-show-super.c @@ -187,7 +187,7 @@ static void dump_superblock(struct btrfs_super_block *sb) s = (char *) &sb->magic; for (i = 0; i < 8; i++) putchar(isprint(s[i]) ? s[i] : '.'); - if (!memcmp(BTRFS_MAGIC, &sb->magic, 8)) + if (sb->magic == cpu_to_le64(BTRFS_MAGIC)) printf(" [match]\n"); else printf(" [DON'T MATCH]\n"); diff --git a/ctree.h b/ctree.h index d0f6062..18b68e1 100644 --- a/ctree.h +++ b/ctree.h @@ -28,7 +28,7 @@ struct btrfs_root; struct btrfs_trans_handle; -#define BTRFS_MAGIC "_BHRfS_M" +#define BTRFS_MAGIC 0x4D5F53665248425F /* ascii _BHRfS_M, no null */ #define BTRFS_MAX_LEVEL 8 diff --git a/disk-io.c b/disk-io.c index 0bf73f0..d3b8c51 100644 --- a/disk-io.c +++ b/disk-io.c @@ -932,8 +932,7 @@ int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr) return -1; if (btrfs_super_bytenr(&buf) != sb_bytenr || - strncmp((char *)(&buf.magic), BTRFS_MAGIC, - sizeof(buf.magic))) + buf.magic != cpu_to_le64(BTRFS_MAGIC)) return -1; memcpy(sb, &buf, sizeof(*sb)); @@ -951,8 +950,7 @@ int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr) /* if magic is NULL, the device was removed */ if (buf.magic == 0 && i == 0) return -1; - if (strncmp((char *)(&buf.magic), BTRFS_MAGIC, - sizeof(buf.magic))) + if (buf.magic != cpu_to_le64(BTRFS_MAGIC)) continue; if (!fsid_is_initialized) { diff --git a/utils.c b/utils.c index d92f317..d2dec6f 100644 --- a/utils.c +++ b/utils.c @@ -112,7 +112,7 @@ int make_btrfs(int fd, const char *device, const char *label, btrfs_set_super_bytenr(&super, blocks[0]); btrfs_set_super_num_devices(&super, 1); - strncpy((char *)&super.magic, BTRFS_MAGIC, sizeof(super.magic)); + super.magic = cpu_to_le64(BTRFS_MAGIC); btrfs_set_super_generation(&super, 1); btrfs_set_super_root(&super, blocks[1]); btrfs_set_super_chunk_root(&super, blocks[3]); @@ -1085,8 +1085,7 @@ int btrfs_device_already_in_root(struct btrfs_root *root, int fd, ret = 0; disk_super = (struct btrfs_super_block *)buf; - if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC, - sizeof(disk_super->magic))) + if (disk_super->magic != cpu_to_le64(BTRFS_MAGIC)) goto brelse; if (!memcmp(disk_super->fsid, root->fs_info->super_copy.fsid, -- 2.7.4