libbb: introduce kernel-style BUILD_BUG_ON()

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2015-10-13 14:50:20 +02:00
parent f7ad927c20
commit ab3964db4e
9 changed files with 20 additions and 39 deletions

View File

@ -1901,6 +1901,7 @@ extern const char bb_default_login_shell[] ALIGN1;
#define ARRAY_SIZE(x) ((unsigned)(sizeof(x) / sizeof((x)[0]))) #define ARRAY_SIZE(x) ((unsigned)(sizeof(x) / sizeof((x)[0])))
#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
/* We redefine ctype macros. Unicode-correct handling of char types /* We redefine ctype macros. Unicode-correct handling of char types

View File

@ -283,7 +283,6 @@ int FAST_FUNC procps_read_smaps(pid_t pid, struct smaprec *total,
} }
#endif #endif
void BUG_comm_size(void);
procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags) procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags)
{ {
if (!sp) if (!sp)
@ -385,8 +384,7 @@ procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags)
/*if (!cp || cp[1] != ' ') /*if (!cp || cp[1] != ' ')
continue;*/ continue;*/
cp[0] = '\0'; cp[0] = '\0';
if (sizeof(sp->comm) < 16) BUILD_BUG_ON(sizeof(sp->comm) < 16);
BUG_comm_size();
comm1 = strchr(buf, '('); comm1 = strchr(buf, '(');
/*if (comm1)*/ /*if (comm1)*/
safe_strncpy(sp->comm, comm1 + 1, sizeof(sp->comm)); safe_strncpy(sp->comm, comm1 + 1, sizeof(sp->comm));

View File

@ -62,9 +62,6 @@ struct globals {
} FIX_ALIASING; } FIX_ALIASING;
#define G (*(struct globals*)&bb_common_bufsiz1) #define G (*(struct globals*)&bb_common_bufsiz1)
enum { BUFSZ = COMMON_BUFSIZE - offsetof(struct globals, buf) }; enum { BUFSZ = COMMON_BUFSIZE - offsetof(struct globals, buf) };
struct BUG_G_too_big {
char BUG_G_too_big[sizeof(G) <= COMMON_BUFSIZE ? 1 : -1];
};
#define user (G.user ) #define user (G.user )
#define password (G.password ) #define password (G.password )
#define lsa (G.lsa ) #define lsa (G.lsa )
@ -72,7 +69,9 @@ struct BUG_G_too_big {
#define verbose_flag (G.verbose_flag ) #define verbose_flag (G.verbose_flag )
#define do_continue (G.do_continue ) #define do_continue (G.do_continue )
#define buf (G.buf ) #define buf (G.buf )
#define INIT_G() do { } while (0) #define INIT_G() do { \
BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
} while (0)
static void ftp_die(const char *msg) NORETURN; static void ftp_die(const char *msg) NORETURN;

View File

