BUILD_BUG_ON(): fix it and a couple of bogus uses of it
authorJan Beulich <JBeulich@novell.com>
Tue, 22 Sep 2009 23:43:52 +0000 (16:43 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Wed, 23 Sep 2009 14:39:29 +0000 (07:39 -0700)
gcc permitting variable length arrays makes the current construct used for
BUILD_BUG_ON() useless, as that doesn't produce any diagnostic if the
controlling expression isn't really constant.  Instead, this patch makes
it so that a bit field gets used here.  Consequently, those uses where the
condition isn't really constant now also need fixing.

Note that in the gfp.h, kmemcheck.h, and virtio_config.h cases
MAYBE_BUILD_BUG_ON() really just serves documentation purposes - even if
the expression is compile time constant (__builtin_constant_p() yields
true), the array is still deemed of variable length by gcc, and hence the
whole expression doesn't have the intended effect.

[akpm@linux-foundation.org: make arch/sparc/include/asm/vio.h compile]
[akpm@linux-foundation.org: more nonsensical assertions in tpm.c..]
Signed-off-by: Jan Beulich <jbeulich@novell.com>
Cc: Andi Kleen <andi@firstfloor.org>
Cc: Rusty Russell <rusty@rustcorp.com.au>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Rajiv Andrade <srajiv@linux.vnet.ibm.com>
Cc: Mimi Zohar <zohar@us.ibm.com>
Cc: James Morris <jmorris@namei.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
arch/sparc/include/asm/vio.h
drivers/char/tpm/tpm.c
drivers/net/niu.c
include/linux/gfp.h
include/linux/kernel.h
include/linux/kmemcheck.h
include/linux/virtio_config.h

index d4de32f0f8afdb849495f29aff26f3c0f004f7d3..6cdbf7e7351d2acae1bf34d209707e52496878e4 100644 (file)
@@ -258,7 +258,7 @@ static inline void *vio_dring_entry(struct vio_dring_state *dr,
 static inline u32 vio_dring_avail(struct vio_dring_state *dr,
                                  unsigned int ring_size)
 {
-       BUILD_BUG_ON(!is_power_of_2(ring_size));
+       MAYBE_BUILD_BUG_ON(!is_power_of_2(ring_size));
 
        return (dr->pending -
                ((dr->prod - dr->cons) & (ring_size - 1)));
index b0603b2e56844e38e1e4082eaf20ed115d78b07e..32b957efa42047af3065b6e30c31827031e94159 100644 (file)
@@ -696,7 +696,7 @@ int __tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
 
        cmd.header.in = pcrread_header;
        cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx);
-       BUILD_BUG_ON(cmd.header.in.length > READ_PCR_RESULT_SIZE);
+       BUG_ON(cmd.header.in.length > READ_PCR_RESULT_SIZE);
        rc = transmit_cmd(chip, &cmd, cmd.header.in.length,
                          "attempting to read a pcr value");
 
@@ -760,7 +760,7 @@ int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash)
                return -ENODEV;
 
        cmd.header.in = pcrextend_header;
-       BUILD_BUG_ON(be32_to_cpu(cmd.header.in.length) > EXTEND_PCR_SIZE);
+       BUG_ON(be32_to_cpu(cmd.header.in.length) > EXTEND_PCR_SIZE);
        cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx);
        memcpy(cmd.params.pcrextend_in.hash, hash, TPM_DIGEST_SIZE);
        rc = transmit_cmd(chip, &cmd, cmd.header.in.length,
index 76cc2614f480df28cbf1973461529114d4c7d8b7..f9364d0678f27d19cc4ab1e5a0519034e627e2db 100644 (file)
@@ -5615,7 +5615,7 @@ static void niu_init_tx_mac(struct niu *np)
        /* The XMAC_MIN register only accepts values for TX min which
         * have the low 3 bits cleared.
         */
-       BUILD_BUG_ON(min & 0x7);
+       BUG_ON(min & 0x7);
 
        if (np->flags & NIU_FLAGS_XMAC)
                niu_init_tx_xmac(np, min, max);
index f53e9b868c26c053f86c5bcff3cbc582c12bc7b2..557bdad320b624369441119e5f352ccbd41b04a6 100644 (file)
@@ -220,7 +220,7 @@ static inline enum zone_type gfp_zone(gfp_t flags)
                                         ((1 << ZONES_SHIFT) - 1);
 
        if (__builtin_constant_p(bit))
-               BUILD_BUG_ON((GFP_ZONE_BAD >> bit) & 1);
+               MAYBE_BUILD_BUG_ON((GFP_ZONE_BAD >> bit) & 1);
        else {
 #ifdef CONFIG_DEBUG_VM
                BUG_ON((GFP_ZONE_BAD >> bit) & 1);
index 55723afa097bac9477d187b7a38a2a498751c918..63dcaece1ac5349405d284ca592446c6712cb5d9 100644 (file)
@@ -678,13 +678,17 @@ struct sysinfo {
 };
 
 /* Force a compilation error if condition is true */
-#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
+#define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition))
+
+/* Force a compilation error if condition is constant and true */
+#define MAYBE_BUILD_BUG_ON(cond) ((void)sizeof(char[1 - 2 * !!(cond)]))
 
 /* Force a compilation error if condition is true, but also produce a
    result (of value 0 and type size_t), so the expression can be used
    e.g. in a structure initializer (or where-ever else comma expressions
    aren't permitted). */
-#define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
+#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
+#define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
 
 /* Trap pasters of __FUNCTION__ at compile-time */
 #define __FUNCTION__ (__func__)
index 136cdcdf92ef474a438c7767c1541b38036cef90..e880d4cf9e223d1e4ca4b8e4ebe655f599a52b16 100644 (file)
@@ -152,7 +152,7 @@ static inline bool kmemcheck_is_obj_initialized(unsigned long addr, size_t size)
                                                                        \
                _n = (long) &((ptr)->name##_end)                        \
                        - (long) &((ptr)->name##_begin);                \
-               BUILD_BUG_ON(_n < 0);                                   \
+               MAYBE_BUILD_BUG_ON(_n < 0);                             \
                                                                        \
                kmemcheck_mark_initialized(&((ptr)->name##_begin), _n); \
        } while (0)
index e547e3c8ee9a55412e52dabaa9ddc67a423b01ab..0093dd7c1d6f0eaf810001851902444269e82f44 100644 (file)
@@ -109,8 +109,7 @@ static inline bool virtio_has_feature(const struct virtio_device *vdev,
                                      unsigned int fbit)
 {
        /* Did you forget to fix assumptions on max features? */
-       if (__builtin_constant_p(fbit))
-               BUILD_BUG_ON(fbit >= 32);
+       MAYBE_BUILD_BUG_ON(fbit >= 32);
 
        if (fbit < VIRTIO_TRANSPORT_F_START)
                virtio_check_driver_offered_feature(vdev, fbit);