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

@ -178,8 +178,8 @@ static int connack(FILE *dump, curl_socket_t fd)
MQTT_MSG_CONNACK, 0x02,
0x00, 0x00
};
ssize_t rc;
const char *label = "CONNACK";
ssize_t rc;
if(m_config.pingresp_as_connack) {
/* Send a PINGRESP (0xD0) with remaining_length=2 and payload
@ -260,8 +260,8 @@ static int disconnect(FILE *dump, curl_socket_t fd)
MQTT_MSG_DISCONNECT, 0x00,
0x00, 0x00 /* extra bytes for malformed variant */
};
size_t pktlen = 2;
const char *label = "DISCONNECT";
size_t pktlen = 2;
ssize_t rc;
if(m_config.disconnect_malformed) {
@ -638,8 +638,8 @@ static curl_socket_t mqttit(curl_socket_t fd)
}
}
else {
const char *def = "this is random payload yes yes it is";
publish(dump, fd, packet_id, topic, def, strlen(def));
static const char def[] = "this is random payload yes yes it is";
publish(dump, fd, packet_id, topic, def, CURL_CSTRLEN(def));
}
disconnect(dump, fd);
}

View file

@ -106,18 +106,18 @@ struct rtspd_httprequest {
#define END_OF_HEADERS "\r\n\r\n"
/* sent as reply to a QUIT */
static const char *docquit_rtsp = "HTTP/1.1 200 Goodbye" END_OF_HEADERS;
static const char docquit_rtsp[] = "HTTP/1.1 200 Goodbye" END_OF_HEADERS;
/* sent as reply to a CONNECT */
static const char *docconnect =
static const char docconnect[] =
"HTTP/1.1 200 Mighty fine indeed" END_OF_HEADERS;
/* sent as reply to a "bad" CONNECT */
static const char *docbadconnect =
static const char docbadconnect[] =
"HTTP/1.1 501 Forbidden you fool" END_OF_HEADERS;
/* send back this on HTTP 404 file not found */
static const char *doc404_HTTP =
static const char doc404_HTTP[] =
"HTTP/1.1 404 Not Found\r\n"
"Server: " RTSPDVERSION "\r\n"
"Connection: close\r\n"
@ -133,13 +133,13 @@ static const char *doc404_HTTP =
"</BODY></HTML>\n";
/* send back this on RTSP 404 file not found */
static const char *doc404_RTSP = "RTSP/1.0 404 Not Found\r\n"
static const char doc404_RTSP[] = "RTSP/1.0 404 Not Found\r\n"
"Server: " RTSPDVERSION
END_OF_HEADERS;
/* Default size to send away fake RTP data */
#define RTP_DATA_SIZE 12
static const char *RTP_DATA = "$_1234\n\0Rsdf";
static const char RTP_DATA[] = "$_1234\n\0Rsdf";
static int rtspd_ProcessRequest(struct rtspd_httprequest *req)
{
@ -267,16 +267,17 @@ static int rtspd_ProcessRequest(struct rtspd_httprequest *req)
logmsg("Found a reply-servercmd section!");
do {
rtp_size_err = 0;
if(!strncmp(CMD_AUTH_REQUIRED, ptr, strlen(CMD_AUTH_REQUIRED))) {
if(!strncmp(CMD_AUTH_REQUIRED, ptr,
CURL_CSTRLEN(CMD_AUTH_REQUIRED))) {
logmsg("instructed to require authorization header");
req->auth_req = TRUE;
}
else if(!strncmp(CMD_IDLE, ptr, strlen(CMD_IDLE))) {
else if(!strncmp(CMD_IDLE, ptr, CURL_CSTRLEN(CMD_IDLE))) {
logmsg("instructed to idle");
req->rcmd = RCMD_IDLE;
req->open = TRUE;
}
else if(!strncmp(CMD_STREAM, ptr, strlen(CMD_STREAM))) {
else if(!strncmp(CMD_STREAM, ptr, CURL_CSTRLEN(CMD_STREAM))) {
logmsg("instructed to stream");
req->rcmd = RCMD_STREAM;
}
@ -404,7 +405,7 @@ static int rtspd_ProcessRequest(struct rtspd_httprequest *req)
if(req->pipe)
/* we do have a full set, advance the checkindex to after the end of the
headers, for the pipelining case mostly */
req->checkindex += (end - line) + strlen(END_OF_HEADERS);
req->checkindex += (end - line) + CURL_CSTRLEN(END_OF_HEADERS);
/* **** Persistence ****
*
@ -427,7 +428,7 @@ static int rtspd_ProcessRequest(struct rtspd_httprequest *req)
ignore the content-length, we return as soon as all headers
have been received */
curl_off_t clen;
const char *p = line + strlen("Content-Length:");
const char *p = line + CURL_CSTRLEN("Content-Length:");
if(curlx_str_numblanks(&p, &clen)) {
/* this assumes that a zero Content-Length is valid */
logmsg("Found invalid '%s' in the request", line);
@ -442,7 +443,7 @@ static int rtspd_ProcessRequest(struct rtspd_httprequest *req)
break;
}
else if(!CURL_STRNICMP("Transfer-Encoding: chunked", line,
strlen("Transfer-Encoding: chunked"))) {
CURL_CSTRLEN("Transfer-Encoding: chunked"))) {
/* chunked data coming in */
chunked = TRUE;
}
@ -506,12 +507,12 @@ static int rtspd_ProcessRequest(struct rtspd_httprequest *req)
if(!req->pipe &&
req->open &&
req->prot_version >= 11 &&
req->reqbuf + req->offset > end + strlen(END_OF_HEADERS) &&
(!strncmp(req->reqbuf, "GET", strlen("GET")) ||
!strncmp(req->reqbuf, "HEAD", strlen("HEAD")))) {
req->reqbuf + req->offset > end + CURL_CSTRLEN(END_OF_HEADERS) &&
(!strncmp(req->reqbuf, "GET", CURL_CSTRLEN("GET")) ||
!strncmp(req->reqbuf, "HEAD", CURL_CSTRLEN("HEAD")))) {
/* If we have a persistent connection, HTTP version >= 1.1
and GET/HEAD request, enable pipelining. */
req->checkindex = (end - req->reqbuf) + strlen(END_OF_HEADERS);
req->checkindex = (end - req->reqbuf) + CURL_CSTRLEN(END_OF_HEADERS);
req->pipelining = TRUE;
}
@ -523,7 +524,7 @@ static int rtspd_ProcessRequest(struct rtspd_httprequest *req)
end = strstr(line, END_OF_HEADERS);
if(!end)
break;
req->checkindex += (end - line) + strlen(END_OF_HEADERS);
req->checkindex += (end - line) + CURL_CSTRLEN(END_OF_HEADERS);
req->pipe--;
}
@ -535,7 +536,8 @@ static int rtspd_ProcessRequest(struct rtspd_httprequest *req)
return 1; /* done */
if(req->cl > 0) {
if(req->cl <= req->offset - (end - req->reqbuf) - strlen(END_OF_HEADERS))
if(req->cl <= req->offset - (end - req->reqbuf) -
CURL_CSTRLEN(END_OF_HEADERS))
return 1; /* done */
else
return 0; /* not complete yet */

View file

@ -102,9 +102,9 @@ static void socksd_resetdefaults(void)
curlx_strcopy(s_config.addr, sizeof(s_config.addr),
CONFIG_ADDR, strlen(CONFIG_ADDR));
curlx_strcopy(s_config.user, sizeof(s_config.user),
"user", strlen("user"));
"user", CURL_CSTRLEN("user"));
curlx_strcopy(s_config.password, sizeof(s_config.password),
"password", strlen("password"));
"password", CURL_CSTRLEN("password"));
}
static void socksd_getconfig(void)

View file

@ -141,10 +141,10 @@ static const char *cmdfile = "log/server.cmd";
static const char *end_of_headers = END_OF_HEADERS;
/* sent as reply to a QUIT */
static const char *docquit_sws = "HTTP/1.1 200 Goodbye" END_OF_HEADERS;
static const char docquit_sws[] = "HTTP/1.1 200 Goodbye" END_OF_HEADERS;
/* send back this on 404 file not found */
static const char *doc404 =
static const char doc404[] =
"HTTP/1.1 404 Not Found\r\n"
"Server: " SWSVERSION "\r\n"
"Connection: close\r\n"
@ -262,29 +262,29 @@ static int sws_parse_servercmd(struct sws_httprequest *req)
while(cmd && cmdsize) {
const char *check;
if(!strncmp(CMD_AUTH_REQUIRED, cmd, strlen(CMD_AUTH_REQUIRED))) {
if(!strncmp(CMD_AUTH_REQUIRED, cmd, CURL_CSTRLEN(CMD_AUTH_REQUIRED))) {
logmsg("instructed to require authorization header");
req->auth_req = TRUE;
}
else if(!strncmp(CMD_IDLE, cmd, strlen(CMD_IDLE))) {
else if(!strncmp(CMD_IDLE, cmd, CURL_CSTRLEN(CMD_IDLE))) {
logmsg("instructed to idle");
req->rcmd = RCMD_IDLE;
req->open = TRUE;
}
else if(!strncmp(CMD_STREAM, cmd, strlen(CMD_STREAM))) {
else if(!strncmp(CMD_STREAM, cmd, CURL_CSTRLEN(CMD_STREAM))) {
logmsg("instructed to stream");
req->rcmd = RCMD_STREAM;
}
else if(!strncmp(CMD_CONNECTIONMONITOR, cmd,
strlen(CMD_CONNECTIONMONITOR))) {
CURL_CSTRLEN(CMD_CONNECTIONMONITOR))) {
logmsg("enabled connection monitoring");
req->connmon = TRUE;
}
else if(!strncmp(CMD_UPGRADE, cmd, strlen(CMD_UPGRADE))) {
else if(!strncmp(CMD_UPGRADE, cmd, CURL_CSTRLEN(CMD_UPGRADE))) {
logmsg("enabled upgrade");
req->upgrade = TRUE;
}
else if(!strncmp(CMD_SWSCLOSE, cmd, strlen(CMD_SWSCLOSE))) {
else if(!strncmp(CMD_SWSCLOSE, cmd, CURL_CSTRLEN(CMD_SWSCLOSE))) {
logmsg("swsclose: close this connection after response");
req->close = TRUE;
}
@ -292,7 +292,7 @@ static int sws_parse_servercmd(struct sws_httprequest *req)
logmsg("instructed to skip this number of bytes %d", num);
req->skip = num;
}
else if(!strncmp(CMD_NOEXPECT, cmd, strlen(CMD_NOEXPECT))) {
else if(!strncmp(CMD_NOEXPECT, cmd, CURL_CSTRLEN(CMD_NOEXPECT))) {
logmsg("instructed to reject Expect: 100-continue");
req->noexpect = TRUE;
}
@ -591,7 +591,7 @@ static int sws_ProcessRequest(struct sws_httprequest *req)
ignore the content-length, we return as soon as all headers
have been received */
curl_off_t clen;
const char *p = line + strlen("Content-Length:");
const char *p = line + CURL_CSTRLEN("Content-Length:");
if(curlx_str_numblanks(&p, &clen)) {
/* this assumes that a zero Content-Length is valid */
logmsg("Found invalid '%s' in the request", line);
@ -608,12 +608,13 @@ static int sws_ProcessRequest(struct sws_httprequest *req)
logmsg("... but going to abort after %zu bytes", req->cl);
}
else if(!CURL_STRNICMP("Transfer-Encoding: chunked", line,
strlen("Transfer-Encoding: chunked"))) {
CURL_CSTRLEN("Transfer-Encoding: chunked"))) {
/* chunked data coming in */
chunked = TRUE;
}
else if(req->noexpect && !CURL_STRNICMP("Expect: 100-continue", line,
strlen("Expect: 100-continue"))) {
else if(req->noexpect &&
!CURL_STRNICMP("Expect: 100-continue", line,
CURL_CSTRLEN("Expect: 100-continue"))) {
if(req->cl)
req->cl = 0;
req->skipall = TRUE;
@ -709,8 +710,8 @@ static int sws_ProcessRequest(struct sws_httprequest *req)
req->prot_version >= 11 &&
req->reqbuf + req->offset > end + strlen(end_of_headers) &&
!req->cl &&
(!strncmp(req->reqbuf, "GET", strlen("GET")) ||
!strncmp(req->reqbuf, "HEAD", strlen("HEAD")))) {
(!strncmp(req->reqbuf, "GET", CURL_CSTRLEN("GET")) ||
!strncmp(req->reqbuf, "HEAD", CURL_CSTRLEN("HEAD")))) {
/* If we have a persistent connection, HTTP version >= 1.1
and GET/HEAD request, enable pipelining. */
req->checkindex = (end - req->reqbuf) + strlen(end_of_headers);
@ -771,10 +772,10 @@ static int sws_send_doc(curl_socket_t sock, struct sws_httprequest *req)
case RCMD_STREAM: {
static const char streamthis[] = "a string to stream 01234567890\n";
for(;;) {
written = swrite(sock, streamthis, sizeof(streamthis) - 1);
written = swrite(sock, streamthis, CURL_CSTRLEN(streamthis));
if(got_exit_signal)
return -1;
if(written != (ssize_t)(sizeof(streamthis) - 1)) {
if(written != (ssize_t)CURL_CSTRLEN(streamthis)) {
logmsg("Stopped streaming");
break;
}
@ -2325,8 +2326,9 @@ static int test_sws(int argc, const char *argv[])
logmsg("====> Client disconnect %d", req->connmon);
if(req->connmon) {
const char *keepopen = "[DISCONNECT]\n";
storerequest(keepopen, strlen(keepopen), REQUEST_DUMP_FILENAME);
static const char keepopen[] = "[DISCONNECT]\n";
storerequest(keepopen, CURL_CSTRLEN(keepopen),
REQUEST_DUMP_FILENAME);
}
if(!req->open)