Skip to content

Commit 421f8c1

Browse files
committed
Replace manual array size calculation with ARRAY_LEN macro
This commit introduces the ARRAY_LEN macro to simplify array length calculations by replacing occurrences of `sizeof(a)/sizeof(a[0])` with `ARRAY_LEN(a)`.
1 parent b1a1473 commit 421f8c1

26 files changed

+63
-56
lines changed

include/ap_config.h

+14-1
Original file line numberDiff line numberDiff line change
@@ -261,5 +261,18 @@
261261
#define AP_FN_ATTR_NONNULL(x)
262262
#endif
263263

264-
264+
#ifndef ARRAY_LEN
265+
/**
266+
* @def ARRAY_LEN(a)
267+
* @brief Computes the number of elements in a static array.
268+
*
269+
* This macro replaces `sizeof(a)/sizeof(a[0])` to improve readability.
270+
* It should only be used with fixed-size arrays, not pointers or dynamically
271+
* allocated memory.
272+
*
273+
* @param a The static array.
274+
* @return The number of elements in the array.
275+
*/
276+
#define ARRAY_LEN(a) (sizeof(a) / sizeof((a)[0]))
277+
#endif
265278
#endif /* AP_CONFIG_H */

modules/filters/sed1.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "sed.h"
2525
#include "apr_strings.h"
2626
#include "regexp.h"
27+
#include "ap_config.h"
2728

