clang-tidy: avoid/silence bugprone-not-null-terminated-result

Prefer `sizeof()` over `strlen()`, static const variables over macros.
Add a couple of `NOLINT`s to silence false positives.

Also sync similar code patterns between libtests.

Cherry-picked from #20720

Closes #20723
This commit is contained in:
Viktor Szakats 2026-02-25 18:32:14 +01:00
parent 39542f0993
commit 725c5609ae
No known key found for this signature in database
14 changed files with 57 additions and 51 deletions

View file

@ -54,7 +54,7 @@ static size_t t1520_read_cb(char *ptr, size_t size, size_t nmemb, void *userp)
if(data) {
size_t len = strlen(data);
memcpy(ptr, data, len);
memcpy(ptr, data, len); /* NOLINT(bugprone-not-null-terminated-result) */
upload_ctx->lines_read++;
return len;

View file

@ -30,17 +30,18 @@
#include "first.h"
static const char t1525_testdata[] = "Hello Cloud!\n";
static const char t1525_data[] = "Hello Cloud!\n";
static size_t const t1525_datalen = sizeof(t1525_data) - 1;
static size_t t1525_read_cb(char *ptr, size_t size, size_t nmemb, void *stream)
{
size_t amount = nmemb * size; /* Total bytes curl wants */
if(amount < strlen(t1525_testdata)) {
return strlen(t1525_testdata);
if(amount < t1525_datalen) {
return t1525_datalen;
}
(void)stream;
memcpy(ptr, t1525_testdata, strlen(t1525_testdata));
return strlen(t1525_testdata);
memcpy(ptr, t1525_data, t1525_datalen);
return t1525_datalen;
}
static CURLcode test_lib1525(const char *URL)
@ -81,7 +82,7 @@ static CURLcode test_lib1525(const char *URL)
test_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
test_setopt(curl, CURLOPT_READFUNCTION, t1525_read_cb);
test_setopt(curl, CURLOPT_HTTPPROXYTUNNEL, 1L);
test_setopt(curl, CURLOPT_INFILESIZE, (long)strlen(t1525_testdata));
test_setopt(curl, CURLOPT_INFILESIZE, (long)t1525_datalen);
result = curl_easy_perform(curl);

View file

@ -29,17 +29,18 @@
#include "first.h"
static const char t1526_testdata[] = "Hello Cloud!\n";
static const char t1526_data[] = "Hello Cloud!\n";
static size_t const t1526_datalen = sizeof(t1526_data) - 1;
static size_t t1526_read_cb(char *ptr, size_t size, size_t nmemb, void *stream)
{
size_t amount = nmemb * size; /* Total bytes curl wants */
if(amount < strlen(t1526_testdata)) {
return strlen(t1526_testdata);
if(amount < t1526_datalen) {
return t1526_datalen;
}
(void)stream;
memcpy(ptr, t1526_testdata, strlen(t1526_testdata));
return strlen(t1526_testdata);
memcpy(ptr, t1526_data, t1526_datalen);
return t1526_datalen;
}
static CURLcode test_lib1526(const char *URL)
@ -85,7 +86,7 @@ static CURLcode test_lib1526(const char *URL)
test_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
test_setopt(curl, CURLOPT_READFUNCTION, t1526_read_cb);
test_setopt(curl, CURLOPT_HTTPPROXYTUNNEL, 1L);
test_setopt(curl, CURLOPT_INFILESIZE, (long)strlen(t1526_testdata));
test_setopt(curl, CURLOPT_INFILESIZE, (long)t1526_datalen);
result = curl_easy_perform(curl);

View file

@ -29,17 +29,18 @@
#include "first.h"
static const char t1527_testdata[] = "Hello Cloud!\n";
static const char t1527_data[] = "Hello Cloud!\n";
static size_t const t1527_datalen = sizeof(t1527_data) - 1;
static size_t t1527_read_cb(char *ptr, size_t size, size_t nmemb, void *stream)
{
size_t amount = nmemb * size; /* Total bytes curl wants */
if(amount < strlen(t1527_testdata)) {
return strlen(t1527_testdata);
if(amount < t1527_datalen) {
return t1527_datalen;
}
(void)stream;
memcpy(ptr, t1527_testdata, strlen(t1527_testdata));
return strlen(t1527_testdata);
memcpy(ptr, t1527_data, t1527_datalen);
return t1527_datalen;
}
static CURLcode test_lib1527(const char *URL)
@ -82,7 +83,7 @@ static CURLcode test_lib1527(const char *URL)
test_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
test_setopt(curl, CURLOPT_READFUNCTION, t1527_read_cb);
test_setopt(curl, CURLOPT_HTTPPROXYTUNNEL, 1L);
test_setopt(curl, CURLOPT_INFILESIZE, (long)strlen(t1527_testdata));
test_setopt(curl, CURLOPT_INFILESIZE, (long)t1527_datalen);
test_setopt(curl, CURLOPT_HEADEROPT, CURLHEADER_UNIFIED);
result = curl_easy_perform(curl);

View file

@ -25,8 +25,8 @@
static CURLcode test_lib1531(const char *URL)
{
static char const testData[] = ".abc\0xyz";
static curl_off_t const testDataSize = sizeof(testData) - 1;
static char const testdata[] = ".abc\0xyz";
static curl_off_t const testdatalen = sizeof(testdata) - 1;
CURL *curl;
CURLM *multi;
@ -50,8 +50,8 @@ static CURLcode test_lib1531(const char *URL)
/* set the options (I left out a few, you get the point anyway) */
curl_easy_setopt(curl, CURLOPT_URL, URL);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE, testDataSize);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, testData);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE, testdatalen);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, testdata);
/* we start some action by calling perform right away */
curl_multi_perform(multi, &still_running);

View file

@ -23,17 +23,18 @@
***************************************************************************/
#include "first.h"
static char t1576_testdata[] = "request indicates that the client, which made";
static char t1576_data[] = "request indicates that the client, which made";
static size_t const t1576_datalen = sizeof(t1576_data) - 1;
static size_t t1576_read_cb(char *ptr, size_t size, size_t nmemb, void *stream)
{
size_t amount = nmemb * size; /* Total bytes curl wants */
if(amount < strlen(t1576_testdata)) {
return strlen(t1576_testdata);
if(amount < t1576_datalen) {
return t1576_datalen;
}
(void)stream;
memcpy(ptr, t1576_testdata, strlen(t1576_testdata));
return strlen(t1576_testdata);
memcpy(ptr, t1576_data, t1576_datalen);
return t1576_datalen;
}
static int t1576_seek_callback(void *ptr, curl_off_t offset, int origin)
@ -69,7 +70,7 @@ static CURLcode test_lib1576(const char *URL)
test_setopt(curl, CURLOPT_UPLOAD, 1L);
test_setopt(curl, CURLOPT_READFUNCTION, t1576_read_cb);
test_setopt(curl, CURLOPT_SEEKFUNCTION, t1576_seek_callback);
test_setopt(curl, CURLOPT_INFILESIZE, (long)strlen(t1576_testdata));
test_setopt(curl, CURLOPT_INFILESIZE, (long)t1576_datalen);
test_setopt(curl, CURLOPT_CUSTOMREQUEST, "CURL");
if(testnum == 1578 || testnum == 1580) {

View file

@ -30,17 +30,17 @@ struct t1662_WriteThis {
static size_t t1662_read_cb(char *ptr, size_t size, size_t nmemb, void *userp)
{
static const char testdata[] = "mooaaa";
static size_t const testdatalen = sizeof(testdata) - 1;
struct t1662_WriteThis *pooh = (struct t1662_WriteThis *)userp;
size_t len = strlen(testdata);
if(size * nmemb < len)
if(size * nmemb < testdatalen)
return 0;
if(pooh->sizeleft) {
memcpy(ptr, testdata, strlen(testdata));
memcpy(ptr, testdata, testdatalen);
pooh->sizeleft = 0;
return len;
return testdatalen;
}
return 0; /* no more data left to deliver */

View file

@ -51,7 +51,7 @@ static size_t t510_read_cb(char *ptr, size_t size, size_t nmemb, void *userp)
curl_mfprintf(stderr, "read buffer is too small to run test\n");
return 0;
}
memcpy(ptr, data, len);
memcpy(ptr, data, len); /* NOLINT(bugprone-not-null-terminated-result) */
pooh->counter++; /* advance pointer */
return len;
}

View file

@ -29,7 +29,7 @@
#include "first.h"
static const char t547_uploadthis[] = "this is the blurb we want to upload\n";
#define T547_DATALEN (sizeof(t547_uploadthis) - 1)
static size_t const t547_datalen = sizeof(t547_uploadthis) - 1;
static size_t t547_read_cb(char *ptr, size_t size, size_t nmemb, void *clientp)
{
@ -42,10 +42,10 @@ static size_t t547_read_cb(char *ptr, size_t size, size_t nmemb, void *clientp)
}
(*counter)++; /* bump */
if(size * nmemb >= T547_DATALEN) {
if(size * nmemb >= t547_datalen) {
curl_mfprintf(stderr, "READ!\n");
memcpy(ptr, t547_uploadthis, T547_DATALEN);
return T547_DATALEN;
memcpy(ptr, t547_uploadthis, t547_datalen);
return t547_datalen;
}
curl_mfprintf(stderr, "READ NOT FINE!\n");
return 0;
@ -96,7 +96,7 @@ static CURLcode test_lib547(const char *URL)
test_setopt(curl, CURLOPT_READDATA, &counter);
/* We CANNOT do the POST fine without setting the size (or choose
chunked)! */
test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)T547_DATALEN);
test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)t547_datalen);
}
test_setopt(curl, CURLOPT_POST, 1L);
test_setopt(curl, CURLOPT_PROXY, libtest_arg2);

