diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2015-10-13 14:50:20 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2015-10-13 14:50:20 +0200 |
commit | ab3964db4e75e34f6f9347406c5fd2bced04f2dd (patch) | |
tree | 92113e4a008338ed8534858db97d3a5e09dc210b | |
parent | f7ad927c2059ef9cd1cd6befeb43f26b92f6369f (diff) | |
download | busybox-w32-ab3964db4e75e34f6f9347406c5fd2bced04f2dd.tar.gz busybox-w32-ab3964db4e75e34f6f9347406c5fd2bced04f2dd.tar.bz2 busybox-w32-ab3964db4e75e34f6f9347406c5fd2bced04f2dd.zip |
libbb: introduce kernel-style BUILD_BUG_ON()
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | include/libbb.h | 1 | ||||
-rw-r--r-- | libbb/procps.c | 4 | ||||
-rw-r--r-- | networking/ftpgetput.c | 7 | ||||
-rw-r--r-- | networking/isrv.c | 4 | ||||
-rw-r--r-- | networking/ping.c | 8 | ||||
-rw-r--r-- | networking/tc.c | 7 | ||||
-rw-r--r-- | networking/tftp.c | 7 | ||||
-rw-r--r-- | runit/runsv.c | 15 | ||||
-rw-r--r-- | util-linux/umount.c | 6 |
9 files changed, 20 insertions, 39 deletions
diff --git a/include/libbb.h b/include/libbb.h index a8ceb449c..5a270cdca 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -1901,6 +1901,7 @@ extern const char bb_default_login_shell[] ALIGN1; | |||
1901 | 1901 | ||
1902 | 1902 | ||
1903 | #define ARRAY_SIZE(x) ((unsigned)(sizeof(x) / sizeof((x)[0]))) | 1903 | #define ARRAY_SIZE(x) ((unsigned)(sizeof(x) / sizeof((x)[0]))) |
1904 | #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) | ||
1904 | 1905 | ||
1905 | 1906 | ||
1906 | /* We redefine ctype macros. Unicode-correct handling of char types | 1907 | /* We redefine ctype macros. Unicode-correct handling of char types |
diff --git a/libbb/procps.c b/libbb/procps.c index 71ad071e6..05eefe0da 100644 --- a/libbb/procps.c +++ b/libbb/procps.c | |||
@@ -283,7 +283,6 @@ int FAST_FUNC procps_read_smaps(pid_t pid, struct smaprec *total, | |||
283 | } | 283 | } |
284 | #endif | 284 | #endif |
285 | 285 | ||
286 | void BUG_comm_size(void); | ||
287 | procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags) | 286 | procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags) |
288 | { | 287 | { |
289 | if (!sp) | 288 | if (!sp) |
@@ -385,8 +384,7 @@ procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags) | |||
385 | /*if (!cp || cp[1] != ' ') | 384 | /*if (!cp || cp[1] != ' ') |
386 | continue;*/ | 385 | continue;*/ |
387 | cp[0] = '\0'; | 386 | cp[0] = '\0'; |
388 | if (sizeof(sp->comm) < 16) | 387 | BUILD_BUG_ON(sizeof(sp->comm) < 16); |
389 | BUG_comm_size(); | ||
390 | comm1 = strchr(buf, '('); | 388 | comm1 = strchr(buf, '('); |
391 | /*if (comm1)*/ | 389 | /*if (comm1)*/ |
392 | safe_strncpy(sp->comm, comm1 + 1, sizeof(sp->comm)); | 390 | safe_strncpy(sp->comm, comm1 + 1, sizeof(sp->comm)); |
diff --git a/networking/ftpgetput.c b/networking/ftpgetput.c index 8283366cc..b398bc874 100644 --- a/networking/ftpgetput.c +++ b/networking/ftpgetput.c | |||
@@ -62,9 +62,6 @@ struct globals { | |||
62 | } FIX_ALIASING; | 62 | } FIX_ALIASING; |
63 | #define G (*(struct globals*)&bb_common_bufsiz1) | 63 | #define G (*(struct globals*)&bb_common_bufsiz1) |
64 | enum { BUFSZ = COMMON_BUFSIZE - offsetof(struct globals, buf) }; | 64 | enum { BUFSZ = COMMON_BUFSIZE - offsetof(struct globals, buf) }; |
65 | struct BUG_G_too_big { | ||
66 | char BUG_G_too_big[sizeof(G) <= COMMON_BUFSIZE ? 1 : -1]; | ||
67 | }; | ||
68 | #define user (G.user ) | 65 | #define user (G.user ) |
69 | #define password (G.password ) | 66 | #define password (G.password ) |
70 | #define lsa (G.lsa ) | 67 | #define lsa (G.lsa ) |
@@ -72,7 +69,9 @@ struct BUG_G_too_big { | |||
72 | #define verbose_flag (G.verbose_flag ) | 69 | #define verbose_flag (G.verbose_flag ) |
73 | #define do_continue (G.do_continue ) | 70 | #define do_continue (G.do_continue ) |
74 | #define buf (G.buf ) | 71 | #define buf (G.buf ) |
75 | #define INIT_G() do { } while (0) | 72 | #define INIT_G() do { \ |
73 | BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \ | ||
74 | } while (0) | ||
76 | 75 | ||
77 | 76 | ||
78 | static void ftp_die(const char *msg) NORETURN; | 77 | static void ftp_die(const char *msg) NORETURN; |
diff --git a/networking/isrv.c b/networking/isrv.c index 1c6491edd..3673db715 100644 --- a/networking/isrv.c +++ b/networking/isrv.c | |||
@@ -194,7 +194,6 @@ static void handle_accept(isrv_state_t *state, int fd) | |||
194 | remove_peer(state, n); /* unsuccesful peer start */ | 194 | remove_peer(state, n); /* unsuccesful peer start */ |
195 | } | 195 | } |
196 | 196 | ||
197 | void BUG_sizeof_fd_set_is_strange(void); | ||
198 | static void handle_fd_set(isrv_state_t *state, fd_set *fds, int (*h)(int, void **)) | 197 | static void handle_fd_set(isrv_state_t *state, fd_set *fds, int (*h)(int, void **)) |
199 | { | 198 | { |
200 | enum { LONG_CNT = sizeof(fd_set) / sizeof(long) }; | 199 | 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 * | |||
203 | /* need to know value at _the beginning_ of this routine */ | 202 | /* need to know value at _the beginning_ of this routine */ |
204 | int fd_cnt = FD_COUNT; | 203 | int fd_cnt = FD_COUNT; |
205 | 204 | ||
206 | if (LONG_CNT * sizeof(long) != sizeof(fd_set)) | 205 | BUILD_BUG_ON(LONG_CNT * sizeof(long) != sizeof(fd_set)); |
207 | BUG_sizeof_fd_set_is_strange(); | ||
208 | 206 | ||
209 | fds_pos = 0; | 207 | fds_pos = 0; |
210 | while (1) { | 208 | while (1) { |
diff --git a/networking/ping.c b/networking/ping.c index dcbf19682..0eb1ae799 100644 --- a/networking/ping.c +++ b/networking/ping.c | |||
@@ -396,10 +396,8 @@ struct globals { | |||
396 | #define dotted (G.dotted ) | 396 | #define dotted (G.dotted ) |
397 | #define pingaddr (G.pingaddr ) | 397 | #define pingaddr (G.pingaddr ) |
398 | #define rcvd_tbl (G.rcvd_tbl ) | 398 | #define rcvd_tbl (G.rcvd_tbl ) |
399 | void BUG_ping_globals_too_big(void); | ||
400 | #define INIT_G() do { \ | 399 | #define INIT_G() do { \ |
401 | if (sizeof(G) > COMMON_BUFSIZE) \ | 400 | BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \ |
402 | BUG_ping_globals_too_big(); \ | ||
403 | datalen = DEFDATALEN; \ | 401 | datalen = DEFDATALEN; \ |
404 | timeout = MAXWAIT; \ | 402 | timeout = MAXWAIT; \ |
405 | tmin = UINT_MAX; \ | 403 | tmin = UINT_MAX; \ |
@@ -732,7 +730,6 @@ static void ping4(len_and_sockaddr *lsa) | |||
732 | } | 730 | } |
733 | } | 731 | } |
734 | #if ENABLE_PING6 | 732 | #if ENABLE_PING6 |
735 | extern int BUG_bad_offsetof_icmp6_cksum(void); | ||
736 | static void ping6(len_and_sockaddr *lsa) | 733 | static void ping6(len_and_sockaddr *lsa) |
737 | { | 734 | { |
738 | int sockopt; | 735 | int sockopt; |
@@ -769,8 +766,7 @@ static void ping6(len_and_sockaddr *lsa) | |||
769 | setsockopt_SOL_SOCKET_int(pingsock, SO_RCVBUF, sockopt); | 766 | setsockopt_SOL_SOCKET_int(pingsock, SO_RCVBUF, sockopt); |
770 | 767 | ||
771 | sockopt = offsetof(struct icmp6_hdr, icmp6_cksum); | 768 | sockopt = offsetof(struct icmp6_hdr, icmp6_cksum); |
772 | if (offsetof(struct icmp6_hdr, icmp6_cksum) != 2) | 769 | BUILD_BUG_ON(offsetof(struct icmp6_hdr, icmp6_cksum) != 2); |
773 | BUG_bad_offsetof_icmp6_cksum(); | ||
774 | setsockopt_int(pingsock, SOL_RAW, IPV6_CHECKSUM, sockopt); | 770 | setsockopt_int(pingsock, SOL_RAW, IPV6_CHECKSUM, sockopt); |
775 | 771 | ||
776 | /* request ttl info to be returned in ancillary data */ | 772 | /* request ttl info to be returned in ancillary data */ |
diff --git a/networking/tc.c b/networking/tc.c index 76e2e8359..6d1fef993 100644 --- a/networking/tc.c +++ b/networking/tc.c | |||
@@ -64,15 +64,14 @@ struct globals { | |||
64 | uint32_t filter_proto; | 64 | uint32_t filter_proto; |
65 | } FIX_ALIASING; | 65 | } FIX_ALIASING; |
66 | #define G (*(struct globals*)&bb_common_bufsiz1) | 66 | #define G (*(struct globals*)&bb_common_bufsiz1) |
67 | struct BUG_G_too_big { | ||
68 | char BUG_G_too_big[sizeof(G) <= COMMON_BUFSIZE ? 1 : -1]; | ||
69 | }; | ||
70 | #define filter_ifindex (G.filter_ifindex) | 67 | #define filter_ifindex (G.filter_ifindex) |
71 | #define filter_qdisc (G.filter_qdisc) | 68 | #define filter_qdisc (G.filter_qdisc) |
72 | #define filter_parent (G.filter_parent) | 69 | #define filter_parent (G.filter_parent) |
73 | #define filter_prio (G.filter_prio) | 70 | #define filter_prio (G.filter_prio) |
74 | #define filter_proto (G.filter_proto) | 71 | #define filter_proto (G.filter_proto) |
75 | #define INIT_G() do { } while (0) | 72 | #define INIT_G() do { \ |
73 | BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \ | ||
74 | } while (0) | ||
76 | 75 | ||
77 | /* Allocates a buffer containing the name of a class id. | 76 | /* Allocates a buffer containing the name of a class id. |
78 | * The caller must free the returned memory. */ | 77 | * The caller must free the returned memory. */ |
diff --git a/networking/tftp.c b/networking/tftp.c index 8ecd7bb6f..ad9308e52 100644 --- a/networking/tftp.c +++ b/networking/tftp.c | |||
@@ -129,10 +129,9 @@ struct globals { | |||
129 | #endif | 129 | #endif |
130 | } FIX_ALIASING; | 130 | } FIX_ALIASING; |
131 | #define G (*(struct globals*)&bb_common_bufsiz1) | 131 | #define G (*(struct globals*)&bb_common_bufsiz1) |
132 | struct BUG_G_too_big { | 132 | #define INIT_G() do { \ |
133 | char BUG_G_too_big[sizeof(G) <= COMMON_BUFSIZE ? 1 : -1]; | 133 | BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \ |
134 | }; | 134 | } while (0) |
135 | #define INIT_G() do { } while (0) | ||
136 | 135 | ||
137 | #define G_error_pkt_reason (G.error_pkt[3]) | 136 | #define G_error_pkt_reason (G.error_pkt[3]) |
138 | #define G_error_pkt_str ((char*)(G.error_pkt + 4)) | 137 | #define G_error_pkt_str ((char*)(G.error_pkt + 4)) |
diff --git a/runit/runsv.c b/runit/runsv.c index 94d286059..6cf5bcc29 100644 --- a/runit/runsv.c +++ b/runit/runsv.c | |||
@@ -49,16 +49,11 @@ static void gettimeofday_ns(struct timespec *ts) | |||
49 | #else | 49 | #else |
50 | static void gettimeofday_ns(struct timespec *ts) | 50 | static void gettimeofday_ns(struct timespec *ts) |
51 | { | 51 | { |
52 | if (sizeof(struct timeval) == sizeof(struct timespec) | 52 | BUILD_BUG_ON(sizeof(struct timeval) != sizeof(struct timespec)); |
53 | && sizeof(((struct timeval*)ts)->tv_usec) == sizeof(ts->tv_nsec) | 53 | BUILD_BUG_ON(sizeof(((struct timeval*)ts)->tv_usec) != sizeof(ts->tv_nsec)); |
54 | ) { | 54 | /* Cheat */ |
55 | /* Cheat */ | 55 | gettimeofday((void*)ts, NULL); |
56 | gettimeofday((void*)ts, NULL); | 56 | ts->tv_nsec *= 1000; |
57 | ts->tv_nsec *= 1000; | ||
58 | } else { | ||
59 | extern void BUG_need_to_implement_gettimeofday_ns(void); | ||
60 | BUG_need_to_implement_gettimeofday_ns(); | ||
61 | } | ||
62 | } | 57 | } |
63 | #endif | 58 | #endif |
64 | 59 | ||
diff --git a/util-linux/umount.c b/util-linux/umount.c index c6c7441b8..00910977d 100644 --- a/util-linux/umount.c +++ b/util-linux/umount.c | |||
@@ -82,11 +82,7 @@ int umount_main(int argc UNUSED_PARAM, char **argv) | |||
82 | 82 | ||
83 | // MNT_FORCE and MNT_DETACH (from linux/fs.h) must match | 83 | // MNT_FORCE and MNT_DETACH (from linux/fs.h) must match |
84 | // OPT_FORCE and OPT_LAZY. | 84 | // OPT_FORCE and OPT_LAZY. |
85 | { | 85 | BUILD_BUG_ON(OPT_FORCE != MNT_FORCE || OPT_LAZY != MNT_DETACH); |
86 | typedef char bug[ | ||
87 | (OPT_FORCE != MNT_FORCE || OPT_LAZY != MNT_DETACH) ? -1 : 1 | ||
88 | ]; | ||
89 | } | ||
90 | doForce = opt & (OPT_FORCE|OPT_LAZY); | 86 | doForce = opt & (OPT_FORCE|OPT_LAZY); |
91 | 87 | ||
92 | /* Get a list of mount points from mtab. We read them all in now mostly | 88 | /* Get a list of mount points from mtab. We read them all in now mostly |