From 3577d48e74381419b6ea8b57a57987eff28ace31 Mon Sep 17 00:00:00 2001 From: Stephen Heumann Date: Sat, 1 Nov 2014 15:47:17 -0500 Subject: [PATCH] Change the ARRAY_SIZE macro to work around an ORCA/C limitation. --- include/libbb.h | 5 ++++- shell/hush.c | 10 +++++----- shell/shell_common.c | 6 +++--- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/include/libbb.h b/include/libbb.h index 28a6a2837..1ab843669 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -1651,7 +1651,10 @@ extern const char bb_default_login_shell[] ALIGN1; #endif -#define ARRAY_SIZE(x) ((unsigned)(sizeof(x) / sizeof((x)[0]))) +#define ARRAY_SIZE(x) ((size_t)(sizeof(x) / sizeof((x)[0]))) +/* ORCA/C will sometimes barf on the expression in ARRAY_SIZE, depending on the element type + * (e.g. for arrays of structs). When it does, use ARRAY_SIZE2 instead. */ +#define ARRAY_SIZE2(x, elttype) ((size_t)(sizeof(x) / sizeof(elttype))) /* We redefine ctype macros. Unicode-correct handling of char types diff --git a/shell/hush.c b/shell/hush.c index ca46b25f5..3a5a600e4 100644 --- a/shell/hush.c +++ b/shell/hush.c @@ -3108,7 +3108,7 @@ static const struct reserved_combo* match_reserved_word(o_string *word) }; const struct reserved_combo *r; - for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) { + for (r = reserved_list; r < reserved_list + ARRAY_SIZE2(reserved_list, struct reserved_combo); r++) { if (strcmp(word->data, r->literal) == 0) return r; } @@ -3999,7 +3999,7 @@ static int parse_dollar(o_string *as_string, if (last_ch == 0) /* error? */ return 0; #else -#error Simple code to only allow ${var} is not implemented +#error "Simple code to only allow ${var} is not implemented" #endif if (as_string) { o_addstr(as_string, dest->data + pos); @@ -6214,14 +6214,14 @@ static const struct built_in_command *find_builtin_helper(const char *name, } static const struct built_in_command *find_builtin1(const char *name) { - return find_builtin_helper(name, bltins1, &bltins1[ARRAY_SIZE(bltins1)]); + return find_builtin_helper(name, bltins1, &bltins1[ARRAY_SIZE2(bltins1, struct built_in_command)]); } static const struct built_in_command *find_builtin(const char *name) { const struct built_in_command *x = find_builtin1(name); if (x) return x; - return find_builtin_helper(name, bltins2, &bltins2[ARRAY_SIZE(bltins2)]); + return find_builtin_helper(name, bltins2, &bltins2[ARRAY_SIZE2(bltins2, struct built_in_command)]); } #if ENABLE_HUSH_FUNCTIONS @@ -8723,7 +8723,7 @@ static int FAST_FUNC builtin_help(char **argv UNUSED_PARAM) printf( "Built-in commands:\n" "------------------\n"); - for (x = bltins1; x != &bltins1[ARRAY_SIZE(bltins1)]; x++) { + for (x = bltins1; x != &bltins1[ARRAY_SIZE2(bltins1, struct built_in_command)]; x++) { if (x->b_descr) printf("%-10s%s\n", x->b_cmd, x->b_descr); } diff --git a/shell/shell_common.c b/shell/shell_common.c index d691fe3c2..b234d489f 100644 --- a/shell/shell_common.c +++ b/shell/shell_common.c @@ -432,7 +432,7 @@ shell_builtin_ulimit(char **argv) } if (opt_char == 'a') { - for (l = limits_tbl; l != &limits_tbl[ARRAY_SIZE(limits_tbl)]; l++) { + for (l = limits_tbl; l != &limits_tbl[ARRAY_SIZE2(limits_tbl, struct limits)]; l++) { getrlimit(l->cmd, &limit); printf("-%c: %-30s ", l->option, l->name); printlim(opts, &limit, l); @@ -442,7 +442,7 @@ shell_builtin_ulimit(char **argv) if (opt_char == 1) opt_char = 'f'; - for (l = limits_tbl; l != &limits_tbl[ARRAY_SIZE(limits_tbl)]; l++) { + for (l = limits_tbl; l != &limits_tbl[ARRAY_SIZE2(limits_tbl, struct limits)]; l++) { if (opt_char == l->option) { char *val_str; @@ -489,7 +489,7 @@ shell_builtin_ulimit(char **argv) } } /* for (every possible opt) */ - if (l == &limits_tbl[ARRAY_SIZE(limits_tbl)]) { + if (l == &limits_tbl[ARRAY_SIZE2(limits_tbl, struct limits)]) { /* bad option. getopt already complained. */ break; }