From d0b97e3ccee9d5e075e2bbc43e9e88c9c1616400 Mon Sep 17 00:00:00 2001 From: Thomas Meyer Date: Thu, 4 Jan 2024 19:31:04 +0100 Subject: [PATCH] ARRAY_LEN: Introduce small helper macro --- include/util_misc.h | 40 +++++++++++++++++++++++++++++++ modules/filters/sed1.c | 3 ++- modules/http2/h2.h | 2 -- modules/http2/h2_bucket_beam.c | 4 +++- modules/http2/h2_c1_io.c | 4 +++- modules/http2/h2_c2_filter.c | 4 ++-- modules/http2/h2_protocol.c | 7 +++--- 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 | 10 ++++---- modules/http2/h2_util.c | 12 ++++++---- modules/http2/h2_util.h | 2 +- modules/http2/mod_http2.c | 11 ++++----- modules/md/md_acme.c | 6 +++-- modules/md/md_acme_authz.c | 4 +++- modules/md/md_core.c | 8 ++++--- modules/md/md_store.c | 4 +++- modules/md/md_store_fs.c | 6 +++-- modules/md/md_util.c | 4 +++- modules/md/mod_md.c | 4 +++- modules/md/mod_md_status.c | 14 ++++++----- modules/metadata/mod_mime_magic.c | 4 +--- modules/ssl/ssl_engine_init.c | 6 ++--- modules/tls/tls_util.h | 5 +--- server/mpm/event/event.c | 5 ++-- server/mpm/motorz/motorz.c | 2 +- server/mpm/simple/simple_run.c | 2 +- 29 files changed, 128 insertions(+), 71 deletions(-) create mode 100644 include/util_misc.h diff --git a/include/util_misc.h b/include/util_misc.h new file mode 100644 index 00000000000..72a6c6bea81 --- /dev/null +++ b/include/util_misc.h @@ -0,0 +1,40 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file util_misc.h + * @brief Apache miscellaneous functions + * + * @defgroup APACHE_CORE_MISC Miscellaneous functions + * @ingroup APACHE_CORE + * @{ + */ + +#ifndef APACHE_UTIL_MISC_H +#define APACHE_UTIL_MISC_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define ARRAY_LEN(a) (sizeof(a)/sizeof(a[0])) + +#ifdef __cplusplus +} +#endif + +#endif /* !APACHE_UTIL_MISC_H */ +/** @} */ diff --git a/modules/filters/sed1.c b/modules/filters/sed1.c index 047f49ba131..d963e26ebb3 100644 --- a/modules/filters/sed1.c +++ b/modules/filters/sed1.c @@ -24,6 +24,7 @@ #include "sed.h" #include "apr_strings.h" #include "regexp.h" +#include "util_misc.h" static const char *const trans[040] = { "\\01", @@ -330,7 +331,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 < 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 30ad9904436..426325cff55 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..52cd5444d10 100644 --- a/modules/http2/h2_bucket_beam.c +++ b/modules/http2/h2_bucket_beam.c @@ -27,6 +27,8 @@ #include #include +#include "util_misc.h" + #include "h2_private.h" #include "h2_conn_ctx.h" #include "h2_headers.h" @@ -74,7 +76,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 = 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 5ed4ee818e7..feb4677c588 100644 --- a/modules/http2/h2_c1_io.c +++ b/modules/http2/h2_c1_io.c @@ -27,6 +27,8 @@ #include #include +#include "util_misc.h" + #include "h2_private.h" #include "h2_bucket_eos.h" #include "h2_config.h" @@ -63,7 +65,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 = 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 523a941450b..5d63e30f41a 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 = 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, 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 874753e4983..3fb172b4df3 100644 --- a/modules/http2/h2_protocol.c +++ b/modules/http2/h2_protocol.c @@ -29,6 +29,8 @@ #include #include +#include "util_misc.h" + #include "mod_http2.h" #include "h2_private.h" @@ -43,7 +45,6 @@ #include "h2_session.h" #include "h2_util.h" #include "h2_protocol.h" -#include "mod_http2.h" const char *h2_protocol_ids_tls[] = { "h2", NULL @@ -77,7 +78,7 @@ static const char *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 < (ARRAY_LEN(h2_err_descr))) { return h2_err_descr[h2_error]; } return "unknown http/2 error code"; @@ -395,7 +396,7 @@ static const char *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 = 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 3faa691c71f..42b7386f8cb 100644 --- a/modules/http2/h2_proxy_session.c +++ b/modules/http2/h2_proxy_session.c @@ -265,7 +265,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, 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); @@ -343,7 +343,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, 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); @@ -801,7 +801,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)); + 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 - @@ -1178,7 +1178,7 @@ static const char *StateNames[] = { static const char *state_name(h2_proxys_state state) { - if (state >= (sizeof(StateNames)/sizeof(StateNames[0]))) { + if (state >= (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 5e1ebe663db..6d08eca1565 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),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 = 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 1e560e47792..4ab496a8ef3 100644 --- a/modules/http2/h2_session.c +++ b/modules/http2/h2_session.c @@ -36,6 +36,8 @@ #include /* for getpid() */ #endif +#include "util_misc.h" + #include "h2_private.h" #include "h2.h" #include "h2_bucket_beam.h" @@ -219,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, 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)"), @@ -344,7 +346,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, ARRAY_LEN(buffer)); if (stream) { ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, session->c1, H2_STRM_LOG(APLOGNO(10302), stream, @@ -440,7 +442,7 @@ static int on_frame_recv_cb(nghttp2_session *ng2s, char buffer[256]; h2_util_frame_print(frame, buffer, - sizeof(buffer)/sizeof(buffer[0])); + ARRAY_LEN(buffer)); ap_log_cerror(APLOG_MARK, APLOG_TRACE2, 0, session->c1, H2_SSSN_MSG(session, "on_frame_rcv %s"), buffer); } @@ -590,7 +592,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, ARRAY_LEN(buffer)); if (stream) { ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, session->c1, H2_STRM_LOG(APLOGNO(10303), stream, @@ -1369,7 +1371,7 @@ static const char *StateNames[] = { const char *h2_session_state_str(h2_session_state state) { - if (state >= (sizeof(StateNames)/sizeof(StateNames[0]))) { + if (state >= (ARRAY_LEN(StateNames))) { return "unknown"; } return StateNames[state]; diff --git a/modules/http2/h2_stream.c b/modules/http2/h2_stream.c index f6c92024519..331a69f6e7a 100644 --- a/modules/http2/h2_stream.c +++ b/modules/http2/h2_stream.c @@ -29,6 +29,8 @@ #include #include +#include "util_misc.h" + #include #include "h2_private.h" @@ -156,12 +158,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, 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, ARRAY_LEN(trans_on_recv)); } static int on_event(h2_stream* stream, h2_stream_event_t ev) @@ -170,7 +172,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 < ARRAY_LEN(trans_on_event)) { return on_map(stream->state, trans_on_event[ev]); } return stream->state; @@ -189,7 +191,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 = 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 8e53cebdf92..390ffbe243e 100644 --- a/modules/http2/h2_util.c +++ b/modules/http2/h2_util.c @@ -25,6 +25,8 @@ #include #include +#include "util_misc.h" + #include #include "h2.h" @@ -1530,7 +1532,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); + ARRAY_LEN(keys), keys, values, response->headers); } #else /* AP_HAS_RESPONSE_BUCKETS */ @@ -1558,7 +1560,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); + ARRAY_LEN(keys), keys, values, headers->headers); } #endif /* else AP_HAS_RESPONSE_BUCKETS */ @@ -1585,7 +1587,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, ARRAY_LEN(keys), keys, values, req->headers); } /******************************************************************************* @@ -1599,7 +1601,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),ARRAY_LEN(a) static literal IgnoredRequestHeaders[] = { H2_DEF_LITERAL("upgrade"), @@ -1769,7 +1771,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 = 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 dcec73eaf2c..e6f537bd7a9 100644 --- a/modules/http2/h2_util.h +++ b/modules/http2/h2_util.h @@ -458,7 +458,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 = 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..2712d8e9ff5 100644 --- a/modules/http2/mod_http2.c +++ b/modules/http2/mod_http2.c @@ -28,6 +28,8 @@ #include "mod_http2.h" +#include "util_misc.h" + #include #include "h2_stream.h" #include "h2_c1.h" @@ -306,11 +308,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 +318,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 < 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 +335,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 < 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 4366bf695c2..a8a3fb07203 100644 --- a/modules/md/md_acme.c +++ b/modules/md/md_acme.c @@ -23,6 +23,8 @@ #include #include +#include "util_misc.h" + #include "md.h" #include "md_crypt.h" #include "md_json.h" @@ -80,7 +82,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 < (ARRAY_LEN(Problems)); ++i) { if (!apr_strnatcasecmp(type, Problems[i].type)) { return Problems[i].rv; } @@ -99,7 +101,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 < (ARRAY_LEN(Problems)); ++i) { if (!apr_strnatcasecmp(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 f4579b366bd..4014dd737f2 100644 --- a/modules/md/md_acme_authz.c +++ b/modules/md/md_acme_authz.c @@ -26,6 +26,8 @@ #include #include +#include "util_misc.h" + #include "md.h" #include "md_crypt.h" #include "md_json.h" @@ -569,7 +571,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 = (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 7aacff0497a..a262f550a6c 100644 --- a/modules/md/md_core.c +++ b/modules/md/md_core.c @@ -24,6 +24,8 @@ #include #include +#include "util_misc.h" + #include "md_json.h" #include "md.h" #include "md_crypt.h" @@ -419,7 +421,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 < ARRAY_LEN(KNOWN_CAs); ++i) { if (!apr_strnatcasecmp(KNOWN_CAs[i].url, url)) { return KNOWN_CAs[i].name; } @@ -437,7 +439,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 < ARRAY_LEN(KNOWN_CAs); ++i) { if (!apr_strnatcasecmp(KNOWN_CAs[i].name, name)) { *purl = KNOWN_CAs[i].url; goto leave; @@ -449,7 +451,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 < 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 59dbd676e9c..4b9e8a27f0f 100644 --- a/modules/md/md_store.c +++ b/modules/md/md_store.c @@ -26,6 +26,8 @@ #include #include +#include "util_misc.h" + #include "md.h" #include "md_crypt.h" #include "md_log.h" @@ -61,7 +63,7 @@ static const char *GROUP_NAME[] = { const char *md_store_group_name(unsigned int group) { - if (group < sizeof(GROUP_NAME)/sizeof(GROUP_NAME[0])) { + if (group < 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 79f694ad30d..8d571c6b3eb 100644 --- a/modules/md/md_store_fs.c +++ b/modules/md/md_store_fs.c @@ -26,6 +26,8 @@ #include #include +#include "util_misc.h" + #include "md.h" #include "md_crypt.h" #include "md_json.h" @@ -367,7 +369,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 >= (ARRAY_LEN(s_fs->group_perms))) { return APR_ENOTIMPL; } s_fs->group_perms[group].file = file_perms; @@ -386,7 +388,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 >= (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 95ecc27b7af..fc1a8fdbaf2 100644 --- a/modules/md/md_util.c +++ b/modules/md/md_util.c @@ -29,6 +29,8 @@ #include #endif +#include "util_misc.h" + #include "md.h" #include "md_log.h" #include "md_util.h" @@ -477,7 +479,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 = 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 1fc4ba5512c..bb465fa0b35 100644 --- a/modules/md/mod_md.c +++ b/modules/md/mod_md.c @@ -28,6 +28,8 @@ #include #include +#include "util_misc.h" + #include "mod_status.h" #include "md.h" @@ -162,7 +164,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)(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 6b29256b678..1f05de49172 100644 --- a/modules/md/mod_md_status.c +++ b/modules/md/mod_md_status.c @@ -26,6 +26,8 @@ #include #include +#include "util_misc.h" + #include "mod_status.h" #include "md.h" @@ -720,14 +722,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)(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)(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; @@ -791,7 +793,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)(ARRAY_LEN(status_infos)); ++i) { si_add_header(&ctx, &status_infos[i]); } apr_brigade_puts(ctx.bb, NULL, NULL, "\n"); @@ -847,14 +849,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)(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)(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; @@ -907,7 +909,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)(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 e45f209b73c..7477421ff8e 100644 --- a/modules/metadata/mod_mime_magic.c +++ b/modules/metadata/mod_mime_magic.c @@ -2076,15 +2076,13 @@ 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 < ARRAY_LEN(compr); i++) { if (nbytes < compr[i].maglen) continue; if (memcmp(buf, compr[i].magic, compr[i].maglen) == 0) diff --git a/modules/ssl/ssl_engine_init.c b/modules/ssl/ssl_engine_init.c index 30fd6c5e4b5..f696f24474e 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 < 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 < 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 < ARRAY_LEN(dhparams); n++) if (keylen >= dhparams[n].min) return dhparams[n].dh; diff --git a/modules/tls/tls_util.h b/modules/tls/tls_util.h index 18ae4dff4a3..6b9c70af0b3 100644 --- a/modules/tls/tls_util.h +++ b/modules/tls/tls_util.h @@ -16,9 +16,6 @@ #ifndef tls_util_h #define tls_util_h -#define TLS_DIM(a) (sizeof(a)/sizeof(a[0])) - - /** * Simple struct to hold a range of bytes and its length together. */ @@ -146,7 +143,7 @@ apr_size_t tls_util_bb_print(char *buffer, apr_size_t bmax, do { \ char buffer[4 * 1024]; \ const char *line = "(null)"; \ - apr_size_t len, bmax = sizeof(buffer)/sizeof(buffer[0]); \ + apr_size_t len, bmax = ARRAY_LEN(buffer); \ len = tls_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/server/mpm/event/event.c b/server/mpm/event/event.c index 29d18a943ac..7b979619e41 100644 --- a/server/mpm/event/event.c +++ b/server/mpm/event/event.c @@ -96,6 +96,7 @@ #include "unixd.h" #include "apr_skiplist.h" #include "util_time.h" +#include "util_misc.h" #include #include /* for INT_MAX */ @@ -2631,7 +2632,7 @@ static void setup_threads_runtime(void) /* Create the main pollset */ pollset_flags = APR_POLLSET_THREADSAFE | APR_POLLSET_NOCOPY | APR_POLLSET_NODEFAULT | APR_POLLSET_WAKEABLE; - for (i = 0; i < sizeof(good_methods) / sizeof(good_methods[0]); i++) { + for (i = 0; i < ARRAY_LEN(good_methods); i++) { rv = apr_pollset_create_ex(&event_pollset, pollset_size, pruntime, pollset_flags, good_methods[i]); if (rv == APR_SUCCESS) { @@ -2641,7 +2642,7 @@ static void setup_threads_runtime(void) } if (rv != APR_SUCCESS) { pollset_flags &= ~APR_POLLSET_WAKEABLE; - for (i = 0; i < sizeof(good_methods) / sizeof(good_methods[0]); i++) { + for (i = 0; i < ARRAY_LEN(good_methods); i++) { rv = apr_pollset_create_ex(&event_pollset, pollset_size, 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 01d83d8011a..820d8419e50 100644 --- a/server/mpm/motorz/motorz.c +++ b/server/mpm/motorz/motorz.c @@ -529,7 +529,7 @@ static int motorz_setup_pollset(motorz_core_t *mz) 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 < ARRAY_LEN(good_methods); i++) { rv = apr_pollset_create_ex(&mz->pollset, 512, mz->pool, diff --git a/server/mpm/simple/simple_run.c b/server/mpm/simple/simple_run.c index fe68d269f12..a1a893e29f7 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 < 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]);