tidy-up: use more static, sizeof(), char[], double-const

- make `const` data `static`, where missing and possible.
- replace `strlen()` on literal or const strings with `sizeof()`.
  While the latter is optimized by popular C compiler, e.g. MSVC only
  does it with `/O2`.
- replace magic numbers with `sizeof()`, where missing.
- introduce `CURL_CSTRLEN()` macro for `sizeof(char[]) - 1`.
- use `CURL_CSTRLEN()` macro.
- move `const` before integer types, where missing.
- replace `char *var` with `var[]`, where missing and possible.
- use double const, where missing.
  `static const char *` -> `static const char * const`.
- lib1514: constify pointers.
- unit3205: drop redundant cast, avoid another one.
- unit1666: map `OID()` macro to identical `STRCONST()`.

Closes #22406
This commit is contained in:
Viktor Szakats 2026-07-23 01:50:06 +02:00
parent 573a6ec16b
commit e1450d8fda
No known key found for this signature in database
117 changed files with 311 additions and 287 deletions

View file

@ -85,7 +85,7 @@ static CURLcode test_unit1303(const char *arg)
const char *comment;
};
const struct timetest run[] = {
static const struct timetest run[] = {
/* both timeouts set, not connecting */
{BASE + 4, 0, 10000, 8000, FALSE, 6000, "6 seconds should be left"},
{BASE + 4, 990000, 10000, 8000, FALSE, 5010, "5010 ms should be left"},

View file

@ -35,7 +35,7 @@ static CURLcode test_unit1395(const char *arg)
const char *output;
};
const struct dotdot pairs[] = {
static const struct dotdot pairs[] = {
{ "/%2f%2e%2e%2f/../a", "/a" },
{ "/%2f%2e%2e%2f/../", "/" },
{ "/%2f%2e%2e%2f/.", "/%2f%2e%2e%2f/" },

View file

@ -51,7 +51,7 @@ static CURLcode test_unit1396(const char *arg)
};
/* unescape, this => that */
const struct test list1[] = {
static const struct test list1[] = {
{ "%61", 3, "a", 1 },
{ "%61a", 4, "aa", 2 },
{ "%61b", 4, "ab", 2 },
@ -67,7 +67,7 @@ static CURLcode test_unit1396(const char *arg)
{ NULL, 0, NULL, 0 } /* end of list marker */
};
/* escape, this => that */
const struct test list2[] = {
static const struct test list2[] = {
{ "a", 1, "a", 1 },
{ "/", 1, "%2F", 3 },
{ "a=b", 3, "a%3Db", 5 },

View file

@ -34,7 +34,7 @@ static CURLcode test_unit1398(const char *arg)
int rc;
char buf[3] = { 'b', 'u', 'g' };
static const char *str = "bug";
static const char str[] = "bug";
int width = 3;
char output[130];

View file

@ -41,8 +41,8 @@ static CURLcode test_unit1625(const char *arg)
char *large;
size_t i;
const size_t large_value_len = MAX_HTTP_RESP_HEADER_SIZE;
const size_t large_prefix_len = strlen("Encoding: ");
const size_t large_suffix_len = strlen(", chunked");
const size_t large_prefix_len = CURL_CSTRLEN("Encoding: ");
const size_t large_suffix_len = CURL_CSTRLEN(", chunked");
static const struct check1625 list[] = {
/* basic case */
{ "Encoding: gzip, chunked", "Encoding:", "chunked", TRUE },

View file

@ -32,7 +32,7 @@ static CURLcode test_unit1627(const char *arg)
size_t i, j;
/* existing schemes in different cases */
static const char *okay[] = {
static const char * const okay[] = {
/* all upper */
"DICT", "FILE", "FTP", "FTPS", "GOPHER", "GOPHERS", "HTTP", "HTTPS",
"IMAP", "IMAPS", "LDAP", "LDAPS", "MQTT", "MQTTS", "POP3", "POP3S",
@ -50,7 +50,7 @@ static CURLcode test_unit1627(const char *arg)
"TELNEt", "tFTP", "Ws", "wSS",
};
/* non-existing schemes */
static const char *notokay[] = {
static const char * const notokay[] = {
"a", "A", "htt", "ttp", "httt", "http+", "HTTPP", "HTTPPS", "HTTSP",
"GROPHER", "D1CT", "AbG", "zLQp", "mNrtW", "PkY", "bVcxZq", "LmO",
"iUhyT", "rEwQA", "xSdfG", "nBvC", "pOiuY", "tRewQ", "aSdfG", "hJkl",

View file

@ -54,8 +54,10 @@ static CURLcode test_unit1650(const char *arg)
};
static const struct dohrequest req[] = {
{"test.host.name", CURL_DNS_TYPE_A, DNS_Q1, sizeof(DNS_Q1)-1, DOH_OK },
{"test.host.name", CURL_DNS_TYPE_AAAA, DNS_Q2, sizeof(DNS_Q2)-1, DOH_OK },
{"test.host.name",
CURL_DNS_TYPE_A, DNS_Q1, CURL_CSTRLEN(DNS_Q1), DOH_OK },
{"test.host.name",
CURL_DNS_TYPE_AAAA, DNS_Q2, CURL_CSTRLEN(DNS_Q2), DOH_OK },
{"zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"
".host.name",
CURL_DNS_TYPE_AAAA, NULL, 0, DOH_DNS_BAD_LABEL }
@ -235,7 +237,7 @@ static CURLcode test_unit1650(const char *arg)
}
/* pass all sizes into the decoder until full */
for(i = 0; i < sizeof(full49) - 1; i++) {
for(i = 0; i < CURL_CSTRLEN(full49); i++) {
struct dohentry d;
DOHcode rc;
memset(&d, 0, sizeof(d));
@ -267,8 +269,8 @@ static CURLcode test_unit1650(const char *arg)
struct dohentry d;
struct dohaddr *a;
memset(&d, 0, sizeof(d));
rc = doh_resp_decode((const unsigned char *)full49,
sizeof(full49) - 1, CURL_DNS_TYPE_A, &d);
rc = doh_resp_decode((const unsigned char *)full49, CURL_CSTRLEN(full49),
CURL_DNS_TYPE_A, &d);
fail_if(d.numaddr != 1, "missing address");
a = &d.addr[0];
p = &a->ip.v4[0];

View file

@ -132,9 +132,9 @@ static CURLcode test_unit1655(const char *arg)
const size_t buflen = sizeof(buffer);
const size_t magic1 = 9765;
size_t olen1 = magic1;
static const char *sunshine1 = "a.com";
static const char *dotshine1 = "a.com.";
static const char *sunshine2 = "aa.com";
static const char sunshine1[] = "a.com";
static const char dotshine1[] = "a.com.";
static const char sunshine2[] = "aa.com";
size_t olen2;
DOHcode ret2;
size_t olen;

View file

@ -41,7 +41,7 @@ static CURLcode test_unit1664(const char *arg)
{
UNITTEST_BEGIN(t1664_setup())
static const char *wordparse[] = {
static const char * const wordparse[] = {
"word",
"word ",
" word ",
@ -78,7 +78,7 @@ static CURLcode test_unit1664(const char *arg)
}
{
static const char *qwords[] = {
static const char * const qwords[] = {
"\"word\"",
"\"word",
"word\"",
@ -112,7 +112,7 @@ static CURLcode test_unit1664(const char *arg)
}
{
static const char *single[] = {
static const char * const single[] = {
"a",
"aa",
"A",
@ -133,7 +133,7 @@ static CURLcode test_unit1664(const char *arg)
}
{
static const char *single[] = {
static const char * const single[] = {
"a",
"aa",
"A",
@ -156,7 +156,7 @@ static CURLcode test_unit1664(const char *arg)
}
{
static const char *single[] = {
static const char * const single[] = {
"a",
"aa",
"A",
@ -177,7 +177,7 @@ static CURLcode test_unit1664(const char *arg)
}
{
static const char *nums[] = {
static const char * const nums[] = {
"1",
"10000",
"1234",
@ -313,7 +313,7 @@ static CURLcode test_unit1664(const char *arg)
{
/* CURL_OFF_T is typically 9223372036854775807 */
static const char *nums[] = {
static const char * const nums[] = {
"9223372036854775807", /* 2^63 -1 */
"9223372036854775808", /* 2^63 */
"18446744073709551615", /* 2^64 - 1 */
@ -347,7 +347,7 @@ static CURLcode test_unit1664(const char *arg)
}
{
static const char *newl[] = {
static const char * const newl[] = {
"a",
"aa",
"A",
@ -372,7 +372,7 @@ static CURLcode test_unit1664(const char *arg)
}
{
static const char *nums[] = {
static const char * const nums[] = {
"1",
"1000",
"1234",
@ -399,7 +399,7 @@ static CURLcode test_unit1664(const char *arg)
}
{
static const char *nums[] = {
static const char * const nums[] = {
"1",
"1000",
"1234",
@ -427,7 +427,7 @@ static CURLcode test_unit1664(const char *arg)
{
/* CURL_OFF_T is typically 2^63-1 */
static const char *nums[] = {
static const char * const nums[] = {
"777777777777777777777", /* 2^63 -1 */
"1000000000000000000000", /* 2^63 */
"111111111111111111111",
@ -451,7 +451,7 @@ static CURLcode test_unit1664(const char *arg)
{
/* CURL_OFF_T is typically 2^63-1 */
static const char *nums[] = {
static const char * const nums[] = {
"7FFFFFFFFFFFFFFF", /* 2^63 -1 */
"8000000000000000", /* 2^63 */
"1111111111111111",

View file

@ -36,7 +36,7 @@ struct test_1666 {
};
/* the size of the object needs to deduct the null-terminator */
#define OID(x) x, sizeof(x) - 1
#define OID(x) STRCONST(x)
static bool test1666(const struct test_1666 *spec, size_t i,
struct dynbuf *dbuf)

View file

@ -38,7 +38,7 @@ static CURLcode test_unit1675(const char *arg)
const char *in;
const char *out;
};
const struct ipv4_test tests[] = {
static const struct ipv4_test tests[] = {
{ "0x.0x.0x.0x", NULL }, /* invalid hex */
{ "0x.0x.0x", NULL }, /* invalid hex */
{ "0x.0x", NULL }, /* invalid hex */
@ -146,7 +146,7 @@ static CURLcode test_unit1675(const char *arg)
unsigned int query;
const char *out;
};
const struct urlencode_test tests[] = {
static const struct urlencode_test tests[] = {
{ "http://leave\x01/hello\x01world", FALSE, QUERY_NO,
"http://leave\x01/hello%01world" },
{ "http://leave/hello\x01world", FALSE, QUERY_NO,
@ -204,7 +204,7 @@ static CURLcode test_unit1675(const char *arg)
const char *out_host;
const char *out_zone;
};
const struct ipv6_test tests[] = {
static const struct ipv6_test tests[] = {
{ "[::1]", "[::1]", NULL },
{ "[fe80::1%eth0]", "[fe80::1]", "eth0" },
{ "[fe80::1%25eth0]", "[fe80::1]", "eth0" },
@ -260,7 +260,7 @@ static CURLcode test_unit1675(const char *arg)
const char *out_path;
bool fine;
};
const struct file_test tests[] = {
static const struct file_test tests[] = {
{ "file:///etc/hosts", "/etc/hosts", TRUE },
{ "file://localhost/etc/hosts", "/etc/hosts", TRUE },
{ "file://apple/etc/hosts", "/etc/hosts", FALSE },
@ -317,7 +317,7 @@ static CURLcode test_unit1675(const char *arg)
const char *path;
bool expect_match;
};
const struct origin_test tests[] = {
static const struct origin_test tests[] = {
{ "http://host:123/x", "http", "host", "123", "/y", TRUE },
{ "http://host:123/x", NULL, "host", "123", "/y", TRUE },
{ "http://host:123/x", NULL, NULL, NULL, "/y", TRUE },
@ -395,7 +395,7 @@ loop_end:
const char *options;
size_t offset;
};
const struct test tests[] = {
static const struct test tests[] = {
{ CURLUE_OK, "foo:bar@host", NULL, 0, "foo", "bar", "o", 8 },
{ CURLUE_OK, "foo:bar;abc@host", "imap", 0, "foo", "bar", "abc", 12 },
{ CURLUE_OK, "foo:bar;abc@host", NULL, 0, "foo", "bar;abc", "o", 12 },

View file

@ -48,7 +48,7 @@ static void check_eq(const char *s, const char *exp_s, const char *name)
}
struct tcase {
const char **input;
const char * const *input;
const char *default_scheme;
const char *custom_method;
const char *method;
@ -117,7 +117,7 @@ static CURLcode test_unit2603(const char *arg)
UNITTEST_BEGIN_SIMPLE
#ifndef CURL_DISABLE_HTTP
static const char *T1_INPUT[] = {
static const char * const T1_INPUT[] = {
"GET /path HTTP/1.1\r\nHost: test.curl.se\r\n\r\n",
NULL,
};
@ -128,7 +128,7 @@ static CURLcode test_unit2603(const char *arg)
T1_INPUT, "https", NULL, "GET", "https", NULL, "/path", 1, 0
};
static const char *T2_INPUT[] = {
static const char * const T2_INPUT[] = {
"GET /path HTT",
"P/1.1\r\nHost: te",
"st.curl.se\r\n\r",
@ -139,7 +139,7 @@ static CURLcode test_unit2603(const char *arg)
T2_INPUT, NULL, NULL, "GET", NULL, NULL, "/path", 1, 8
};
static const char *T3_INPUT[] = {
static const char * const T3_INPUT[] = {
"GET ftp://ftp.curl.se/xxx?a=2 HTTP/1.1\r\nContent-Length: 0\r",
"\nUser-Agent: xxx\r\n\r\n",
NULL,
@ -148,7 +148,7 @@ static CURLcode test_unit2603(const char *arg)
T3_INPUT, NULL, NULL, "GET", "ftp", "ftp.curl.se", "/xxx?a=2", 2, 0
};
static const char *T4_INPUT[] = {
static const char * const T4_INPUT[] = {
"CONNECT ftp.curl.se:123 HTTP/1.1\r\nContent-Length: 0\r\n",
"User-Agent: xxx\r\n",
"nothing: \r\n\r\n\n\n",
@ -158,7 +158,7 @@ static CURLcode test_unit2603(const char *arg)
T4_INPUT, NULL, NULL, "CONNECT", NULL, "ftp.curl.se:123", NULL, 3, 2
};
static const char *T6_INPUT[] = {
static const char * const T6_INPUT[] = {
"PUT /path HTTP/1.1\nHost: test.curl.se\n\n123",
NULL,
};
@ -167,7 +167,7 @@ static CURLcode test_unit2603(const char *arg)
};
/* test a custom method with space, #19543 */
static const char *T7_INPUT[] = {
static const char * const T7_INPUT[] = {
"IN SANE /path HTTP/1.1\r\nContent-Length: 0\r\n\r\n",
NULL,
};

View file

@ -45,7 +45,7 @@ static CURLcode test_unit3200(const char *arg)
#define C1024 C256 C256 C256 C256
#define C4096 C1024 C1024 C1024 C1024
static const char *filecontents[] = {
static const char * const filecontents[] = {
/* Both should be read */
"LINE1\n"
"LINE2 NEWLINE\n",

View file

@ -420,7 +420,7 @@ static CURLcode test_unit3205(const char *arg)
#endif
};
static const char *cs_test_string =
static const char cs_test_string[] =
"TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:"
"TLS_CHACHA20_POLY1305_SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:"
"ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:"
@ -561,11 +561,12 @@ static CURLcode test_unit3205(const char *arg)
if(test->id >= 0x0011 && test->id < 0x0017) {
if(expect && !memcmp(expect, "EDH-", 4)) {
curlx_strcopy(alt, sizeof(alt), expect, strlen(expect));
expect = (const char *)memcpy(alt, "DHE-", sizeof("DHE-") - 1);
expect = memcpy(alt, "DHE-", CURL_CSTRLEN("DHE-"));
}
if(expect && !memcmp(expect + 4, "EDH-", 4)) {
curlx_strcopy(alt, sizeof(alt), expect, strlen(expect));
expect = (const char *)memcpy(alt + 4, "DHE-", sizeof("DHE-") - 1) - 4;
expect = memcpy(alt + 4, "DHE-", CURL_CSTRLEN("DHE-"));
expect -= 4;
}
}

View file

@ -56,13 +56,13 @@ static void check_capsule_hdr(size_t payload_len,
static void test_capsule_encap_udp_hdr_boundaries(void)
{
const unsigned char p0[] = { 0x00, 0x01, 0x00 };
const unsigned char p62[] = { 0x00, 0x3F, 0x00 };
const unsigned char p63[] = { 0x00, 0x40, 0x40, 0x00 };
const unsigned char p64[] = { 0x00, 0x40, 0x41, 0x00 };
const unsigned char p16382[] = { 0x00, 0x7F, 0xFF, 0x00 };
const unsigned char p16383[] = { 0x00, 0x80, 0x00, 0x40, 0x00, 0x00 };
const unsigned char p16384[] = { 0x00, 0x80, 0x00, 0x40, 0x01, 0x00 };
static const unsigned char p0[] = { 0x00, 0x01, 0x00 };
static const unsigned char p62[] = { 0x00, 0x3F, 0x00 };
static const unsigned char p63[] = { 0x00, 0x40, 0x40, 0x00 };
static const unsigned char p64[] = { 0x00, 0x40, 0x41, 0x00 };
static const unsigned char p16382[] = { 0x00, 0x7F, 0xFF, 0x00 };
static const unsigned char p16383[] = { 0x00, 0x80, 0x00, 0x40, 0x00, 0x00 };
static const unsigned char p16384[] = { 0x00, 0x80, 0x00, 0x40, 0x01, 0x00 };
check_capsule_hdr(0, p0, sizeof(p0));
check_capsule_hdr(62, p62, sizeof(p62));
@ -139,7 +139,7 @@ static void test_capsule_sequential_decode(void)
CURLcode err;
size_t nread;
/* Two back-to-back 3-byte UDP capsules */
const unsigned char two_caps[] = {
static const unsigned char two_caps[] = {
0x00, 0x04, 0x00, 0x11, 0x22, 0x33, /* capsule 1: [0x11,0x22,0x33] */
0x00, 0x04, 0x00, 0xAA, 0xBB, 0xCC /* capsule 2: [0xAA,0xBB,0xCC] */
};
@ -186,13 +186,15 @@ static void test_capsule_decode_paths(void)
unsigned char out[8];
CURLcode err = CURLE_OK;
size_t nread;
const unsigned char invalid_type[] = { 0x01 };
const unsigned char partial_len[] = { 0x00, 0x40 };
const unsigned char invalid_context[] = { 0x00, 0x01, 0x01 };
const unsigned char invalid_caps_len[] = { 0x00, 0x00, 0x00 };
const unsigned char partial_payload[] = { 0x00, 0x04, 0x00, 0x11, 0x22 };
const unsigned char payload_3b[] = { 0x00, 0x04, 0x00, 0x11, 0x22, 0x33 };
const unsigned char payload_empty[] = { 0x00, 0x01, 0x00 };
static const unsigned char invalid_type[] = { 0x01 };
static const unsigned char partial_len[] = { 0x00, 0x40 };
static const unsigned char invalid_context[] = { 0x00, 0x01, 0x01 };
static const unsigned char invalid_caps_len[] = { 0x00, 0x00, 0x00 };
static const unsigned char partial_payload[] = { 0x00, 0x04, 0x00, 0x11,
0x22 };
static const unsigned char payload_3b[] = { 0x00, 0x04, 0x00,
0x11, 0x22, 0x33 };
static const unsigned char payload_empty[] = { 0x00, 0x01, 0x00 };
Curl_bufq_init2(&q, 32, 4, BUFQ_OPT_NONE);