View file

@ -33,7 +33,7 @@
#include "first.h"
static const char t555_uploadthis[] = "this is the blurb we want to upload\n";
#define T555_DATALEN (sizeof(t555_uploadthis) - 1)
static size_t const t555_datalen = sizeof(t555_uploadthis) - 1;
static size_t t555_read_cb(char *ptr, size_t size, size_t nmemb, void *clientp)
{
@ -46,10 +46,10 @@ static size_t t555_read_cb(char *ptr, size_t size, size_t nmemb, void *clientp)
}
(*counter)++; /* bump */
if(size * nmemb >= T555_DATALEN) {
if(size * nmemb >= t555_datalen) {
curl_mfprintf(stderr, "READ!\n");
memcpy(ptr, t555_uploadthis, T555_DATALEN);
return T555_DATALEN;
memcpy(ptr, t555_uploadthis, t555_datalen);
return t555_datalen;
}
curl_mfprintf(stderr, "READ NOT FINE!\n");
return 0;
@ -92,7 +92,7 @@ static CURLcode test_lib555(const char *URL)
easy_setopt(curl, CURLOPT_READDATA, &counter);
/* We CANNOT do the POST fine without setting the size (or choose
chunked)! */
easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)T555_DATALEN);
easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)t555_datalen);
easy_setopt(curl, CURLOPT_POST, 1L);
easy_setopt(curl, CURLOPT_PROXY, libtest_arg2);