@ -194,7 +194,6 @@ static void handle_accept(isrv_state_t *state, int fd)
remove_peer(state, n); /* unsuccesful peer start */ remove_peer(state, n); /* unsuccesful peer start */
} }
void BUG_sizeof_fd_set_is_strange(void);
static void handle_fd_set(isrv_state_t *state, fd_set *fds, int (*h)(int, void **)) static void handle_fd_set(isrv_state_t *state, fd_set *fds, int (*h)(int, void **))
{ {
enum { LONG_CNT = sizeof(fd_set) / sizeof(long) }; enum { LONG_CNT = sizeof(fd_set) / sizeof(long) };
@ -203,8 +202,7 @@ static void handle_fd_set(isrv_state_t *state, fd_set *fds, int (*h)(int, void *
/* need to know value at _the beginning_ of this routine */ /* need to know value at _the beginning_ of this routine */
int fd_cnt = FD_COUNT; int fd_cnt = FD_COUNT;
if (LONG_CNT * sizeof(long) != sizeof(fd_set)) BUILD_BUG_ON(LONG_CNT * sizeof(long) != sizeof(fd_set));
BUG_sizeof_fd_set_is_strange();
fds_pos = 0; fds_pos = 0;
while (1) { while (1) {

View File

@ -396,10 +396,8 @@ struct globals {
#define dotted (G.dotted ) #define dotted (G.dotted )
#define pingaddr (G.pingaddr ) #define pingaddr (G.pingaddr )
#define rcvd_tbl (G.rcvd_tbl ) #define rcvd_tbl (G.rcvd_tbl )
void BUG_ping_globals_too_big(void);
#define INIT_G() do { \ #define INIT_G() do { \
if (sizeof(G) > COMMON_BUFSIZE) \ BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
BUG_ping_globals_too_big(); \
datalen = DEFDATALEN; \ datalen = DEFDATALEN; \
timeout = MAXWAIT; \ timeout = MAXWAIT; \
tmin = UINT_MAX; \ tmin = UINT_MAX; \
@ -732,7 +730,6 @@ static void ping4(len_and_sockaddr *lsa)
} }
} }
#if ENABLE_PING6 #if ENABLE_PING6
extern int BUG_bad_offsetof_icmp6_cksum(void);
static void ping6(len_and_sockaddr *lsa) static void ping6(len_and_sockaddr *lsa)
{ {
int sockopt; int sockopt;
@ -769,8 +766,7 @@ static void ping6(len_and_sockaddr *lsa)
setsockopt_SOL_SOCKET_int(pingsock, SO_RCVBUF, sockopt); setsockopt_SOL_SOCKET_int(pingsock, SO_RCVBUF, sockopt);
sockopt = offsetof(struct icmp6_hdr, icmp6_cksum); sockopt = offsetof(struct icmp6_hdr, icmp6_cksum);
if (offsetof(struct icmp6_hdr, icmp6_cksum) != 2) BUILD_BUG_ON(offsetof(struct icmp6_hdr, icmp6_cksum) != 2);
BUG_bad_offsetof_icmp6_cksum();
setsockopt_int(pingsock, SOL_RAW, IPV6_CHECKSUM, sockopt); setsockopt_int(pingsock, SOL_RAW, IPV6_CHECKSUM, sockopt);
/* request ttl info to be returned in ancillary data */ /* request ttl info to be returned in ancillary data */

View File

@ -64,15 +64,14 @@ struct globals {
uint32_t filter_proto; uint32_t filter_proto;
} FIX_ALIASING; } FIX_ALIASING;
#define G (*(struct globals*)&bb_common_bufsiz1) #define G (*(struct globals*)&bb_common_bufsiz1)
struct BUG_G_too_big {
char BUG_G_too_big[sizeof(G) <= COMMON_BUFSIZE ? 1 : -1];
};
#define filter_ifindex (G.filter_ifindex) #define filter_ifindex (G.filter_ifindex)
#define filter_qdisc (G.filter_qdisc) #define filter_qdisc (G.filter_qdisc)
#define filter_parent (G.filter_parent) #define filter_parent (G.filter_parent)
#define filter_prio (G.filter_prio) #define filter_prio (G.filter_prio)
#define filter_proto (G.filter_proto) #define filter_proto (G.filter_proto)
#define INIT_G() do { } while (0) #define INIT_G() do { \
BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
} while (0)
/* Allocates a buffer containing the name of a class id. /* Allocates a buffer containing the name of a class id.
* The caller must free the returned memory. */ * The caller must free the returned memory. */

View File

@ -129,10 +129,9 @@ struct globals {
#endif #endif
} FIX_ALIASING; } FIX_ALIASING;
#define G (*(struct globals*)&bb_common_bufsiz1) #define G (*(struct globals*)&bb_common_bufsiz1)
struct BUG_G_too_big { #define INIT_G() do { \
char BUG_G_too_big[sizeof(G) <= COMMON_BUFSIZE ? 1 : -1]; BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
}; } while (0)
#define INIT_G() do { } while (0)
#define G_error_pkt_reason (G.error_pkt[3]) #define G_error_pkt_reason (G.error_pkt[3])
#define G_error_pkt_str ((char*)(G.error_pkt + 4)) #define G_error_pkt_str ((char*)(G.error_pkt + 4))

View File

@ -49,16 +49,11 @@ static void gettimeofday_ns(struct timespec *ts)
#else #else
static void gettimeofday_ns(struct timespec *ts) static void gettimeofday_ns(struct timespec *ts)
{ {
if (sizeof(struct timeval) == sizeof(struct timespec) BUILD_BUG_ON(sizeof(struct timeval) != sizeof(struct timespec));
&& sizeof(((struct timeval*)ts)->tv_usec) == sizeof(ts->tv_nsec) BUILD_BUG_ON(sizeof(((struct timeval*)ts)->tv_usec) != sizeof(ts->tv_nsec));
) {
/* Cheat */ /* Cheat */
gettimeofday((void*)ts, NULL); gettimeofday((void*)ts, NULL);
ts->tv_nsec *= 1000; ts->tv_nsec *= 1000;
} else {
extern void BUG_need_to_implement_gettimeofday_ns(void);
BUG_need_to_implement_gettimeofday_ns();
}
} }
#endif #endif

View File

@ -82,11 +82,7 @@ int umount_main(int argc UNUSED_PARAM, char **argv)
// MNT_FORCE and MNT_DETACH (from linux/fs.h) must match // MNT_FORCE and MNT_DETACH (from linux/fs.h) must match
// OPT_FORCE and OPT_LAZY. // OPT_FORCE and OPT_LAZY.
{ BUILD_BUG_ON(OPT_FORCE != MNT_FORCE || OPT_LAZY != MNT_DETACH);
typedef char bug[
(OPT_FORCE != MNT_FORCE || OPT_LAZY != MNT_DETACH) ? -1 : 1
];
}
doForce = opt & (OPT_FORCE|OPT_LAZY); doForce = opt & (OPT_FORCE|OPT_LAZY);
/* Get a list of mount points from mtab. We read them all in now mostly /* Get a list of mount points from mtab. We read them all in now mostly