Linux NILFS development
 help / color / mirror / Atom feed
* [PATCH] mkfs: check sizes of important structs at build time
@ 2014-01-04 13:29 Hitoshi Mitake
       [not found] ` <1388842171-16105-1-git-send-email-mitake.hitoshi-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
  0 siblings, 1 reply; 8+ messages in thread
From: Hitoshi Mitake @ 2014-01-04 13:29 UTC (permalink / raw)
  To: linux-nilfs-u79uwXL29TY76Z2rM5mHXA
  Cc: mitake.hitoshi-Re5JQEeQqe8AvxtiuMwx3w, Hitoshi Mitake

Current nilfs_check_ondisk_sizes() checks sizes of important structs
at run time. The checking should be done at build time. This patch
adds a new macro, BUILD_BUG_ON(), for this purpose. It is similar to
static_assert() of C++11. If an argument is true, the macro causes a
bulid error.

Below is an example of BUILD_BUG_ON(). When the checked conditions are
true like below:

/* intentional change for testing BUILD_BUG_ON() */

static __attribute__((used)) void nilfs_check_ondisk_sizes(void)
{
	BUILD_BUG_ON(sizeof(struct nilfs_inode) > NILFS_MIN_BLOCKSIZE);
...

build process of mkfs.o causes errors like this:

gcc -std=gnu99 -DHAVE_CONFIG_H -I. -I../..  -I../../include  -Wall -g -O2 -MT mkfs.o -MD -MP -MF .deps/mkfs.Tpo -c -o mkfs.o mkfs.c
mkfs.c: In function 'nilfs_check_ondisk_sizes':
mkfs.c:429:2: error: negative width in bit-field '<anonymous>'
mkfs.c:430:2: error: negative width in bit-field '<anonymous>'
mkfs.c:431:2: error: negative width in bit-field '<anonymous>'
mkfs.c:432:2: error: negative width in bit-field '<anonymous>'
mkfs.c:433:2: error: negative width in bit-field '<anonymous>'
mkfs.c:434:2: error: negative width in bit-field '<anonymous>'
mkfs.c:435:2: error: negative width in bit-field '<anonymous>'

Signed-off-by: Hitoshi Mitake <mitake.hitoshi-Zyj7fXuS5i5L9jVzuh4AOg@public.gmane.org>
---
 sbin/mkfs/mkfs.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/sbin/mkfs/mkfs.c b/sbin/mkfs/mkfs.c
index 4e153ce..8eb00bf 100644
--- a/sbin/mkfs/mkfs.c
+++ b/sbin/mkfs/mkfs.c
@@ -85,6 +85,9 @@
 typedef __u64  blocknr_t;
 
 #define BUG_ON(x)	   assert(!(x))
+/* Force a compilation error if the condition is true */
+#define BUILD_BUG_ON(condition) ((void)sizeof(struct { int: -!!(condition); }))
+
 #define ROUNDUP_DIV(n, m)	(((n) - 1) / (m) + 1)
 #define max_t(type, x, y) \
 	({ type __x = (x); type __y = (y); __x > __y ? __x : __y; })
@@ -417,16 +420,15 @@ static unsigned count_dat_blocks(unsigned nr_dat_entries)
 	return nblocks;
 }
 
-static void nilfs_check_ondisk_sizes(void)
+static __attribute__((used)) void nilfs_check_ondisk_sizes(void)
 {
-	if (sizeof(struct nilfs_inode) > NILFS_MIN_BLOCKSIZE ||
-	    sizeof(struct nilfs_sufile_header) > NILFS_MIN_BLOCKSIZE ||
-	    sizeof(struct nilfs_segment_usage) > NILFS_MIN_BLOCKSIZE ||
-	    sizeof(struct nilfs_cpfile_header) > NILFS_MIN_BLOCKSIZE ||
-	    sizeof(struct nilfs_checkpoint) > NILFS_MIN_BLOCKSIZE ||
-	    sizeof(struct nilfs_dat_entry) > NILFS_MIN_BLOCKSIZE ||
-	    sizeof(struct nilfs_super_root) > NILFS_MIN_BLOCKSIZE)
-		perr("Internal error: too large on-disk structure");
+	BUILD_BUG_ON(sizeof(struct nilfs_inode) > NILFS_MIN_BLOCKSIZE);
+	BUILD_BUG_ON(sizeof(struct nilfs_sufile_header) > NILFS_MIN_BLOCKSIZE);
+	BUILD_BUG_ON(sizeof(struct nilfs_segment_usage) > NILFS_MIN_BLOCKSIZE);
+	BUILD_BUG_ON(sizeof(struct nilfs_cpfile_header) > NILFS_MIN_BLOCKSIZE);
+	BUILD_BUG_ON(sizeof(struct nilfs_checkpoint) > NILFS_MIN_BLOCKSIZE);
+	BUILD_BUG_ON(sizeof(struct nilfs_dat_entry) > NILFS_MIN_BLOCKSIZE);
+	BUILD_BUG_ON(sizeof(struct nilfs_super_root) > NILFS_MIN_BLOCKSIZE);
 }
 
 static unsigned long
@@ -523,8 +525,6 @@ static void init_disk_layout(struct nilfs_disk_info *di, int fd,
 		     "or shorten segments with -B option.", dev_size,
 		     (unsigned long long)segment_size * min_nsegments);
 	di->nseginfo = 0;
-
-	nilfs_check_ondisk_sizes();
 }
 
 static struct nilfs_segment_info *new_segment(struct nilfs_disk_info *di)
-- 
1.8.1.2

--
To unsubscribe from this list: send the line "unsubscribe linux-nilfs" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2014-01-05 17:17 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-01-04 13:29 [PATCH] mkfs: check sizes of important structs at build time Hitoshi Mitake
     [not found] ` <1388842171-16105-1-git-send-email-mitake.hitoshi-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2014-01-04 14:52   ` Vyacheslav Dubeyko
     [not found]     ` <EF28F22A-C43F-41C9-A5B9-8597C5879E3E-yeENwD64cLxBDgjK7y7TUQ@public.gmane.org>
2014-01-04 13:54       ` Hitoshi Mitake
     [not found]         ` <CAE1WaKLr1EuovgHgXQa1o9LQVk1fRkUXbDrGiKfsdTB347ieqg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2014-01-04 15:39           ` Vyacheslav Dubeyko
     [not found]             ` <2276BC9A-0688-4566-8FA8-D280E6F71F5F-yeENwD64cLxBDgjK7y7TUQ@public.gmane.org>
2014-01-05 15:17               ` Hitoshi Mitake
2014-01-04 16:08   ` Ryusuke Konishi
     [not found]     ` <20140105.010843.356918311.konishi.ryusuke-Zyj7fXuS5i5L9jVzuh4AOg@public.gmane.org>
2014-01-05 15:22       ` Hitoshi Mitake
     [not found]         ` <87wqiea1xt.wl%mitake.hitoshi-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2014-01-05 17:17           ` Ryusuke Konishi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox