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

Lines changed: 14 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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"),

0 commit comments

Comments
 (0)