Removed the unsafe nxt_memcmp() wrapper for memcmp(3).

The casts are unnecessary, since memcmp(3)'s arguments are 'void *'.
It might have been necessary in the times of K&R, where 'void *' didn't
exist.  Nowadays, it's unnecessary, and _very_ unsafe, since casts can
hide all classes of bugs by silencing most compiler warnings.

The changes from nxt_memcmp() to memcmp(3) were scripted:

$ find src/ -type f \
  | grep '\.[ch]$' \
  | xargs sed -i 's/nxt_memcmp/memcmp/'

Reviewed-by: Andrew Clayton <a.clayton@nginx.com>
Signed-off-by: Alejandro Colomar <alx@nginx.com>
This commit is contained in:
Alejandro Colomar
2022-11-02 21:45:40 +01:00
parent a03274456b
commit 1b05161107
18 changed files with 42 additions and 46 deletions

View File

@@ -366,7 +366,7 @@ nxt_discovery_module(nxt_task_t *task, nxt_mp_t *mp, nxt_array_t *modules,
&app->type, app->version, name); &app->type, app->version, name);
if (app->compat_length != sizeof(compat) if (app->compat_length != sizeof(compat)
|| nxt_memcmp(app->compat, compat, sizeof(compat)) != 0) || memcmp(app->compat, compat, sizeof(compat)) != 0)
{ {
nxt_log(task, NXT_LOG_NOTICE, "incompatible module %s", name); nxt_log(task, NXT_LOG_NOTICE, "incompatible module %s", name);

View File

@@ -1389,7 +1389,7 @@ nxt_conf_json_parse_value(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start,
case 't': case 't':
if (nxt_fast_path(end - start >= 4 if (nxt_fast_path(end - start >= 4
&& nxt_memcmp(start, "true", 4) == 0)) && memcmp(start, "true", 4) == 0))
{ {
value->u.boolean = 1; value->u.boolean = 1;
value->type = NXT_CONF_VALUE_BOOLEAN; value->type = NXT_CONF_VALUE_BOOLEAN;
@@ -1401,7 +1401,7 @@ nxt_conf_json_parse_value(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start,
case 'f': case 'f':
if (nxt_fast_path(end - start >= 5 if (nxt_fast_path(end - start >= 5
&& nxt_memcmp(start, "false", 5) == 0)) && memcmp(start, "false", 5) == 0))
{ {
value->u.boolean = 0; value->u.boolean = 0;
value->type = NXT_CONF_VALUE_BOOLEAN; value->type = NXT_CONF_VALUE_BOOLEAN;
@@ -1413,7 +1413,7 @@ nxt_conf_json_parse_value(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start,
case 'n': case 'n':
if (nxt_fast_path(end - start >= 4 if (nxt_fast_path(end - start >= 4
&& nxt_memcmp(start, "null", 4) == 0)) && memcmp(start, "null", 4) == 0))
{ {
value->type = NXT_CONF_VALUE_NULL; value->type = NXT_CONF_VALUE_NULL;
return start + 4; return start + 4;

View File

@@ -1263,7 +1263,7 @@ nxt_controller_process_config(nxt_task_t *task, nxt_controller_request_t *req,
/* Skip UTF-8 BOM. */ /* Skip UTF-8 BOM. */
if (nxt_buf_mem_used_size(mbuf) >= 3 if (nxt_buf_mem_used_size(mbuf) >= 3
&& nxt_memcmp(mbuf->pos, "\xEF\xBB\xBF", 3) == 0) && memcmp(mbuf->pos, "\xEF\xBB\xBF", 3) == 0)
{ {
mbuf->pos += 3; mbuf->pos += 3;
} }
@@ -1940,7 +1940,7 @@ nxt_controller_process_control(nxt_task_t *task,
} }
if (!nxt_str_start(path, "applications/", 13) if (!nxt_str_start(path, "applications/", 13)
|| nxt_memcmp(path->start + path->length - 8, "/restart", 8) != 0) || memcmp(path->start + path->length - 8, "/restart", 8) != 0)
{ {
goto not_found; goto not_found;
} }

View File

@@ -78,7 +78,7 @@ nxt_strerror_start(void)
if (length == 0 /* HP-UX empty strings. */ if (length == 0 /* HP-UX empty strings. */
|| nxt_errno == NXT_EINVAL || nxt_errno == NXT_EINVAL
|| nxt_memcmp(msg, "Unknown error", 13) == 0) || memcmp(msg, "Unknown error", 13) == 0)
{ {
invalid++; invalid++;
continue; continue;
@@ -86,8 +86,8 @@ nxt_strerror_start(void)
#if (NXT_AIX) #if (NXT_AIX)
if (nxt_memcmp(msg, "Error ", 6) == 0 if (memcmp(msg, "Error ", 6) == 0
&& nxt_memcmp(msg + length - 10, " occurred.", 9) == 0) && memcmp(msg + length - 10, " occurred.", 9) == 0)
{ {
invalid++; invalid++;
continue; continue;

View File

@@ -823,7 +823,7 @@ nxt_h1p_transfer_encoding(void *ctx, nxt_http_field_t *field, uintptr_t data)
field->hopbyhop = 1; field->hopbyhop = 1;
if (field->value_length == 7 if (field->value_length == 7
&& nxt_memcmp(field->value, "chunked", 7) == 0) && memcmp(field->value, "chunked", 7) == 0)
{ {
te = NXT_HTTP_TE_CHUNKED; te = NXT_HTTP_TE_CHUNKED;
@@ -2594,7 +2594,7 @@ nxt_h1p_peer_header_parse(nxt_http_peer_t *peer, nxt_buf_mem_t *bm)
p = bm->pos; p = bm->pos;
if (nxt_slow_path(nxt_memcmp(p, "HTTP/1.", 7) != 0 if (nxt_slow_path(memcmp(p, "HTTP/1.", 7) != 0
|| (p[7] != '0' && p[7] != '1'))) || (p[7] != '0' && p[7] != '1')))
{ {
return NXT_ERROR; return NXT_ERROR;
@@ -2868,7 +2868,7 @@ nxt_h1p_peer_transfer_encoding(void *ctx, nxt_http_field_t *field,
field->skip = 1; field->skip = 1;
if (field->value_length == 7 if (field->value_length == 7
&& nxt_memcmp(field->value, "chunked", 7) == 0) && memcmp(field->value, "chunked", 7) == 0)
{ {
r->peer->proto.h1->chunked = 1; r->peer->proto.h1->chunked = 1;
} }

View File

@@ -357,7 +357,7 @@ space_after_target:
} while (*p == ' '); } while (*p == ' ');
if (nxt_memcmp(p, "HTTP/", nxt_min(end - p, 5)) == 0) { if (memcmp(p, "HTTP/", nxt_min(end - p, 5)) == 0) {
switch (end - p) { switch (end - p) {
case 8: case 8:
@@ -412,7 +412,7 @@ space_after_target:
if (nxt_fast_path(ver.ui64 == http11.ui64 if (nxt_fast_path(ver.ui64 == http11.ui64
|| ver.ui64 == http10.ui64 || ver.ui64 == http10.ui64
|| (nxt_memcmp(ver.str, "HTTP/1.", 7) == 0 || (memcmp(ver.str, "HTTP/1.", 7) == 0
&& ver.s.minor >= '0' && ver.s.minor <= '9'))) && ver.s.minor >= '0' && ver.s.minor <= '9')))
{ {
rp->version.ui64 = ver.ui64; rp->version.ui64 = ver.ui64;
@@ -464,7 +464,7 @@ space_after_target:
return nxt_http_parse_field_name(rp, pos, end); return nxt_http_parse_field_name(rp, pos, end);
} }
if (nxt_memcmp(ver.s.prefix, "HTTP/", 5) == 0 if (memcmp(ver.s.prefix, "HTTP/", 5) == 0
&& ver.s.major >= '0' && ver.s.major <= '9' && ver.s.major >= '0' && ver.s.major <= '9'
&& ver.s.point == '.' && ver.s.point == '.'
&& ver.s.minor >= '0' && ver.s.minor <= '9') && ver.s.minor >= '0' && ver.s.minor <= '9')

View File

@@ -1740,15 +1740,15 @@ nxt_http_route_addr_pattern_match(nxt_http_route_addr_pattern_t *p,
break; break;
case NXT_HTTP_ROUTE_ADDR_EXACT: case NXT_HTTP_ROUTE_ADDR_EXACT:
match = (nxt_memcmp(&sin->sin_addr, &p->addr.v4.start, match = (memcmp(&sin->sin_addr, &p->addr.v4.start,
sizeof(struct in_addr)) sizeof(struct in_addr))
== 0); == 0);
break; break;
case NXT_HTTP_ROUTE_ADDR_RANGE: case NXT_HTTP_ROUTE_ADDR_RANGE:
match = (nxt_memcmp(&sin->sin_addr, &p->addr.v4.start, match = (memcmp(&sin->sin_addr, &p->addr.v4.start,
sizeof(struct in_addr)) >= 0 sizeof(struct in_addr)) >= 0
&& nxt_memcmp(&sin->sin_addr, &p->addr.v4.end, && memcmp(&sin->sin_addr, &p->addr.v4.end,
sizeof(struct in_addr)) <= 0); sizeof(struct in_addr)) <= 0);
break; break;
@@ -1786,15 +1786,15 @@ nxt_http_route_addr_pattern_match(nxt_http_route_addr_pattern_t *p,
break; break;
case NXT_HTTP_ROUTE_ADDR_EXACT: case NXT_HTTP_ROUTE_ADDR_EXACT:
match = (nxt_memcmp(&sin6->sin6_addr, &p->addr.v6.start, match = (memcmp(&sin6->sin6_addr, &p->addr.v6.start,
sizeof(struct in6_addr)) sizeof(struct in6_addr))
== 0); == 0);
break; break;
case NXT_HTTP_ROUTE_ADDR_RANGE: case NXT_HTTP_ROUTE_ADDR_RANGE:
match = (nxt_memcmp(&sin6->sin6_addr, &p->addr.v6.start, match = (memcmp(&sin6->sin6_addr, &p->addr.v6.start,
sizeof(struct in6_addr)) >= 0 sizeof(struct in6_addr)) >= 0
&& nxt_memcmp(&sin6->sin6_addr, &p->addr.v6.end, && memcmp(&sin6->sin6_addr, &p->addr.v6.end,
sizeof(struct in6_addr)) <= 0); sizeof(struct in6_addr)) <= 0);
break; break;
@@ -1937,7 +1937,7 @@ nxt_http_route_test_argument(nxt_http_request_t *r,
if (rule->u.name.hash == nv->hash if (rule->u.name.hash == nv->hash
&& rule->u.name.length == nv->name_length && rule->u.name.length == nv->name_length
&& nxt_memcmp(rule->u.name.start, nv->name, nv->name_length) == 0) && memcmp(rule->u.name.start, nv->name, nv->name_length) == 0)
{ {
ret = nxt_http_route_test_rule(r, rule, nv->value, ret = nxt_http_route_test_rule(r, rule, nv->value,
nv->value_length); nv->value_length);
@@ -2015,7 +2015,7 @@ nxt_http_route_test_cookie(nxt_http_request_t *r,
if (rule->u.name.hash == nv->hash if (rule->u.name.hash == nv->hash
&& rule->u.name.length == nv->name_length && rule->u.name.length == nv->name_length
&& nxt_memcmp(rule->u.name.start, nv->name, nv->name_length) == 0) && memcmp(rule->u.name.start, nv->name, nv->name_length) == 0)
{ {
ret = nxt_http_route_test_rule(r, rule, nv->value, ret = nxt_http_route_test_rule(r, rule, nv->value,
nv->value_length); nv->value_length);
@@ -2158,7 +2158,7 @@ nxt_http_route_memcmp(u_char *start, u_char *test, size_t test_length,
nxt_int_t n; nxt_int_t n;
if (case_sensitive) { if (case_sensitive) {
n = nxt_memcmp(start, test, test_length); n = memcmp(start, test, test_length);
} else { } else {
n = nxt_memcasecmp(start, test, test_length); n = nxt_memcasecmp(start, test, test_length);

View File

@@ -115,7 +115,7 @@ nxt_http_route_addr_pattern_parse(nxt_mp_t *mp,
return NXT_ADDR_PATTERN_FORMAT_ERROR; return NXT_ADDR_PATTERN_FORMAT_ERROR;
} }
if (nxt_slow_path(nxt_memcmp(&inet6->start, &inet6->end, if (nxt_slow_path(memcmp(&inet6->start, &inet6->end,
sizeof(struct in6_addr)) > 0)) sizeof(struct in6_addr)) > 0))
{ {
return NXT_ADDR_PATTERN_RANGE_OVERLAP_ERROR; return NXT_ADDR_PATTERN_RANGE_OVERLAP_ERROR;
@@ -223,7 +223,7 @@ nxt_http_route_addr_pattern_parse(nxt_mp_t *mp,
return NXT_ADDR_PATTERN_FORMAT_ERROR; return NXT_ADDR_PATTERN_FORMAT_ERROR;
} }
if (nxt_slow_path(nxt_memcmp(&inet->start, &inet->end, if (nxt_slow_path(memcmp(&inet->start, &inet->end,
sizeof(struct in_addr)) > 0)) sizeof(struct in_addr)) > 0))
{ {
return NXT_ADDR_PATTERN_RANGE_OVERLAP_ERROR; return NXT_ADDR_PATTERN_RANGE_OVERLAP_ERROR;

View File

@@ -421,7 +421,7 @@ nxt_http_var_arg(nxt_task_t *task, nxt_str_t *str, void *ctx, uint16_t field)
if (vf->hash == nv->hash if (vf->hash == nv->hash
&& vf->name.length == nv->name_length && vf->name.length == nv->name_length
&& nxt_memcmp(vf->name.start, nv->name, nv->name_length) == 0) && memcmp(vf->name.start, nv->name, nv->name_length) == 0)
{ {
str->start = nv->value; str->start = nv->value;
str->length = nv->value_length; str->length = nv->value_length;
@@ -499,7 +499,7 @@ nxt_http_var_cookie(nxt_task_t *task, nxt_str_t *str, void *ctx, uint16_t field)
if (vf->hash == nv->hash if (vf->hash == nv->hash
&& vf->name.length == nv->name_length && vf->name.length == nv->name_length
&& nxt_memcmp(vf->name.start, nv->name, nv->name_length) == 0) && memcmp(vf->name.start, nv->name, nv->name_length) == 0)
{ {
str->start = nv->value; str->start = nv->value;
str->length = nv->value_length; str->length = nv->value_length;

View File

@@ -780,7 +780,7 @@ nxt_tls_ticket_key_callback(SSL *s, unsigned char *name, unsigned char *iv,
/* decrypt session ticket */ /* decrypt session ticket */
do { do {
if (nxt_memcmp(name, ticket[i].name, 16) == 0) { if (memcmp(name, ticket[i].name, 16) == 0) {
goto found; goto found;
} }

View File

@@ -1036,7 +1036,7 @@ nxt_php_dynamic_request(nxt_php_run_ctx_t *ctx, nxt_unit_request_t *r)
script_name = *ctx->index; script_name = *ctx->index;
} else if (path.length < 4 } else if (path.length < 4
|| nxt_memcmp(path.start + (path.length - 4), ".php", 4) != 0) || memcmp(path.start + (path.length - 4), ".php", 4) != 0)
{ {
char tpath[PATH_MAX]; char tpath[PATH_MAX];
nxt_int_t ec; nxt_int_t ec;

View File

@@ -382,7 +382,7 @@ nxt_sockaddr_cmp(nxt_sockaddr_t *sa1, nxt_sockaddr_t *sa2)
return 0; return 0;
} }
if (nxt_memcmp(&sa1->u.sockaddr_in6.sin6_addr, if (memcmp(&sa1->u.sockaddr_in6.sin6_addr,
&sa2->u.sockaddr_in6.sin6_addr, 16) &sa2->u.sockaddr_in6.sin6_addr, 16)
!= 0) != 0)
{ {
@@ -401,7 +401,7 @@ nxt_sockaddr_cmp(nxt_sockaddr_t *sa1, nxt_sockaddr_t *sa2)
length = sa1->socklen - offsetof(struct sockaddr_un, sun_path); length = sa1->socklen - offsetof(struct sockaddr_un, sun_path);
if (nxt_memcmp(&sa1->u.sockaddr_un.sun_path, if (memcmp(&sa1->u.sockaddr_un.sun_path,
&sa2->u.sockaddr_un.sun_path, length) &sa2->u.sockaddr_un.sun_path, length)
!= 0) != 0)
{ {
@@ -550,7 +550,7 @@ nxt_sockaddr_parse_optport(nxt_mp_t *mp, nxt_str_t *addr)
return NULL; return NULL;
} }
if (addr->length > 6 && nxt_memcmp(addr->start, "unix:", 5) == 0) { if (addr->length > 6 && memcmp(addr->start, "unix:", 5) == 0) {
sa = nxt_sockaddr_unix_parse(mp, addr); sa = nxt_sockaddr_unix_parse(mp, addr);
} else if (addr->start[0] == '[' || nxt_inet6_probe(addr)) { } else if (addr->start[0] == '[' || nxt_inet6_probe(addr)) {

View File

@@ -257,7 +257,7 @@ nxt_memstrn(const u_char *s, const u_char *end, const char *ss, size_t length)
return NULL; return NULL;
} }
if (nxt_memcmp(s, s2, length) == 0) { if (memcmp(s, s2, length) == 0) {
return (u_char *) s - 1; return (u_char *) s - 1;
} }
} }
@@ -325,7 +325,7 @@ nxt_rmemstrn(const u_char *s, const u_char *end, const char *ss, size_t length)
c1 = *s1; c1 = *s1;
if (c1 == c2) { if (c1 == c2) {
if (nxt_memcmp(s1 + 1, s2, length) == 0) { if (memcmp(s1 + 1, s2, length) == 0) {
return (u_char *) s1; return (u_char *) s1;
} }
} }

View File

@@ -66,10 +66,6 @@ nxt_cpymem(void *dst, const void *src, size_t length)
(void) memmove(dst, src, length) (void) memmove(dst, src, length)
#define nxt_memcmp(s1, s2, length) \
memcmp((char *) s1, (char *) s2, length)
#define nxt_memchr(s, c, length) \ #define nxt_memchr(s, c, length) \
memchr((char *) s, c, length) memchr((char *) s, c, length)
@@ -132,7 +128,7 @@ NXT_EXPORT char *nxt_str_cstrz(nxt_mp_t *mp, const nxt_str_t *src);
#define nxt_strstr_eq(s1, s2) \ #define nxt_strstr_eq(s1, s2) \
(((s1)->length == (s2)->length) \ (((s1)->length == (s2)->length) \
&& (nxt_memcmp((s1)->start, (s2)->start, (s1)->length) == 0)) && (memcmp((s1)->start, (s2)->start, (s1)->length) == 0))
#define nxt_strcasestr_eq(s1, s2) \ #define nxt_strcasestr_eq(s1, s2) \
@@ -141,11 +137,11 @@ NXT_EXPORT char *nxt_str_cstrz(nxt_mp_t *mp, const nxt_str_t *src);
#define nxt_str_eq(s, p, _length) \ #define nxt_str_eq(s, p, _length) \
(((s)->length == _length) && (nxt_memcmp((s)->start, p, _length) == 0)) (((s)->length == _length) && (memcmp((s)->start, p, _length) == 0))
#define nxt_str_start(s, p, _length) \ #define nxt_str_start(s, p, _length) \
(((s)->length >= _length) && (nxt_memcmp((s)->start, p, _length) == 0)) (((s)->length >= _length) && (memcmp((s)->start, p, _length) == 0))
#define nxt_strchr_eq(s, c) \ #define nxt_strchr_eq(s, c) \

View File

@@ -753,7 +753,7 @@ nxt_http_parse_test_request_line(nxt_http_request_parse_t *rp,
return NXT_ERROR; return NXT_ERROR;
} }
if (nxt_memcmp(rp->version.str, test->version, 8) != 0) { if (memcmp(rp->version.str, test->version, 8) != 0) {
nxt_log_alert(log, "http parse test case failed:\n" nxt_log_alert(log, "http parse test case failed:\n"
" - request:\n\"%V\"\n" " - request:\n\"%V\"\n"
" - version: \"%*s\" (expected: \"%*s\")", request, " - version: \"%*s\" (expected: \"%*s\")", request,

View File

@@ -41,7 +41,7 @@ main(int argc, char **argv)
#if (NXT_TEST_RTDTSC) #if (NXT_TEST_RTDTSC)
if (nxt_process_argv[1] != NULL if (nxt_process_argv[1] != NULL
&& nxt_memcmp(nxt_process_argv[1], "rbm", 3) == 0) && memcmp(nxt_process_argv[1], "rbm", 3) == 0)
{ {
if (nxt_rbtree1_mb_start(thr) != NXT_OK) { if (nxt_rbtree1_mb_start(thr) != NXT_OK) {
return 1; return 1;

View File

@@ -129,7 +129,7 @@ nxt_utf8_file_name_test(nxt_thread_t *thr)
nxt_file_close(&task, &lc_file); nxt_file_close(&task, &lc_file);
if (n != 4 || nxt_memcmp(utf8, test, 4) != 0) { if (n != 4 || memcmp(utf8, test, 4) != 0) {
nxt_log_alert(thr->log, "nxt_file_read() mismatch"); nxt_log_alert(thr->log, "nxt_file_read() mismatch");
nxt_file_delete(lc_file.name); nxt_file_delete(lc_file.name);

View File

@@ -59,7 +59,7 @@ nxt_utf8_overlong(nxt_thread_t *thr, u_char *overlong, size_t len)
size = (p != NULL) ? p - utf8 : 0; size = (p != NULL) ? p - utf8 : 0;
if (len != size || nxt_memcmp(overlong, utf8, size) != 0) { if (len != size || memcmp(overlong, utf8, size) != 0) {
u = 0; u = 0;
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {