From b083a53b6af945dfcc7cabbd82499ac82715cba0 Mon Sep 17 00:00:00 2001 From: Arturo Bernal Date: Thu, 15 Oct 2026 11:16:55 +0200 Subject: [PATCH] core: Add AP_ARRAY_LEN() and use it for array lengths Add AP_ARRAY_LEN() to ap_config.h and use it instead of sizeof(a) / sizeof((a)[0]) where a is a fixed-size array. Remove the local H2_ALEN() macros replaced by AP_ARRAY_LEN(). --- changes-entries/ap-array-len.txt | 2 ++ include/ap_config.h | 6 ++++++ include/ap_mmn.h | 3 ++- modules/filters/sed1.c | 3 ++- modules/http2/h2.h | 2 -- modules/http2/h2_bucket_beam.c | 2 +- modules/http2/h2_c1_io.c | 2 +- modules/http2/h2_c2_filter.c | 4 ++-- modules/http2/h2_protocol.c | 4 ++-- modules/http2/h2_proxy_session.c | 8 ++++---- modules/http2/h2_proxy_session.h | 2 -- modules/http2/h2_proxy_util.c | 4 ++-- modules/http2/h2_session.c | 12 ++++++------ modules/http2/h2_stream.c | 8 ++++---- modules/http2/h2_util.c | 10 +++++----- modules/http2/h2_util.h | 2 +- modules/http2/mod_http2.c | 9 ++------- modules/md/md_acme.c | 5 +++-- modules/md/md_acme_authz.c | 3 ++- modules/md/md_core.c | 7 ++++--- modules/md/md_store.c | 3 ++- modules/md/md_store_fs.c | 5 +++-- modules/md/md_util.c | 3 ++- modules/md/mod_md.c | 2 +- modules/md/mod_md_status.c | 12 ++++++------ modules/metadata/mod_mime_magic.c | 6 ++---- modules/ssl/mod_ssl_ct.c | 2 +- modules/ssl/ssl_ct_util.c | 2 +- modules/ssl/ssl_engine_init.c | 6 +++--- server/mpm/event/event.c | 2 +- server/mpm/motorz/motorz.c | 2 +- server/mpm/simple/simple_run.c | 2 +- 32 files changed, 75 insertions(+), 70 deletions(-) create mode 100644 changes-entries/ap-array-len.txt diff --git a/changes-entries/ap-array-len.txt b/changes-entries/ap-array-len.txt new file mode 100644 index 00000000000..1e75b7dbe43 --- /dev/null +++ b/changes-entries/ap-array-len.txt @@ -0,0 +1,2 @@ + *) core: Add AP_ARRAY_LEN() macro for determining the number of + elements in a fixed-size array. [Arturo Bernal] diff --git a/include/ap_config.h b/include/ap_config.h index 41f0caf174b..d6d4ffdbe93 100644 --- a/include/ap_config.h +++ b/include/ap_config.h @@ -261,5 +261,11 @@ #define AP_FN_ATTR_NONNULL(x) #endif +/** + * Return the number of elements in an array. + * + * @note The argument must be an array, not a pointer. + */ +#define AP_ARRAY_LEN(a) (sizeof(a) / sizeof((a)[0])) #endif /* AP_CONFIG_H */ diff --git a/include/ap_mmn.h b/include/ap_mmn.h index b69d07b74af..9c78ed5da43 100644 --- a/include/ap_mmn.h +++ b/include/ap_mmn.h @@ -739,6 +739,7 @@ * 20211221.31 (2.5.1-dev) Add ap_*_timingsafe() to httpd.h * 20211221.32 (2.5.1-dev) Add ap_cstr_stripws() to httpd.h * 20211221.33 (2.5.1-dev) Add ap_size_parameter_parse() to httpd.h + * 20211221.34 (2.5.1-dev) Add AP_ARRAY_LEN() */ #define MODULE_MAGIC_COOKIE 0x41503235UL /* "AP25" */ @@ -746,7 +747,7 @@ #ifndef MODULE_MAGIC_NUMBER_MAJOR #define MODULE_MAGIC_NUMBER_MAJOR 20211221 #endif -#define MODULE_MAGIC_NUMBER_MINOR 33 /* 0...n */ +#define MODULE_MAGIC_NUMBER_MINOR 34 /* 0...n */ /** * Determine if the server's current MODULE_MAGIC_NUMBER is at least a diff --git a/modules/filters/sed1.c b/modules/filters/sed1.c index f693a2a459d..b134fe3634f 100644 --- a/modules/filters/sed1.c +++ b/modules/filters/sed1.c @@ -23,6 +23,7 @@ #include "libsed.h" #include "sed.h" #include "apr_strings.h" +#include "ap_config.h" #include "regexp.h" static const char *const trans[040] = { @@ -332,7 +333,7 @@ apr_status_t sed_reset_eval(sed_eval_t *eval, sed_commands_t *commands, sed_err_ eval->hspend = eval->holdbuf; eval->lcomend = &eval->genbuf[71]; - for (i = 0; i < sizeof(eval->abuf) / sizeof(eval->abuf[0]); i++) + for (i = 0; i < AP_ARRAY_LEN(eval->abuf); i++) eval->abuf[i] = NULL; eval->aptr = eval->abuf; eval->pending = NULL; diff --git a/modules/http2/h2.h b/modules/http2/h2.h index f496a6dcb2b..4e130b255c6 100644 --- a/modules/http2/h2.h +++ b/modules/http2/h2.h @@ -95,8 +95,6 @@ extern const char *H2_MAGIC_TOKEN; #define H2_STREAM_CLIENT_INITIATED(id) (id&0x01) -#define H2_ALEN(a) (sizeof(a)/sizeof((a)[0])) - #define H2MAX(x,y) ((x) > (y) ? (x) : (y)) #define H2MIN(x,y) ((x) < (y) ? (x) : (y)) diff --git a/modules/http2/h2_bucket_beam.c b/modules/http2/h2_bucket_beam.c index 69782541aa4..0f34b448e32 100644 --- a/modules/http2/h2_bucket_beam.c +++ b/modules/http2/h2_bucket_beam.c @@ -74,7 +74,7 @@ static int h2_blist_count(h2_blist *blist) do { \ if (APLOG_C_IS_LEVEL((c),(level))) { \ char buffer[4 * 1024]; \ - apr_size_t len, bmax = sizeof(buffer)/sizeof(buffer[0]); \ + apr_size_t len, bmax = AP_ARRAY_LEN(buffer); \ len = bb? h2_util_bb_print(buffer, bmax, "", "", bb) : 0; \ ap_log_cerror(APLOG_MARK, (level), rv, (c), \ "BEAM[%s,%s%sdata=%ld,buckets(send/consumed)=%d/%d]: %s %s", \ diff --git a/modules/http2/h2_c1_io.c b/modules/http2/h2_c1_io.c index 7ac1c8d6aaa..0fa232c61bc 100644 --- a/modules/http2/h2_c1_io.c +++ b/modules/http2/h2_c1_io.c @@ -63,7 +63,7 @@ static void h2_c1_io_bb_log(conn_rec *c, int stream_id, int level, { char buffer[16 * 1024]; const char *line = "(null)"; - int bmax = sizeof(buffer)/sizeof(buffer[0]); + int bmax = AP_ARRAY_LEN(buffer); int off = 0; apr_bucket *b; diff --git a/modules/http2/h2_c2_filter.c b/modules/http2/h2_c2_filter.c index 17a2c131d8c..5e5df5dadaf 100644 --- a/modules/http2/h2_c2_filter.c +++ b/modules/http2/h2_c2_filter.c @@ -154,7 +154,7 @@ apr_status_t h2_c2_filter_request_in(ap_filter_t *f, do { \ if (APLOG_C_IS_LEVEL((c),(level))) { \ char buffer[4 * 1024]; \ - apr_size_t len, bmax = sizeof(buffer)/sizeof(buffer[0]); \ + apr_size_t len, bmax = AP_ARRAY_LEN(buffer); \ len = h2_util_bb_print(buffer, bmax, "", "", (bb)); \ ap_log_cerror(APLOG_MARK, (level), rv, (c), \ "FILTER[%s]: %s %s", \ @@ -800,7 +800,7 @@ static void make_chunk(conn_rec *c, h2_chunk_filter_t *fctx, apr_bucket_brigade apr_bucket *b; apr_size_t len; - len = (apr_size_t)apr_snprintf(buffer, H2_ALEN(buffer), + len = (apr_size_t)apr_snprintf(buffer, AP_ARRAY_LEN(buffer), "%"APR_UINT64_T_HEX_FMT"\r\n", (apr_uint64_t)chunk_len); b = apr_bucket_heap_create(buffer, len, NULL, bb->bucket_alloc); APR_BUCKET_INSERT_BEFORE(first, b); diff --git a/modules/http2/h2_protocol.c b/modules/http2/h2_protocol.c index 84a17ef8948..a5a7601aec2 100644 --- a/modules/http2/h2_protocol.c +++ b/modules/http2/h2_protocol.c @@ -77,7 +77,7 @@ static const char *const h2_err_descr[] = { const char *h2_protocol_err_description(unsigned int h2_error) { - if (h2_error < (sizeof(h2_err_descr)/sizeof(h2_err_descr[0]))) { + if (h2_error < AP_ARRAY_LEN(h2_err_descr)) { return h2_err_descr[h2_error]; } return "unknown http/2 error code"; @@ -395,7 +395,7 @@ static const char *const RFC7540_names[] = { "SSL3_CK_SCSV", /* TLS_EMPTY_RENEGOTIATION_INFO_SCSV */ "SSL3_CK_FALLBACK_SCSV" }; -static size_t RFC7540_names_LEN = sizeof(RFC7540_names)/sizeof(RFC7540_names[0]); +static size_t RFC7540_names_LEN = AP_ARRAY_LEN(RFC7540_names); static apr_hash_t *BLCNames; diff --git a/modules/http2/h2_proxy_session.c b/modules/http2/h2_proxy_session.c index f5f0f0d63ae..7e94a3852cc 100644 --- a/modules/http2/h2_proxy_session.c +++ b/modules/http2/h2_proxy_session.c @@ -266,7 +266,7 @@ static int on_frame_recv(nghttp2_session *ngh2, const nghttp2_frame *frame, if (APLOGcdebug(session->c)) { char buffer[256]; - h2_proxy_util_frame_print(frame, buffer, sizeof(buffer)/sizeof(buffer[0])); + h2_proxy_util_frame_print(frame, buffer, AP_ARRAY_LEN(buffer)); ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, session->c, APLOGNO(03341) "h2_proxy_session(%s): recv FRAME[%s]", session->id, buffer); @@ -355,7 +355,7 @@ static int before_frame_send(nghttp2_session *ngh2, if (APLOGcdebug(session->c)) { char buffer[256]; - h2_proxy_util_frame_print(frame, buffer, sizeof(buffer)/sizeof(buffer[0])); + h2_proxy_util_frame_print(frame, buffer, AP_ARRAY_LEN(buffer)); ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, session->c, APLOGNO(03343) "h2_proxy_session(%s): sent FRAME[%s]", session->id, buffer); @@ -813,7 +813,7 @@ static apr_status_t session_start(h2_proxy_session *session) settings[1].value = (1 << session->window_bits_stream) - 1; rv = nghttp2_submit_settings(session->ngh2, NGHTTP2_FLAG_NONE, settings, - H2_ALEN(settings)); + AP_ARRAY_LEN(settings)); /* If the connection window is larger than our default, trigger a WINDOW_UPDATE */ add_conn_window = ((1 << session->window_bits_connection) - 1 - @@ -1202,7 +1202,7 @@ static const char *const StateNames[] = { static const char *state_name(h2_proxys_state state) { - if (state >= (sizeof(StateNames)/sizeof(StateNames[0]))) { + if (state >= AP_ARRAY_LEN(StateNames)) { return "unknown"; } return StateNames[state]; diff --git a/modules/http2/h2_proxy_session.h b/modules/http2/h2_proxy_session.h index 3bc16d70b3e..cdda931bc0b 100644 --- a/modules/http2/h2_proxy_session.h +++ b/modules/http2/h2_proxy_session.h @@ -17,8 +17,6 @@ #ifndef h2_proxy_session_h #define h2_proxy_session_h -#define H2_ALEN(a) (sizeof(a)/sizeof((a)[0])) - #include struct h2_proxy_iqueue; diff --git a/modules/http2/h2_proxy_util.c b/modules/http2/h2_proxy_util.c index b7a4c509107..b9816a2e7dc 100644 --- a/modules/http2/h2_proxy_util.c +++ b/modules/http2/h2_proxy_util.c @@ -478,7 +478,7 @@ typedef struct { } literal; #define H2_DEF_LITERAL(n) { (n), (sizeof(n)-1) } -#define H2_LIT_ARGS(a) (a),H2_ALEN(a) +#define H2_LIT_ARGS(a) (a),AP_ARRAY_LEN(a) static literal IgnoredRequestHeaders[] = { H2_DEF_LITERAL("upgrade"), @@ -653,7 +653,7 @@ apr_status_t h2_proxy_req_make(h2_proxy_request *req, apr_pool_t *pool, int h2_proxy_util_frame_print(const nghttp2_frame *frame, char *buffer, size_t maxlen) { char scratch[128]; - size_t s_len = sizeof(scratch)/sizeof(scratch[0]); + size_t s_len = AP_ARRAY_LEN(scratch); switch (frame->hd.type) { case NGHTTP2_DATA: { diff --git a/modules/http2/h2_session.c b/modules/http2/h2_session.c index 5f32454363b..702c5d6e553 100644 --- a/modules/http2/h2_session.c +++ b/modules/http2/h2_session.c @@ -221,7 +221,7 @@ static int on_invalid_frame_recv_cb(nghttp2_session *ngh2, if (APLOGcdebug(session->c1)) { char buffer[256]; - h2_util_frame_print(frame, buffer, sizeof(buffer)/sizeof(buffer[0])); + h2_util_frame_print(frame, buffer, AP_ARRAY_LEN(buffer)); ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, session->c1, H2_SSSN_LOG(APLOGNO(03063), session, "recv invalid FRAME[%s], frames=%ld/%ld (r/s)"), @@ -355,7 +355,7 @@ static int on_frame_recv_cb(nghttp2_session *ng2s, if (APLOGcdebug(session->c1)) { char buffer[256]; - h2_util_frame_print(frame, buffer, sizeof(buffer)/sizeof(buffer[0])); + h2_util_frame_print(frame, buffer, AP_ARRAY_LEN(buffer)); if (stream) { ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, session->c1, H2_STRM_LOG(APLOGNO(10302), stream, @@ -453,7 +453,7 @@ static int on_frame_recv_cb(nghttp2_session *ng2s, char buffer[256]; h2_util_frame_print(frame, buffer, - sizeof(buffer)/sizeof(buffer[0])); + AP_ARRAY_LEN(buffer)); ap_log_cerror(APLOG_MARK, APLOG_TRACE2, 0, session->c1, H2_SSSN_MSG(session, "on_frame_rcv %s"), buffer); } @@ -610,7 +610,7 @@ static int on_frame_send_cb(nghttp2_session *ngh2, if (APLOGcdebug(session->c1)) { char buffer[256]; - h2_util_frame_print(frame, buffer, sizeof(buffer)/sizeof(buffer[0])); + h2_util_frame_print(frame, buffer, AP_ARRAY_LEN(buffer)); if (stream) { ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, session->c1, H2_STRM_LOG(APLOGNO(10303), stream, @@ -645,7 +645,7 @@ static int on_frame_not_send_cb(nghttp2_session *ngh2, char buffer[256]; stream = get_stream(session, stream_id); - h2_util_frame_print(frame, buffer, sizeof(buffer)/sizeof(buffer[0])); + h2_util_frame_print(frame, buffer, AP_ARRAY_LEN(buffer)); if (!stream || !stream->rst_error) ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, session->c1, H2_SSSN_LOG(APLOGNO(10509), session, @@ -1430,7 +1430,7 @@ static const char *const StateNames[] = { const char *h2_session_state_str(h2_session_state state) { - if (state >= (sizeof(StateNames)/sizeof(StateNames[0]))) { + if (state >= AP_ARRAY_LEN(StateNames)) { return "unknown"; } return StateNames[state]; diff --git a/modules/http2/h2_stream.c b/modules/http2/h2_stream.c index 4e5ba62be8d..87c2001c781 100644 --- a/modules/http2/h2_stream.c +++ b/modules/http2/h2_stream.c @@ -156,12 +156,12 @@ static int on_frame(h2_stream_state_t state, int frame_type, static int on_frame_send(h2_stream_state_t state, int frame_type) { - return on_frame(state, frame_type, trans_on_send, H2_ALEN(trans_on_send)); + return on_frame(state, frame_type, trans_on_send, AP_ARRAY_LEN(trans_on_send)); } static int on_frame_recv(h2_stream_state_t state, int frame_type) { - return on_frame(state, frame_type, trans_on_recv, H2_ALEN(trans_on_recv)); + return on_frame(state, frame_type, trans_on_recv, AP_ARRAY_LEN(trans_on_recv)); } static int on_event(h2_stream* stream, h2_stream_event_t ev) @@ -170,7 +170,7 @@ static int on_event(h2_stream* stream, h2_stream_event_t ev) if (stream->monitor && stream->monitor->on_event) { stream->monitor->on_event(stream->monitor->ctx, stream, ev); } - if (ev < H2_ALEN(trans_on_event)) { + if (ev < AP_ARRAY_LEN(trans_on_event)) { return on_map(stream->state, trans_on_event[ev]); } return stream->state; @@ -189,7 +189,7 @@ static void H2_STREAM_OUT_LOG(int lvl, h2_stream *s, const char *tag) if (APLOG_C_IS_LEVEL(s->session->c1, lvl)) { conn_rec *c = s->session->c1; char buffer[4 * 1024]; - apr_size_t len, bmax = sizeof(buffer)/sizeof(buffer[0]); + apr_size_t len, bmax = AP_ARRAY_LEN(buffer); len = h2_util_bb_print(buffer, bmax, tag, "", s->out_buffer); ap_log_cerror(APLOG_MARK, lvl, 0, c, diff --git a/modules/http2/h2_util.c b/modules/http2/h2_util.c index b3039451f39..f396653ba50 100644 --- a/modules/http2/h2_util.c +++ b/modules/http2/h2_util.c @@ -1530,7 +1530,7 @@ apr_status_t h2_res_create_ngheader(h2_ngheader **ph, apr_pool_t *p, apr_psprintf(p, "%d", response->status) }; return ngheader_create(ph, p, is_unsafe(response), - H2_ALEN(keys), keys, values, response->headers); + AP_ARRAY_LEN(keys), keys, values, response->headers); } #else /* AP_HAS_RESPONSE_BUCKETS */ @@ -1558,7 +1558,7 @@ apr_status_t h2_res_create_ngheader(h2_ngheader **ph, apr_pool_t *p, apr_psprintf(p, "%d", headers->status) }; return ngheader_create(ph, p, is_unsafe(headers), - H2_ALEN(keys), keys, values, headers->headers); + AP_ARRAY_LEN(keys), keys, values, headers->headers); } #endif /* else AP_HAS_RESPONSE_BUCKETS */ @@ -1585,7 +1585,7 @@ apr_status_t h2_req_create_ngheader(h2_ngheader **ph, apr_pool_t *p, ap_assert(req->path); ap_assert(req->method); - return ngheader_create(ph, p, 0, H2_ALEN(keys), keys, values, req->headers); + return ngheader_create(ph, p, 0, AP_ARRAY_LEN(keys), keys, values, req->headers); } /******************************************************************************* @@ -1599,7 +1599,7 @@ typedef struct { } literal; #define H2_DEF_LITERAL(n) { (n), (sizeof(n)-1) } -#define H2_LIT_ARGS(a) (a),H2_ALEN(a) +#define H2_LIT_ARGS(a) (a),AP_ARRAY_LEN(a) static literal IgnoredRequestHeaders[] = { H2_DEF_LITERAL("upgrade"), @@ -1784,7 +1784,7 @@ apr_status_t h2_req_add_header(apr_table_t *headers, apr_pool_t *pool, int h2_util_frame_print(const nghttp2_frame *frame, char *buffer, size_t maxlen) { char scratch[128]; - size_t s_len = sizeof(scratch)/sizeof(scratch[0]); + size_t s_len = AP_ARRAY_LEN(scratch); switch (frame->hd.type) { case NGHTTP2_DATA: { diff --git a/modules/http2/h2_util.h b/modules/http2/h2_util.h index e813bf586f9..ee1da4f6bd6 100644 --- a/modules/http2/h2_util.h +++ b/modules/http2/h2_util.h @@ -465,7 +465,7 @@ if (APLOG_C_IS_LEVEL(c, level)) { \ do { \ char buffer[4 * 1024]; \ const char *line = "(null)"; \ - apr_size_t len, bmax = sizeof(buffer)/sizeof(buffer[0]); \ + apr_size_t len, bmax = AP_ARRAY_LEN(buffer); \ len = h2_util_bb_print(buffer, bmax, (tag), "", (bb)); \ ap_log_cerror(APLOG_MARK, level, 0, (c), "bb_dump(%ld): %s", \ ((c)->master? (c)->master->id : (c)->id), (len? buffer : line)); \ diff --git a/modules/http2/mod_http2.c b/modules/http2/mod_http2.c index 1bd34b207dd..956aa68bd08 100644 --- a/modules/http2/mod_http2.c +++ b/modules/http2/mod_http2.c @@ -306,11 +306,6 @@ static h2_var_def H2_VARS[] = { { "H2_STREAM_TAG", val_H2_STREAM_TAG, 1 }, }; -#ifndef H2_ALEN -#define H2_ALEN(a) (sizeof(a)/sizeof((a)[0])) -#endif - - static int http2_is_h2(conn_rec *c) { return h2_conn_ctx_get(c->master? c->master : c) != NULL; @@ -321,7 +316,7 @@ static char *http2_var_lookup(apr_pool_t *p, server_rec *s, { unsigned int i; /* If the # of vars grow, we need to put definitions in a hash */ - for (i = 0; i < H2_ALEN(H2_VARS); ++i) { + for (i = 0; i < AP_ARRAY_LEN(H2_VARS); ++i) { h2_var_def *vdef = &H2_VARS[i]; if (!strcmp(vdef->name, name)) { h2_conn_ctx_t *ctx = (r? h2_conn_ctx_get(c) : @@ -338,7 +333,7 @@ static int h2_h2_fixups(request_rec *r) h2_conn_ctx_t *ctx = h2_conn_ctx_get(r->connection); unsigned int i; - for (i = 0; ctx && i < H2_ALEN(H2_VARS); ++i) { + for (i = 0; ctx && i < AP_ARRAY_LEN(H2_VARS); ++i) { h2_var_def *vdef = &H2_VARS[i]; if (vdef->subprocess) { apr_table_setn(r->subprocess_env, vdef->name, diff --git a/modules/md/md_acme.c b/modules/md/md_acme.c index 60940f04f08..a6a7c49eda9 100644 --- a/modules/md/md_acme.c +++ b/modules/md/md_acme.c @@ -22,6 +22,7 @@ #include #include #include +#include #include @@ -82,7 +83,7 @@ static apr_status_t problem_status_get(const char *type) { type += strlen("urn:"); } - for(i = 0; i < (sizeof(Problems)/sizeof(Problems[0])); ++i) { + for(i = 0; i < AP_ARRAY_LEN(Problems); ++i) { if (!apr_cstr_casecmp(type, Problems[i].type)) { return Problems[i].rv; } @@ -101,7 +102,7 @@ int md_acme_problem_is_input_related(const char *problem) { problem += strlen("urn:"); } - for(i = 0; i < (sizeof(Problems)/sizeof(Problems[0])); ++i) { + for(i = 0; i < AP_ARRAY_LEN(Problems); ++i) { if (!apr_cstr_casecmp(problem, Problems[i].type)) { return Problems[i].input_related; } diff --git a/modules/md/md_acme_authz.c b/modules/md/md_acme_authz.c index b155c0a3d6b..5c4528fe5db 100644 --- a/modules/md/md_acme_authz.c +++ b/modules/md/md_acme_authz.c @@ -25,6 +25,7 @@ #include #include #include +#include #include "md.h" #include "md_crypt.h" @@ -579,7 +580,7 @@ static const cha_type CHA_TYPES[] = { { MD_AUTHZ_TYPE_TLSALPN01, cha_tls_alpn_01_setup, cha_teardown_dir }, { MD_AUTHZ_TYPE_DNS01, cha_dns_01_setup, cha_dns_01_teardown }, }; -static const apr_size_t CHA_TYPES_LEN = (sizeof(CHA_TYPES)/sizeof(CHA_TYPES[0])); +static const apr_size_t CHA_TYPES_LEN = AP_ARRAY_LEN(CHA_TYPES); typedef struct { apr_pool_t *p; diff --git a/modules/md/md_core.c b/modules/md/md_core.c index 704212365e9..fb7d6f6261a 100644 --- a/modules/md/md_core.c +++ b/modules/md/md_core.c @@ -23,6 +23,7 @@ #include #include #include +#include #include "md_json.h" #include "md.h" @@ -438,7 +439,7 @@ const char *md_get_ca_name_from_url(apr_pool_t *p, const char *url) apr_uri_t uri_parsed; unsigned int i; - for (i = 0; i < sizeof(KNOWN_CAs)/sizeof(KNOWN_CAs[0]); ++i) { + for (i = 0; i < AP_ARRAY_LEN(KNOWN_CAs); ++i) { if (!apr_cstr_casecmp(KNOWN_CAs[i].url, url)) { return KNOWN_CAs[i].name; } @@ -456,7 +457,7 @@ apr_status_t md_get_ca_url_from_name(const char **purl, apr_pool_t *p, const cha apr_status_t rv = APR_SUCCESS; *purl = NULL; - for (i = 0; i < sizeof(KNOWN_CAs)/sizeof(KNOWN_CAs[0]); ++i) { + for (i = 0; i < AP_ARRAY_LEN(KNOWN_CAs); ++i) { if (!apr_cstr_casecmp(KNOWN_CAs[i].name, name)) { *purl = KNOWN_CAs[i].url; goto leave; @@ -468,7 +469,7 @@ apr_status_t md_get_ca_url_from_name(const char **purl, apr_pool_t *p, const cha apr_array_header_t *names; names = apr_array_make(p, 10, sizeof(const char*)); - for (i = 0; i < sizeof(KNOWN_CAs)/sizeof(KNOWN_CAs[0]); ++i) { + for (i = 0; i < AP_ARRAY_LEN(KNOWN_CAs); ++i) { APR_ARRAY_PUSH(names, const char *) = KNOWN_CAs[i].name; } *purl = apr_psprintf(p, diff --git a/modules/md/md_store.c b/modules/md/md_store.c index 381bbbc4a2f..3f25ca62be5 100644 --- a/modules/md/md_store.c +++ b/modules/md/md_store.c @@ -25,6 +25,7 @@ #include #include #include +#include #include "md.h" #include "md_crypt.h" @@ -61,7 +62,7 @@ static const char *const GROUP_NAME[] = { const char *md_store_group_name(unsigned int group) { - if (group < sizeof(GROUP_NAME)/sizeof(GROUP_NAME[0])) { + if (group < AP_ARRAY_LEN(GROUP_NAME)) { return GROUP_NAME[group]; } return "UNKNOWN"; diff --git a/modules/md/md_store_fs.c b/modules/md/md_store_fs.c index 93bd7def1e4..e33de5b1a15 100644 --- a/modules/md/md_store_fs.c +++ b/modules/md/md_store_fs.c @@ -25,6 +25,7 @@ #include #include #include +#include #include "md.h" #include "md_crypt.h" @@ -375,7 +376,7 @@ apr_status_t md_store_fs_group_perms_set(md_store_t *store, md_store_group_t gro { md_store_fs_t *s_fs = FS_STORE(store); - if (group >= (sizeof(s_fs->group_perms)/sizeof(s_fs->group_perms[0]))) { + if (group >= AP_ARRAY_LEN(s_fs->group_perms)) { return APR_ENOTIMPL; } s_fs->group_perms[group].file = file_perms; @@ -394,7 +395,7 @@ apr_status_t md_store_fs_set_event_cb(struct md_store_t *store, md_store_fs_cb * static const perms_t *gperms(md_store_fs_t *s_fs, md_store_group_t group) { - if (group >= (sizeof(s_fs->group_perms)/sizeof(s_fs->group_perms[0])) + if (group >= AP_ARRAY_LEN(s_fs->group_perms) || !s_fs->group_perms[group].dir) { return &s_fs->def_perms; } diff --git a/modules/md/md_util.c b/modules/md/md_util.c index f55d83fb676..a0bc8c0d890 100644 --- a/modules/md/md_util.c +++ b/modules/md/md_util.c @@ -28,6 +28,7 @@ #if APR_VERSION_AT_LEAST(1,7,0) #include +#include #endif #if APR_HAVE_STDLIB_H @@ -489,7 +490,7 @@ apr_status_t md_text_fread8k(const char **ptext, apr_pool_t *p, const char *fpat *ptext = NULL; if (APR_SUCCESS == (rv = apr_file_open(&f, fpath, APR_FOPEN_READ, 0, p))) { - apr_size_t blen = sizeof(buffer)/sizeof(buffer[0]) - 1; + apr_size_t blen = AP_ARRAY_LEN(buffer) - 1; rv = apr_file_read_full(f, buffer, blen, &blen); if (APR_SUCCESS == rv || APR_STATUS_IS_EOF(rv)) { *ptext = apr_pstrndup(p, buffer, blen); diff --git a/modules/md/mod_md.c b/modules/md/mod_md.c index b7b61c6f4d5..d95fa3e6e40 100644 --- a/modules/md/mod_md.c +++ b/modules/md/mod_md.c @@ -159,7 +159,7 @@ static apr_status_t notify(md_job_t *job, const char *reason, int i; log_msg_reason = apr_psprintf(p, "message-%s", reason); - for (i = 0; i < (int)(sizeof(notify_rates)/sizeof(notify_rates[0])); ++i) { + for (i = 0; i < (int)AP_ARRAY_LEN(notify_rates); ++i) { if (!strcmp(reason, notify_rates[i].reason)) { min_interim = notify_rates[i].min_interim; } diff --git a/modules/md/mod_md_status.c b/modules/md/mod_md_status.c index 2f3904cec59..b06d3a19f26 100644 --- a/modules/md/mod_md_status.c +++ b/modules/md/mod_md_status.c @@ -717,14 +717,14 @@ static int add_md_row(void *baton, apr_size_t index, md_json_t *mdj) if (HTML_STATUS(ctx)) { apr_brigade_printf(ctx->bb, NULL, NULL, "", (index % 2)? "odd" : "even"); - for (i = 0; i < (int)(sizeof(status_infos)/sizeof(status_infos[0])); ++i) { + for (i = 0; i < (int)AP_ARRAY_LEN(status_infos); ++i) { apr_brigade_puts(ctx->bb, NULL, NULL, ""); add_status_cell(ctx, mdj, &status_infos[i]); apr_brigade_puts(ctx->bb, NULL, NULL, ""); } apr_brigade_puts(ctx->bb, NULL, NULL, ""); } else { - for (i = 0; i < (int)(sizeof(status_infos)/sizeof(status_infos[0])); ++i) { + for (i = 0; i < (int)AP_ARRAY_LEN(status_infos); ++i) { ctx->prefix = apr_pstrcat(ctx->p, prefix, apr_psprintf(ctx->p, "[%" APR_SIZE_T_FMT "]", index), NULL); add_status_cell(ctx, mdj, &status_infos[i]); ctx->prefix = prefix; @@ -788,7 +788,7 @@ int md_domains_status_hook(request_rec *r, int flags) ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r, "html managed domain status table"); apr_brigade_puts(ctx.bb, NULL, NULL, "
\n