2829
static const char *const trans[040] = {
2930
"\\01",
@@ -330,7 +331,7 @@ apr_status_t sed_reset_eval(sed_eval_t *eval, sed_commands_t *commands, sed_err_
330331
eval->hspend = eval->holdbuf;
331332
eval->lcomend = &eval->genbuf[71];
332333

333-
for (i = 0; i < sizeof(eval->abuf) / sizeof(eval->abuf[0]); i++)
334+
for (i = 0; i < ARRAY_LEN(eval->abuf); i++)
334335
eval->abuf[i] = NULL;
335336
eval->aptr = eval->abuf;
336337
eval->pending = NULL;

modules/http2/h2.h

-2
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,6 @@ extern const char *H2_MAGIC_TOKEN;
9595

9696
#define H2_STREAM_CLIENT_INITIATED(id) (id&0x01)
9797

98-
#define H2_ALEN(a) (sizeof(a)/sizeof((a)[0]))
99-
10098
#define H2MAX(x,y) ((x) > (y) ? (x) : (y))
10199
#define H2MIN(x,y) ((x) < (y) ? (x) : (y))
102100

modules/http2/h2_bucket_beam.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ static int h2_blist_count(h2_blist *blist)
7474
do { \
7575
if (APLOG_C_IS_LEVEL((c),(level))) { \
7676
char buffer[4 * 1024]; \
77-
apr_size_t len, bmax = sizeof(buffer)/sizeof(buffer[0]); \
77+
apr_size_t len, bmax = ARRAY_LEN(buffer); \
7878
len = bb? h2_util_bb_print(buffer, bmax, "", "", bb) : 0; \
7979
ap_log_cerror(APLOG_MARK, (level), rv, (c), \
8080
"BEAM[%s,%s%sdata=%ld,buckets(send/consumed)=%d/%d]: %s %s", \

modules/http2/h2_c1_io.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ static void h2_c1_io_bb_log(conn_rec *c, int stream_id, int level,
6363
{
6464
char buffer[16 * 1024];
6565
const char *line = "(null)";
66-
int bmax = sizeof(buffer)/sizeof(buffer[0]);
66+
int bmax = ARRAY_LEN(buffer);
6767
int off = 0;
6868
apr_bucket *b;
6969

modules/http2/h2_c2_filter.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ apr_status_t h2_c2_filter_request_in(ap_filter_t *f,
154154
do { \
155155
if (APLOG_C_IS_LEVEL((c),(level))) { \
156156
char buffer[4 * 1024]; \
157-
apr_size_t len, bmax = sizeof(buffer)/sizeof(buffer[0]); \
157+
apr_size_t len, bmax = ARRAY_LEN(buffer); \
158158
len = h2_util_bb_print(buffer, bmax, "", "", (bb)); \
159159
ap_log_cerror(APLOG_MARK, (level), rv, (c), \
160160
"FILTER[%s]: %s %s", \
@@ -800,7 +800,7 @@ static void make_chunk(conn_rec *c, h2_chunk_filter_t *fctx, apr_bucket_brigade
800800
apr_bucket *b;
801801
apr_size_t len;
802802

803-
len = (apr_size_t)apr_snprintf(buffer, H2_ALEN(buffer),
803+
len = (apr_size_t)apr_snprintf(buffer, ARRAY_LEN(buffer),
804804
"%"APR_UINT64_T_HEX_FMT"\r\n", (apr_uint64_t)chunk_len);
805805
b = apr_bucket_heap_create(buffer, len, NULL, bb->bucket_alloc);
806806
APR_BUCKET_INSERT_BEFORE(first, b);

modules/http2/h2_protocol.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ static const char *h2_err_descr[] = {
7777

7878
const char *h2_protocol_err_description(unsigned int h2_error)
7979
{
80-
if (h2_error < (sizeof(h2_err_descr)/sizeof(h2_err_descr[0]))) {
80+
if (h2_error < (ARRAY_LEN(h2_err_descr))) {
8181
return h2_err_descr[h2_error];
8282
}
8383
return "unknown http/2 error code";
@@ -395,7 +395,7 @@ static const char *RFC7540_names[] = {
395395
"SSL3_CK_SCSV", /* TLS_EMPTY_RENEGOTIATION_INFO_SCSV */
396396
"SSL3_CK_FALLBACK_SCSV"
397397
};
398-
static size_t RFC7540_names_LEN = sizeof(RFC7540_names)/sizeof(RFC7540_names[0]);
398+
static size_t RFC7540_names_LEN = ARRAY_LEN(RFC7540_names);
399399

400400

401401
static apr_hash_t *BLCNames;

modules/http2/h2_proxy_session.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ static int on_frame_recv(nghttp2_session *ngh2, const nghttp2_frame *frame,
265265
if (APLOGcdebug(session->c)) {
266266
char buffer[256];
267267

268-
h2_proxy_util_frame_print(frame, buffer, sizeof(buffer)/sizeof(buffer[0]));
268+
h2_proxy_util_frame_print(frame, buffer, ARRAY_LEN(buffer));
269269
ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, session->c, APLOGNO(03341)
270270
"h2_proxy_session(%s): recv FRAME[%s]",
271271
session->id, buffer);
@@ -343,7 +343,7 @@ static int before_frame_send(nghttp2_session *ngh2,
343343
if (APLOGcdebug(session->c)) {
344344
char buffer[256];
345345

346-
h2_proxy_util_frame_print(frame, buffer, sizeof(buffer)/sizeof(buffer[0]));
346+
h2_proxy_util_frame_print(frame, buffer, ARRAY_LEN(buffer));
347347
ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, session->c, APLOGNO(03343)
348348
"h2_proxy_session(%s): sent FRAME[%s]",
349349
session->id, buffer);
@@ -801,7 +801,7 @@ static apr_status_t session_start(h2_proxy_session *session)
801801
settings[1].value = (1 << session->window_bits_stream) - 1;
802802

803803
rv = nghttp2_submit_settings(session->ngh2, NGHTTP2_FLAG_NONE, settings,
804-
H2_ALEN(settings));
804+
ARRAY_LEN(settings));
805805

806806
/* If the connection window is larger than our default, trigger a WINDOW_UPDATE */
807807
add_conn_window = ((1 << session->window_bits_connection) - 1 -

modules/http2/h2_proxy_session.h

-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
#ifndef h2_proxy_session_h
1818
#define h2_proxy_session_h
1919

20-
#define H2_ALEN(a) (sizeof(a)/sizeof((a)[0]))
21-
2220
#include <nghttp2/nghttp2.h>
2321

2422
struct h2_proxy_iqueue;

modules/http2/h2_proxy_util.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ typedef struct {
478478
} literal;
479479

480480
#define H2_DEF_LITERAL(n) { (n), (sizeof(n)-1) }
481-
#define H2_LIT_ARGS(a) (a),H2_ALEN(a)
481+
#define H2_LIT_ARGS(a) (a),ARRAY_LEN(a)
482482

483483
static literal IgnoredRequestHeaders[] = {
484484
H2_DEF_LITERAL("upgrade"),

modules/http2/h2_session.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ static int on_invalid_frame_recv_cb(nghttp2_session *ngh2,
219219
if (APLOGcdebug(session->c1)) {
220220
char buffer[256];
221221

222-
h2_util_frame_print(frame, buffer, sizeof(buffer)/sizeof(buffer[0]));
222+
h2_util_frame_print(frame, buffer, ARRAY_LEN(buffer));
223223
ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, session->c1,
224224
H2_SSSN_LOG(APLOGNO(03063), session,
225225
"recv invalid FRAME[%s], frames=%ld/%ld (r/s)"),
@@ -352,7 +352,7 @@ static int on_frame_recv_cb(nghttp2_session *ng2s,
352352
if (APLOGcdebug(session->c1)) {
353353
char buffer[256];
354354

355-
h2_util_frame_print(frame, buffer, sizeof(buffer)/sizeof(buffer[0]));
355+
h2_util_frame_print(frame, buffer, ARRAY_LEN(buffer));
356356
if (stream) {
357357
ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, session->c1,
358358
H2_STRM_LOG(APLOGNO(10302), stream,
@@ -450,7 +450,7 @@ static int on_frame_recv_cb(nghttp2_session *ng2s,
450450
char buffer[256];
451451

452452
h2_util_frame_print(frame, buffer,
453-
sizeof(buffer)/sizeof(buffer[0]));
453+
ARRAY_LEN(buffer));
454454
ap_log_cerror(APLOG_MARK, APLOG_TRACE2, 0, session->c1,
455455
H2_SSSN_MSG(session, "on_frame_rcv %s"), buffer);
456456
}
@@ -600,7 +600,7 @@ static int on_frame_send_cb(nghttp2_session *ngh2,
600600
if (APLOGcdebug(session->c1)) {
601601
char buffer[256];
602602

603-
h2_util_frame_print(frame, buffer, sizeof(buffer)/sizeof(buffer[0]));
603+
h2_util_frame_print(frame, buffer, ARRAY_LEN(buffer));
604604
if (stream) {
605605
ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, session->c1,
606606
H2_STRM_LOG(APLOGNO(10303), stream,
@@ -635,7 +635,7 @@ static int on_frame_not_send_cb(nghttp2_session *ngh2,
635635
char buffer[256];
636636

637637
stream = get_stream(session, stream_id);
638-
h2_util_frame_print(frame, buffer, sizeof(buffer)/sizeof(buffer[0]));
638+
h2_util_frame_print(frame, buffer, ARRAY_LEN(buffer));
639639
ap_log_cerror(APLOG_MARK, APLOG_ERR, 0, session->c1,
640640
H2_SSSN_LOG(APLOGNO(), session,
641641
"not sent FRAME[%s], error %d: %s"),

modules/http2/h2_stream.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,12 @@ static int on_frame(h2_stream_state_t state, int frame_type,
156156

157157
static int on_frame_send(h2_stream_state_t state, int frame_type)
158158
{
159-
return on_frame(state, frame_type, trans_on_send, H2_ALEN(trans_on_send));
159+
return on_frame(state, frame_type, trans_on_send, ARRAY_LEN(trans_on_send));
160160
}
161161

162162
static int on_frame_recv(h2_stream_state_t state, int frame_type)
163163
{
164-
return on_frame(state, frame_type, trans_on_recv, H2_ALEN(trans_on_recv));
164+
return on_frame(state, frame_type, trans_on_recv, ARRAY_LEN(trans_on_recv));
165165
}
166166

167167
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)
170170
if (stream->monitor && stream->monitor->on_event) {
171171
stream->monitor->on_event(stream->monitor->ctx, stream, ev);
172172
}
173-
if (ev < H2_ALEN(trans_on_event)) {
173+
if (ev < ARRAY_LEN(trans_on_event)) {
174174
return on_map(stream->state, trans_on_event[ev]);
175175
}
176176
return stream->state;
@@ -189,7 +189,7 @@ static void H2_STREAM_OUT_LOG(int lvl, h2_stream *s, const char *tag)
189189
if (APLOG_C_IS_LEVEL(s->session->c1, lvl)) {
190190
conn_rec *c = s->session->c1;
191191
char buffer[4 * 1024];
192-
apr_size_t len, bmax = sizeof(buffer)/sizeof(buffer[0]);
192+
apr_size_t len, bmax = ARRAY_LEN(buffer);
193193

194194
len = h2_util_bb_print(buffer, bmax, tag, "", s->out_buffer);
195195
ap_log_cerror(APLOG_MARK, lvl, 0, c,

modules/http2/h2_util.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -1530,7 +1530,7 @@ apr_status_t h2_res_create_ngheader(h2_ngheader **ph, apr_pool_t *p,
15301530
apr_psprintf(p, "%d", response->status)
15311531
};
15321532
return ngheader_create(ph, p, is_unsafe(response),
1533-
H2_ALEN(keys), keys, values, response->headers);
1533+
ARRAY_LEN(keys), keys, values, response->headers);
15341534
}
15351535

15361536
#else /* AP_HAS_RESPONSE_BUCKETS */
@@ -1558,7 +1558,7 @@ apr_status_t h2_res_create_ngheader(h2_ngheader **ph, apr_pool_t *p,
15581558
apr_psprintf(p, "%d", headers->status)
15591559
};
15601560
return ngheader_create(ph, p, is_unsafe(headers),
1561-
H2_ALEN(keys), keys, values, headers->headers);
1561+
ARRAY_LEN(keys), keys, values, headers->headers);
15621562
}
15631563

15641564
#endif /* else AP_HAS_RESPONSE_BUCKETS */
@@ -1585,7 +1585,7 @@ apr_status_t h2_req_create_ngheader(h2_ngheader **ph, apr_pool_t *p,
15851585
ap_assert(req->path);
15861586
ap_assert(req->method);
15871587

1588-
return ngheader_create(ph, p, 0, H2_ALEN(keys), keys, values, req->headers);
1588+
return ngheader_create(ph, p, 0, ARRAY_LEN(keys), keys, values, req->headers);
15891589
}
15901590

15911591
/*******************************************************************************
@@ -1599,7 +1599,7 @@ typedef struct {
15991599
} literal;
16001600

16011601
#define H2_DEF_LITERAL(n) { (n), (sizeof(n)-1) }
1602-
#define H2_LIT_ARGS(a) (a),H2_ALEN(a)
1602+
#define H2_LIT_ARGS(a) (a),ARRAY_LEN(a)
16031603

16041604
static literal IgnoredRequestHeaders[] = {
16051605
H2_DEF_LITERAL("upgrade"),

modules/http2/h2_util.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ if (APLOG_C_IS_LEVEL(c, level)) { \
458458
do { \
459459
char buffer[4 * 1024]; \
460460
const char *line = "(null)"; \
461-
apr_size_t len, bmax = sizeof(buffer)/sizeof(buffer[0]); \
461+
apr_size_t len, bmax = ARRAY_LEN(buffer); \
462462
len = h2_util_bb_print(buffer, bmax, (tag), "", (bb)); \
463463
ap_log_cerror(APLOG_MARK, level, 0, (c), "bb_dump(%ld): %s", \
464464
((c)->master? (c)->master->id : (c)->id), (len? buffer : line)); \

modules/http2/mod_http2.c

+2-7
Original file line numberDiff line numberDiff line change
@@ -306,11 +306,6 @@ static h2_var_def H2_VARS[] = {
306306
{ "H2_STREAM_TAG", val_H2_STREAM_TAG, 1 },
307307
};
308308

309-
#ifndef H2_ALEN
310-
#define H2_ALEN(a) (sizeof(a)/sizeof((a)[0]))
311-
#endif
312-
313-
314309
static int http2_is_h2(conn_rec *c)
315310
{
316311
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,
321316
{
322317
unsigned int i;
323318
/* If the # of vars grow, we need to put definitions in a hash */
324-
for (i = 0; i < H2_ALEN(H2_VARS); ++i) {
319+
for (i = 0; i < ARRAY_LEN(H2_VARS); ++i) {
325320
h2_var_def *vdef = &H2_VARS[i];
326321
if (!strcmp(vdef->name, name)) {
327322
h2_conn_ctx_t *ctx = (r? h2_conn_ctx_get(c) :
@@ -338,7 +333,7 @@ static int h2_h2_fixups(request_rec *r)
338333
h2_conn_ctx_t *ctx = h2_conn_ctx_get(r->connection);
339334
unsigned int i;
340335

341-
for (i = 0; ctx && i < H2_ALEN(H2_VARS); ++i) {
336+
for (i = 0; ctx && i < ARRAY_LEN(H2_VARS); ++i) {
342337
h2_var_def *vdef = &H2_VARS[i];
343338
if (vdef->subprocess) {
344339
apr_table_setn(r->subprocess_env, vdef->name,

modules/md/md_acme_authz.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
#include "md_acme.h"
4040
#include "md_acme_authz.h"
41+
#include "ap_config.h"
4142

4243
md_acme_authz_t *md_acme_authz_create(apr_pool_t *p)
4344
{
@@ -568,7 +569,7 @@ static const cha_type CHA_TYPES[] = {
568569
{ MD_AUTHZ_TYPE_TLSALPN01, cha_tls_alpn_01_setup, cha_teardown_dir },
569570
{ MD_AUTHZ_TYPE_DNS01, cha_dns_01_setup, cha_dns_01_teardown },
570571
};
571-
static const apr_size_t CHA_TYPES_LEN = (sizeof(CHA_TYPES)/sizeof(CHA_TYPES[0]));
572+
static const apr_size_t CHA_TYPES_LEN = (ARRAY_LEN(CHA_TYPES));
572573

573574
typedef struct {
574575
apr_pool_t *p;

modules/md/md_store.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "md_json.h"
3333
#include "md_store.h"
3434
#include "md_util.h"
35+
#include "ap_config.h"
3536

3637
/**************************************************************************************************/
3738
/* generic callback handling */
@@ -61,7 +62,7 @@ static const char *GROUP_NAME[] = {
6162

6263
const char *md_store_group_name(unsigned int group)
6364
{
64-
if (group < sizeof(GROUP_NAME)/sizeof(GROUP_NAME[0])) {
65+
if (group < ARRAY_LEN(GROUP_NAME)) {
6566
return GROUP_NAME[group];
6667
}
6768
return "UNKNOWN";

modules/md/md_store_fs.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "md_store_fs.h"
3535
#include "md_util.h"
3636
#include "md_version.h"
37+
#include "ap_config.h"
3738

3839
/**************************************************************************************************/
3940
/* file system based implementation of md_store_t */
@@ -376,7 +377,7 @@ apr_status_t md_store_fs_group_perms_set(md_store_t *store, md_store_group_t gro
376377
{
377378
md_store_fs_t *s_fs = FS_STORE(store);
378379

379-
if (group >= (sizeof(s_fs->group_perms)/sizeof(s_fs->group_perms[0]))) {
380+
if (group >= (ARRAY_LEN(s_fs->group_perms))) {
380381
return APR_ENOTIMPL;
381382
}
382383
s_fs->group_perms[group].file = file_perms;
@@ -395,7 +396,7 @@ apr_status_t md_store_fs_set_event_cb(struct md_store_t *store, md_store_fs_cb *
395396

396397
static const perms_t *gperms(md_store_fs_t *s_fs, md_store_group_t group)
397398
{
398-
if (group >= (sizeof(s_fs->group_perms)/sizeof(s_fs->group_perms[0]))
399+
if (group >= (ARRAY_LEN(s_fs->group_perms))
399400
|| !s_fs->group_perms[group].dir) {
400401
return &s_fs->def_perms;
401402
}

modules/md/md_util.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "md.h"
3333
#include "md_log.h"
3434
#include "md_util.h"
35+
#include "ap_config.h"
3536

3637
/**************************************************************************************************/
3738
/* pool utils */
@@ -477,7 +478,7 @@ apr_status_t md_text_fread8k(const char **ptext, apr_pool_t *p, const char *fpat
477478

478479
*ptext = NULL;
479480
if (APR_SUCCESS == (rv = apr_file_open(&f, fpath, APR_FOPEN_READ, 0, p))) {
480-
apr_size_t blen = sizeof(buffer)/sizeof(buffer[0]) - 1;
481+
apr_size_t blen = ARRAY_LEN(buffer) - 1;
481482
rv = apr_file_read_full(f, buffer, blen, &blen);
482483
if (APR_SUCCESS == rv || APR_STATUS_IS_EOF(rv)) {
483484
*ptext = apr_pstrndup(p, buffer, blen);

modules/md/mod_md.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ static apr_status_t notify(md_job_t *job, const char *reason,
162162
int i;
163163

164164
log_msg_reason = apr_psprintf(p, "message-%s", reason);
165-
for (i = 0; i < (int)(sizeof(notify_rates)/sizeof(notify_rates[0])); ++i) {
165+
for (i = 0; i < (int)(ARRAY_LEN(notify_rates)); ++i) {
166166
if (!strcmp(reason, notify_rates[i].reason)) {
167167
min_interim = notify_rates[i].min_interim;
168168
}

0 commit comments

Comments
 (0)