Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

BUILD_BUG_ON(): fix it and a couple of bogus uses of it

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>

authored by

Jan Beulich and committed by
Linus Torvalds
8c87df45 1fe72eaa

+13 -10
+1 -1
arch/sparc/include/asm/vio.h
··· 258 258 static inline u32 vio_dring_avail(struct vio_dring_state *dr, 259 259 unsigned int ring_size) 260 260 { 261 - BUILD_BUG_ON(!is_power_of_2(ring_size)); 261 + MAYBE_BUILD_BUG_ON(!is_power_of_2(ring_size)); 262 262 263 263 return (dr->pending - 264 264 ((dr->prod - dr->cons) & (ring_size - 1)));
+2 -2
drivers/char/tpm/tpm.c
··· 696 696 697 697 cmd.header.in = pcrread_header; 698 698 cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx); 699 - BUILD_BUG_ON(cmd.header.in.length > READ_PCR_RESULT_SIZE); 699 + BUG_ON(cmd.header.in.length > READ_PCR_RESULT_SIZE); 700 700 rc = transmit_cmd(chip, &cmd, cmd.header.in.length, 701 701 "attempting to read a pcr value"); 702 702 ··· 760 760 return -ENODEV; 761 761 762 762 cmd.header.in = pcrextend_header; 763 - BUILD_BUG_ON(be32_to_cpu(cmd.header.in.length) > EXTEND_PCR_SIZE); 763 + BUG_ON(be32_to_cpu(cmd.header.in.length) > EXTEND_PCR_SIZE); 764 764 cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx); 765 765 memcpy(cmd.params.pcrextend_in.hash, hash, TPM_DIGEST_SIZE); 766 766 rc = transmit_cmd(chip, &cmd, cmd.header.in.length,
+1 -1
drivers/net/niu.c
··· 5615 5615 /* The XMAC_MIN register only accepts values for TX min which 5616 5616 * have the low 3 bits cleared. 5617 5617 */ 5618 - BUILD_BUG_ON(min & 0x7); 5618 + BUG_ON(min & 0x7); 5619 5619 5620 5620 if (np->flags & NIU_FLAGS_XMAC) 5621 5621 niu_init_tx_xmac(np, min, max);
+1 -1
include/linux/gfp.h
··· 220 220 ((1 << ZONES_SHIFT) - 1); 221 221 222 222 if (__builtin_constant_p(bit)) 223 - BUILD_BUG_ON((GFP_ZONE_BAD >> bit) & 1); 223 + MAYBE_BUILD_BUG_ON((GFP_ZONE_BAD >> bit) & 1); 224 224 else { 225 225 #ifdef CONFIG_DEBUG_VM 226 226 BUG_ON((GFP_ZONE_BAD >> bit) & 1);
+6 -2
include/linux/kernel.h
··· 678 678 }; 679 679 680 680 /* Force a compilation error if condition is true */ 681 - #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) 681 + #define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition)) 682 + 683 + /* Force a compilation error if condition is constant and true */ 684 + #define MAYBE_BUILD_BUG_ON(cond) ((void)sizeof(char[1 - 2 * !!(cond)])) 682 685 683 686 /* Force a compilation error if condition is true, but also produce a 684 687 result (of value 0 and type size_t), so the expression can be used 685 688 e.g. in a structure initializer (or where-ever else comma expressions 686 689 aren't permitted). */ 687 - #define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1) 690 + #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); })) 691 + #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); })) 688 692 689 693 /* Trap pasters of __FUNCTION__ at compile-time */ 690 694 #define __FUNCTION__ (__func__)
+1 -1
include/linux/kmemcheck.h
··· 152 152 \ 153 153 _n = (long) &((ptr)->name##_end) \ 154 154 - (long) &((ptr)->name##_begin); \ 155 - BUILD_BUG_ON(_n < 0); \ 155 + MAYBE_BUILD_BUG_ON(_n < 0); \ 156 156 \ 157 157 kmemcheck_mark_initialized(&((ptr)->name##_begin), _n); \ 158 158 } while (0)
+1 -2
include/linux/virtio_config.h
··· 109 109 unsigned int fbit) 110 110 { 111 111 /* Did you forget to fix assumptions on max features? */ 112 - if (__builtin_constant_p(fbit)) 113 - BUILD_BUG_ON(fbit >= 32); 112 + MAYBE_BUILD_BUG_ON(fbit >= 32); 114 113 115 114 if (fbit < VIRTIO_TRANSPORT_F_START) 116 115 virtio_check_driver_offered_feature(vdev, fbit);