Managed Certificates

\n\n"); - for (i = 0; i < (int)(sizeof(status_infos)/sizeof(status_infos[0])); ++i) { + for (i = 0; i < (int)AP_ARRAY_LEN(status_infos); ++i) { si_add_header(&ctx, &status_infos[i]); } apr_brigade_puts(ctx.bb, NULL, NULL, "\n"); @@ -844,14 +844,14 @@ static int add_ocsp_row(void *baton, apr_size_t index, md_json_t *mdj) if (HTML_STATUS(ctx)) { apr_brigade_printf(ctx->bb, NULL, NULL, "", (index % 2)? "odd" : "even"); - for (i = 0; i < (int)(sizeof(ocsp_status_infos)/sizeof(ocsp_status_infos[0])); ++i) { + for (i = 0; i < (int)AP_ARRAY_LEN(ocsp_status_infos); ++i) { apr_brigade_puts(ctx->bb, NULL, NULL, ""); } apr_brigade_puts(ctx->bb, NULL, NULL, ""); } else { - for (i = 0; i < (int)(sizeof(ocsp_status_infos)/sizeof(ocsp_status_infos[0])); ++i) { + for (i = 0; i < (int)AP_ARRAY_LEN(ocsp_status_infos); ++i) { ctx->prefix = apr_pstrcat(ctx->p, prefix, apr_psprintf(ctx->p, "[%" APR_SIZE_T_FMT "]", index), NULL); add_status_cell(ctx, mdj, &ocsp_status_infos[i]); ctx->prefix = prefix; @@ -904,7 +904,7 @@ int md_ocsp_status_hook(request_rec *r, int flags) ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r, "html ocsp stapling status table"); apr_brigade_puts(ctx.bb, NULL, NULL, "
\n