View file

@ -91,7 +91,7 @@ static size_t t579_read_cb(char *ptr, size_t size, size_t nmemb, void *userp)
if(data) {
size_t len = strlen(data);
memcpy(ptr, data, len);
memcpy(ptr, data, len); /* NOLINT(bugprone-not-null-terminated-result) */
pooh->counter++; /* advance pointer */
return len;
}

View file

@ -31,15 +31,15 @@ static size_t write_757(char *ptr, size_t size, size_t nmemb, void *userdata)
return size * nmemb;
}
#define DATA757 "<title>fun-times</title>"
#define DATALEN (sizeof(DATA757) - 1)
static const char t757_data[] = "<title>fun-times</title>";
static size_t const t757_datalen = sizeof(t757_data) - 1;
static size_t read_757(char *buffer, size_t size, size_t nitems, void *arg)
{
(void)arg;
if((size * nitems) >= DATALEN) {
memcpy(buffer, DATA757, DATALEN);
return DATALEN;
if((size * nitems) >= t757_datalen) {
memcpy(buffer, t757_data, t757_datalen);
return t757_datalen;
}
return 0;
}
@ -83,7 +83,7 @@ static CURLcode test_lib757(const char *URL)
/* Build the first mime structure. */
mime1 = curl_mime_init(curl);
part = curl_mime_addpart(mime1);
curl_mime_data_cb(part, DATALEN, read_757, seek_757, NULL, NULL);
curl_mime_data_cb(part, t757_datalen, read_757, seek_757, NULL, NULL);
curl_mime_type(part, "text/html");
curl_mime_name(part, "data");

View file

@ -351,6 +351,7 @@ static int publish(FILE *dump,
packet[1 + encodedlen] = (unsigned char)(topiclen >> 8);
packet[2 + encodedlen] = (unsigned char)(topiclen & 0xff);
/* NOLINTNEXTLINE(bugprone-not-null-terminated-result) */
memcpy(&packet[3 + encodedlen], topic, topiclen);
payloadindex = 3 + topiclen + encodedlen;

View file

@ -317,6 +317,7 @@ static int rtspd_ProcessRequest(struct rtspd_httprequest *req)
/* Fill it with junk data */
for(i = 0; i < rtp_size; i += RTP_DATA_SIZE) {
/* NOLINTNEXTLINE(bugprone-not-null-terminated-result) */
memcpy(rtp_scratch + 4 + i, RTP_DATA, RTP_DATA_SIZE);
}