fix cppcoreguidelines-init-variables

This commit is contained in:
Meghan Denny
2025-09-22 23:02:13 -07:00
parent 4709eaa5f9
commit c5d2da7fa7
7 changed files with 30 additions and 29 deletions

View File

@@ -20,7 +20,6 @@ Checks: >
-performance-no-int-to-ptr,
cppcoreguidelines-*,
-cppcoreguidelines-avoid-magic-numbers,
-cppcoreguidelines-init-variables,
-cppcoreguidelines-narrowing-conversions,
-cppcoreguidelines-macro-to-enum,
-cppcoreguidelines-avoid-non-const-global-variables,

View File

@@ -593,6 +593,7 @@ LIBUS_SOCKET_DESCRIPTOR bsd_create_socket(int domain, int type, int protocol, in
*err = 0;
}
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
LIBUS_SOCKET_DESCRIPTOR created_fd;
#if defined(SOCK_CLOEXEC) && defined(SOCK_NONBLOCK)
const int flags = SOCK_CLOEXEC | SOCK_NONBLOCK;
@@ -696,6 +697,7 @@ int bsd_addr_get_port(struct bsd_addr_t *addr) {
// called by dispatch_ready_poll
LIBUS_SOCKET_DESCRIPTOR bsd_accept_socket(LIBUS_SOCKET_DESCRIPTOR fd, struct bsd_addr_t *addr) {
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
LIBUS_SOCKET_DESCRIPTOR accepted_fd;
while (1) {
@@ -855,7 +857,7 @@ int bsd_would_block() {
}
static int us_internal_bind_and_listen(LIBUS_SOCKET_DESCRIPTOR listenFd, struct sockaddr *listenAddr, socklen_t listenAddrLength, int backlog, int* error) {
int result;
int result = 0;
do
result = bind(listenFd, listenAddr, listenAddrLength);
while (IS_EINTR(result));
@@ -973,7 +975,7 @@ inline __attribute__((always_inline)) LIBUS_SOCKET_DESCRIPTOR bsd_bind_listen_fd
// return LIBUS_SOCKET_ERROR or the fd that represents listen socket
// listen both on ipv6 and ipv4
LIBUS_SOCKET_DESCRIPTOR bsd_create_listen_socket(const char *host, int port, int options, int* error) {
struct addrinfo hints, *result;
struct addrinfo hints, *result = NULL;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_flags = AI_PASSIVE;
@@ -988,7 +990,7 @@ LIBUS_SOCKET_DESCRIPTOR bsd_create_listen_socket(const char *host, int port, int
}
LIBUS_SOCKET_DESCRIPTOR listenFd = LIBUS_SOCKET_ERROR;
struct addrinfo *listenAddr;
struct addrinfo* listenAddr = NULL;
for (struct addrinfo *a = result; a != NULL; a = a->ai_next) {
if (a->ai_family == AF_INET6) {
listenFd = bsd_create_socket(a->ai_family, a->ai_socktype, a->ai_protocol, NULL);
@@ -1174,7 +1176,7 @@ LIBUS_SOCKET_DESCRIPTOR bsd_create_udp_socket(const char *host, int port, int op
*err = 0;
}
struct addrinfo hints, *result;
struct addrinfo hints, *result = NULL;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_flags = AI_PASSIVE;
@@ -1284,7 +1286,7 @@ LIBUS_SOCKET_DESCRIPTOR bsd_create_udp_socket(const char *host, int port, int op
}
int bsd_connect_udp_socket(LIBUS_SOCKET_DESCRIPTOR fd, const char *host, int port) {
struct addrinfo hints, *result;
struct addrinfo hints, *result = NULL;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
@@ -1394,8 +1396,8 @@ static int bsd_do_connect_raw(LIBUS_SOCKET_DESCRIPTOR fd, struct sockaddr *addr,
#else
int r;
do {
int r = 0;
do {
errno = 0;
r = connect(fd, (struct sockaddr *)addr, namelen);
} while (IS_EINTR(r));

View File

@@ -517,7 +517,7 @@ void *us_socket_context_connect(int ssl, struct us_socket_context_t *context, co
return us_socket_context_connect_resolved_dns(context, &addr, options, socket_ext_size);
}
struct addrinfo_request* ai_req;
struct addrinfo_request* ai_req = NULL;
if (Bun__addrinfo_get(loop, host, (uint16_t)port, &ai_req) == 0) {
// fast path for cached results
struct addrinfo_result *result = Bun__addrinfo_getRequestResult(ai_req);

View File

@@ -808,7 +808,7 @@ create_ssl_context_from_options(struct us_socket_context_options_t options) {
}
if (options.ca_file_name) {
STACK_OF(X509_NAME) * ca_list;
STACK_OF(X509_NAME) * ca_list = NULL;
ca_list = SSL_load_client_CA_file(options.ca_file_name);
if (ca_list == NULL) {
free_ssl_context(ssl_context);
@@ -826,7 +826,7 @@ create_ssl_context_from_options(struct us_socket_context_options_t options) {
if (options.dh_params_file_name) {
/* Set up ephemeral DH parameters. */
DH *dh_2048 = NULL;
FILE *paramfile;
FILE *paramfile = NULL;
paramfile = fopen(options.dh_params_file_name, "r");
if (paramfile) {
@@ -882,8 +882,8 @@ create_ssl_context_from_options(struct us_socket_context_options_t options) {
int us_ssl_ctx_use_privatekey_content(SSL_CTX *ctx, const char *content,
int type) {
int reason_code, ret = 0;
BIO *in;
int reason_code = 0, ret = 0;
BIO *in = NULL;
EVP_PKEY *pkey = NULL;
in = BIO_new_mem_buf(content, strlen(content));
if (in == NULL) {
@@ -948,7 +948,7 @@ end:
}
int us_ssl_ctx_use_certificate_chain(SSL_CTX *ctx, const char *content) {
BIO *in;
BIO *in = NULL;
int ret = 0;
X509 *x = NULL;
@@ -976,9 +976,9 @@ int us_ssl_ctx_use_certificate_chain(SSL_CTX *ctx, const char *content) {
if (ret) {
// If we could set up our certificate, now proceed to the CA
// certificates.
X509 *ca;
int r;
uint32_t err;
X509 *ca = NULL;
int r = 0;
uint32_t err = 0;
SSL_CTX_clear_chain_certs(ctx);
@@ -1203,7 +1203,7 @@ SSL_CTX *create_ssl_context_from_bun_options(
if (options.ca_file_name) {
SSL_CTX_set_cert_store(ssl_context, us_get_default_ca_store());
STACK_OF(X509_NAME) * ca_list;
STACK_OF(X509_NAME) * ca_list = NULL;
ca_list = SSL_load_client_CA_file(options.ca_file_name);
if (ca_list == NULL) {
*err = CREATE_BUN_SOCKET_ERROR_LOAD_CA_FILE;
@@ -1269,7 +1269,7 @@ SSL_CTX *create_ssl_context_from_bun_options(
if (options.dh_params_file_name) {
/* Set up ephemeral DH parameters. */
DH *dh_2048 = NULL;
FILE *paramfile;
FILE *paramfile = NULL;
paramfile = fopen(options.dh_params_file_name, "r");
if (paramfile) {

View File

@@ -366,7 +366,7 @@ int kqueue_change(int kqfd, int fd, int old_events, int new_events, void *user_d
/* Do they differ in writable? */
EV_SET64(&change_list[change_length++], fd, EVFILT_WRITE, (new_events & LIBUS_SOCKET_WRITABLE) ? EV_ADD : EV_DELETE, 0, 0, (uint64_t)(void*)user_data, 0, 0);
}
int ret;
int ret = 0;
do {
ret = kevent64(kqfd, change_list, change_length, change_list, change_length, KEVENT_FLAG_ERROR_EVENTS, NULL);
} while (IS_EINTR(ret));
@@ -564,7 +564,7 @@ void us_timer_close(struct us_timer_t *timer, int fallthrough) {
struct kevent64_s event;
EV_SET64(&event, (uint64_t) (void*) internal_cb, EVFILT_TIMER, EV_DELETE, 0, 0, (uint64_t)internal_cb, 0, 0);
int ret;
int ret = 0;
do {
ret = kevent64(internal_cb->loop->fd, &event, 1, &event, 1, KEVENT_FLAG_ERROR_EVENTS, NULL);
} while (IS_EINTR(ret));
@@ -588,7 +588,7 @@ void us_timer_set(struct us_timer_t *t, void (*cb)(struct us_timer_t *t), int ms
uint64_t ptr = (uint64_t)(void*)internal_cb;
EV_SET64(&event, ptr, EVFILT_TIMER, EV_ADD | (repeat_ms ? 0 : EV_ONESHOT), 0, ms, (uint64_t)internal_cb, 0, 0);
int ret;
int ret = 0;
do {
ret = kevent64(internal_cb->loop->fd, &event, 1, &event, 1, KEVENT_FLAG_ERROR_EVENTS, NULL);
} while (IS_EINTR(ret));
@@ -687,7 +687,7 @@ void us_internal_async_close(struct us_internal_async *a) {
uint64_t ptr = (uint64_t)(void*)internal_cb;
EV_SET64(&event, ptr, EVFILT_MACHPORT, EV_DELETE, 0, 0, (uint64_t)(void*)internal_cb, 0,0);
int ret;
int ret = 0;
do {
ret = kevent64(internal_cb->loop->fd, &event, 1, &event, 1, KEVENT_FLAG_ERROR_EVENTS, NULL);
} while (IS_EINTR(ret));
@@ -718,7 +718,7 @@ void us_internal_async_set(struct us_internal_async *a, void (*cb)(struct us_int
event.ext[1] = MACHPORT_BUF_LEN;
event.udata = (uint64_t)(void*)internal_cb;
int ret;
int ret = 0;
do {
ret = kevent64(internal_cb->loop->fd, &event, 1, &event, 1, KEVENT_FLAG_ERROR_EVENTS, NULL);
} while (IS_EINTR(ret));

View File

@@ -181,7 +181,7 @@ static const int MAX_LOW_PRIO_SOCKETS_PER_LOOP_ITERATION = 5;
void us_internal_handle_low_priority_sockets(struct us_loop_t *loop) {
struct us_internal_loop_data_t *loop_data = &loop->data;
struct us_socket_t *s;
struct us_socket_t *s = NULL;
loop_data->low_prio_budget = MAX_LOW_PRIO_SOCKETS_PER_LOOP_ITERATION;
@@ -425,7 +425,7 @@ void us_internal_dispatch_ready_poll(struct us_poll_t *p, int error, int eof, in
const int recv_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
#endif
int length;
int length = 0;
#if !defined(_WIN32)
if(s->flags.is_ipc) {
struct msghdr msg = {0};

View File

@@ -74,7 +74,7 @@ UV_EXTERN void uv_mutex_destroy(uv_mutex_t* mutex)
UV_EXTERN int uv_mutex_init(uv_mutex_t* mutex)
{
pthread_mutexattr_t attr;
int err;
int err = 0;
if (pthread_mutexattr_init(&attr))
abort();
@@ -94,7 +94,7 @@ UV_EXTERN int uv_mutex_init(uv_mutex_t* mutex)
UV_EXTERN int uv_mutex_init_recursive(uv_mutex_t* mutex)
{
pthread_mutexattr_t attr;
int err;
int err = 0;
if (pthread_mutexattr_init(&attr))
abort();
@@ -120,7 +120,7 @@ UV_EXTERN void uv_mutex_lock(uv_mutex_t* mutex)
// Copy-pasted from libuv
UV_EXTERN int uv_mutex_trylock(uv_mutex_t* mutex)
{
int err;
int err = 0;
err = pthread_mutex_trylock(mutex);
if (err) {