Managed Staplings

\n
"); add_status_cell(ctx, mdj, &ocsp_status_infos[i]); apr_brigade_puts(ctx->bb, NULL, NULL, "
\n"); - for (i = 0; i < (int)(sizeof(ocsp_status_infos)/sizeof(ocsp_status_infos[0])); ++i) { + for (i = 0; i < (int)AP_ARRAY_LEN(ocsp_status_infos); ++i) { si_add_header(&ctx, &ocsp_status_infos[i]); } apr_brigade_puts(ctx.bb, NULL, NULL, "\n"); diff --git a/modules/metadata/mod_mime_magic.c b/modules/metadata/mod_mime_magic.c index 2a5150f9083..28e8f653a67 100644 --- a/modules/metadata/mod_mime_magic.c +++ b/modules/metadata/mod_mime_magic.c @@ -2112,22 +2112,20 @@ static const struct { }, }; -#define ncompr (sizeof(compr) / sizeof(compr[0])) - static int zmagic(request_rec *r, unsigned char *buf, apr_size_t nbytes) { unsigned char *newbuf; int newsize; int i; - for (i = 0; i < ncompr; i++) { + for (i = 0; i < AP_ARRAY_LEN(compr); i++) { if (nbytes < compr[i].maglen) continue; if (memcmp(buf, compr[i].magic, compr[i].maglen) == 0) break; } - if (i == ncompr) + if (i == AP_ARRAY_LEN(compr)) return 0; if ((newsize = uncompress(r, i, &newbuf, HOWMANY)) > 0) { diff --git a/modules/ssl/mod_ssl_ct.c b/modules/ssl/mod_ssl_ct.c index 06c1aeecffb..72bdd106038 100644 --- a/modules/ssl/mod_ssl_ct.c +++ b/modules/ssl/mod_ssl_ct.c @@ -512,7 +512,7 @@ static apr_status_t submission(server_rec *s, apr_pool_t *p, const char *ct_exe, args[i++] = apr_pstrcat(p, "--ct_server_public_key=", log_cfg->public_key_pem, NULL); args[i++] = "upload"; args[i++] = NULL; - ap_assert(i == sizeof args / sizeof args[0]); + ap_assert(i == AP_ARRAY_LEN(args)); rv = ctutil_run_to_log(p, s, args, "log client"); diff --git a/modules/ssl/ssl_ct_util.c b/modules/ssl/ssl_ct_util.c index b3c16dede4b..c0532983955 100644 --- a/modules/ssl/ssl_ct_util.c +++ b/modules/ssl/ssl_ct_util.c @@ -294,7 +294,7 @@ static void io_loop(apr_pool_t *p, server_rec *s, apr_proc_t *proc, int i; int read = 0; - for (i = 0; i < sizeof fds / sizeof fds[0]; i++) { + for (i = 0; i < AP_ARRAY_LEN(fds); i++) { if (!fds[i]) { continue; } diff --git a/modules/ssl/ssl_engine_init.c b/modules/ssl/ssl_engine_init.c index 3f15ca8eaaf..e516892e0a7 100644 --- a/modules/ssl/ssl_engine_init.c +++ b/modules/ssl/ssl_engine_init.c @@ -136,7 +136,7 @@ static void init_dh_params(void) { unsigned n; - for (n = 0; n < sizeof(dhparams)/sizeof(dhparams[0]); n++) + for (n = 0; n < AP_ARRAY_LEN(dhparams); n++) dhparams[n].dh = make_dh_params(dhparams[n].prime); } @@ -147,7 +147,7 @@ static void free_dh_params(void) /* DH_free() is a noop for a NULL parameter, so these are harmless * in the (unexpected) case where these variables are already * NULL. */ - for (n = 0; n < sizeof(dhparams)/sizeof(dhparams[0]); n++) { + for (n = 0; n < AP_ARRAY_LEN(dhparams); n++) { DH_free(dhparams[n].dh); dhparams[n].dh = NULL; } @@ -164,7 +164,7 @@ DH *modssl_get_dh_params(unsigned keylen) { unsigned n; - for (n = 0; n < sizeof(dhparams)/sizeof(dhparams[0]); n++) + for (n = 0; n < AP_ARRAY_LEN(dhparams); n++) if (keylen >= dhparams[n].min) return dhparams[n].dh; diff --git a/server/mpm/event/event.c b/server/mpm/event/event.c index 385532d03b4..70ebc25cf7b 100644 --- a/server/mpm/event/event.c +++ b/server/mpm/event/event.c @@ -2701,7 +2701,7 @@ static void setup_threads_runtime(void) */ pollset_flags = APR_POLLSET_THREADSAFE | APR_POLLSET_NOCOPY | APR_POLLSET_WAKEABLE | APR_POLLSET_NODEFAULT; - for (i = 0; i < sizeof(good_methods) / sizeof(good_methods[0]); i++) { + for (i = 0; i < AP_ARRAY_LEN(good_methods); i++) { rv = apr_pollset_create_ex(&event_pollset, pollset_size + 1, pruntime, pollset_flags, good_methods[i]); if (rv == APR_SUCCESS) { diff --git a/server/mpm/motorz/motorz.c b/server/mpm/motorz/motorz.c index 292fcdc98e3..533a476d639 100644 --- a/server/mpm/motorz/motorz.c +++ b/server/mpm/motorz/motorz.c @@ -1302,7 +1302,7 @@ static int motorz_setup_pollset(motorz_poller_t *poller) * it MUST be thread-safe. All the preferred backends below * (kqueue/port/epoll) support APR_POLLSET_THREADSAFE. */ - for (i = 0; i < sizeof(good_methods) / sizeof(good_methods[0]); i++) { + for (i = 0; i < AP_ARRAY_LEN(good_methods); i++) { rv = apr_pollset_create_ex(&poller->pollset, 512, poller->pool, diff --git a/server/mpm/simple/simple_run.c b/server/mpm/simple/simple_run.c index fe68d269f12..d73fcbb30db 100644 --- a/server/mpm/simple/simple_run.c +++ b/server/mpm/simple/simple_run.c @@ -260,7 +260,7 @@ static int simple_setup_pollcb(simple_core_t * sc) apr_status_t rv; int good_methods[] = {APR_POLLSET_KQUEUE, APR_POLLSET_PORT, APR_POLLSET_EPOLL}; - for (i = 0; i < sizeof(good_methods) / sizeof(good_methods[0]); i++) { + for (i = 0; i < AP_ARRAY_LEN(good_methods); i++) { /* pqXXXXX: make size of pollcb configrable or dynamic */ rv = apr_pollcb_create_ex(&sc->pollcb, 512, sc->pool, APR_POLLSET_NODEFAULT, good_methods[i]);