Skip to content

Commit 42fdaaa

Browse files
committed
clang-tidy bugprone-assignment-in-if-condition fixes
options.c: refactor around opt->logfile
1 parent df6bbb1 commit 42fdaaa

File tree

5 files changed

+44
-37
lines changed

5 files changed

+44
-37
lines changed

src/dns_poller.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ void dns_poller_init(dns_poller_t *d, struct ev_loop *loop,
135135
int bootstrap_dns_polling_interval,
136136
const char *hostname,
137137
int family, dns_poller_cb cb, void *cb_data) {
138-
int r = 0;
139-
if ((r = ares_library_init(ARES_LIB_INIT_ALL)) != ARES_SUCCESS) {
138+
int r = ares_library_init(ARES_LIB_INIT_ALL);
139+
if (r != ARES_SUCCESS) {
140140
FLOG("ares_library_init error: %s", ares_strerror(r));
141141
}
142142

@@ -148,11 +148,13 @@ void dns_poller_init(dns_poller_t *d, struct ev_loop *loop,
148148
};
149149
int optmask = ARES_OPT_TIMEOUTMS | ARES_OPT_TRIES | ARES_OPT_SOCK_STATE_CB;
150150

151-
if ((r = ares_init_options(&d->ares, &options, optmask)) != ARES_SUCCESS) {
151+
r = ares_init_options(&d->ares, &options, optmask);
152+
if (r != ARES_SUCCESS) {
152153
FLOG("ares_init_options error: %s", ares_strerror(r));
153154
}
154155

155-
if((r = ares_set_servers_ports_csv(d->ares, bootstrap_dns)) != ARES_SUCCESS) {
156+
r = ares_set_servers_ports_csv(d->ares, bootstrap_dns);
157+
if (r != ARES_SUCCESS) {
156158
FLOG("ares_set_servers_ports_csv error: %s", ares_strerror(r));
157159
}
158160

src/dns_server.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ static int get_listen_sock(const char *listen_addr, int listen_port,
4646
FLOG("Error creating socket");
4747
}
4848

49-
if ((res = bind(sock, ai->ai_addr, ai->ai_addrlen)) < 0) {
49+
res = bind(sock, ai->ai_addr, ai->ai_addrlen);
50+
if (res < 0) {
5051
FLOG("Error binding %s:%d: %s (%d)", listen_addr, listen_port,
5152
strerror(errno), res);
5253
}

src/https_client.c

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,8 @@ static int https_fetch_ctx_process_response(https_client_t *client,
356356
}
357357
}
358358

359-
if ((res = curl_easy_getinfo(
360-
ctx->curl, CURLINFO_RESPONSE_CODE, &long_resp)) != CURLE_OK) {
359+
res = curl_easy_getinfo(ctx->curl, CURLINFO_RESPONSE_CODE, &long_resp);
360+
if (res != CURLE_OK) {
361361
ELOG_REQ("CURLINFO_RESPONSE_CODE: %s", curl_easy_strerror(res));
362362
faulty_response = 1;
363363
} else if (long_resp != 200) {
@@ -391,8 +391,8 @@ static int https_fetch_ctx_process_response(https_client_t *client,
391391

392392
if (!faulty_response)
393393
{
394-
if ((res = curl_easy_getinfo(
395-
ctx->curl, CURLINFO_CONTENT_TYPE, &str_resp)) != CURLE_OK) {
394+
res = curl_easy_getinfo(ctx->curl, CURLINFO_CONTENT_TYPE, &str_resp);
395+
if (res != CURLE_OK) {
396396
ELOG_REQ("CURLINFO_CONTENT_TYPE: %s", curl_easy_strerror(res));
397397
} else if (str_resp == NULL ||
398398
strncmp(str_resp, DOH_CONTENT_TYPE, sizeof(DOH_CONTENT_TYPE) - 1) != 0) { // at least, start with it
@@ -402,23 +402,25 @@ static int https_fetch_ctx_process_response(https_client_t *client,
402402
}
403403

404404
if (logging_debug_enabled() || faulty_response || ctx->buflen == 0) {
405-
if ((res = curl_easy_getinfo(
406-
ctx->curl, CURLINFO_REDIRECT_URL, &str_resp)) != CURLE_OK) {
405+
res = curl_easy_getinfo(ctx->curl, CURLINFO_REDIRECT_URL, &str_resp);
406+
if (res != CURLE_OK) {
407407
ELOG_REQ("CURLINFO_REDIRECT_URL: %s", curl_easy_strerror(res));
408408
} else if (str_resp != NULL) {
409409
WLOG_REQ("Request would be redirected to: %s", str_resp);
410410
if (strcmp(str_resp, client->opt->resolver_url) != 0) {
411411
WLOG("Please update Resolver URL to avoid redirection!");
412412
}
413413
}
414-
if ((res = curl_easy_getinfo(
415-
ctx->curl, CURLINFO_SSL_VERIFYRESULT, &long_resp)) != CURLE_OK) {
414+
415+
res = curl_easy_getinfo(ctx->curl, CURLINFO_SSL_VERIFYRESULT, &long_resp);
416+
if (res != CURLE_OK) {
416417
ELOG_REQ("CURLINFO_SSL_VERIFYRESULT: %s", curl_easy_strerror(res));
417418
} else if (long_resp != CURLE_OK) {
418419
WLOG_REQ("CURLINFO_SSL_VERIFYRESULT: %s", curl_easy_strerror(long_resp));
419420
}
420-
if ((res = curl_easy_getinfo(
421-
ctx->curl, CURLINFO_OS_ERRNO, &long_resp)) != CURLE_OK) {
421+
422+
res = curl_easy_getinfo(ctx->curl, CURLINFO_OS_ERRNO, &long_resp);
423+
if (res != CURLE_OK) {
422424
ELOG_REQ("CURLINFO_OS_ERRNO: %s", curl_easy_strerror(res));
423425
} else if (long_resp != 0) {
424426
WLOG_REQ("CURLINFO_OS_ERRNO: %d %s", long_resp, strerror(long_resp));
@@ -430,8 +432,8 @@ static int https_fetch_ctx_process_response(https_client_t *client,
430432
}
431433

432434
if (logging_debug_enabled() || client->stat) {
433-
if ((res = curl_easy_getinfo(
434-
ctx->curl, CURLINFO_NUM_CONNECTS , &long_resp)) != CURLE_OK) {
435+
res = curl_easy_getinfo(ctx->curl, CURLINFO_NUM_CONNECTS , &long_resp);
436+
if (res != CURLE_OK) {
435437
ELOG_REQ("CURLINFO_NUM_CONNECTS: %s", curl_easy_strerror(res));
436438
} else {
437439
DLOG_REQ("CURLINFO_NUM_CONNECTS: %d", long_resp);
@@ -442,20 +444,22 @@ static int https_fetch_ctx_process_response(https_client_t *client,
442444
}
443445

444446
if (logging_debug_enabled()) {
445-
if ((res = curl_easy_getinfo(
446-
ctx->curl, CURLINFO_EFFECTIVE_URL, &str_resp)) != CURLE_OK) {
447+
res = curl_easy_getinfo(ctx->curl, CURLINFO_EFFECTIVE_URL, &str_resp);
448+
if (res != CURLE_OK) {
447449
ELOG_REQ("CURLINFO_EFFECTIVE_URL: %s", curl_easy_strerror(res));
448450
} else {
449451
DLOG_REQ("CURLINFO_EFFECTIVE_URL: %s", str_resp);
450452
}
451-
if ((res = curl_easy_getinfo(
452-
ctx->curl, CURLINFO_HTTP_VERSION, &long_resp)) != CURLE_OK) {
453+
454+
res = curl_easy_getinfo(ctx->curl, CURLINFO_HTTP_VERSION, &long_resp);
455+
if (res != CURLE_OK) {
453456
ELOG_REQ("CURLINFO_HTTP_VERSION: %s", curl_easy_strerror(res));
454457
} else if (long_resp != CURL_HTTP_VERSION_NONE) {
455458
DLOG_REQ("CURLINFO_HTTP_VERSION: %s", http_version_str(long_resp));
456459
}
457-
if ((res = curl_easy_getinfo(
458-
ctx->curl, CURLINFO_PROTOCOL, &long_resp)) != CURLE_OK) {
460+
461+
res = curl_easy_getinfo(ctx->curl, CURLINFO_PROTOCOL, &long_resp);
462+
if (res != CURLE_OK) {
459463
ELOG_REQ("CURLINFO_PROTOCOL: %s", curl_easy_strerror(res));
460464
} else if (long_resp != CURLPROTO_HTTPS) {
461465
DLOG_REQ("CURLINFO_PROTOCOL: %d", long_resp);

src/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ static int hostname_from_uri(const char* uri,
5353
if (!isalpha(*(end - 1))) { return 0; } // last digit non-alpha.
5454

5555
// If using basic authentication in URL, chop off prefix.
56-
char *tmp = NULL;
57-
if ((tmp = strchr(uri, '@'))) {
56+
char *tmp = strchr(uri, '@');
57+
if (tmp) {
5858
tmp++;
5959
if (tmp < end) {
6060
uri = tmp;

src/options.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void options_init(struct Options *opt) {
3232
opt->listen_addr = "127.0.0.1";
3333
opt->listen_port = 5053;
3434
opt->logfile = "-";
35-
opt->logfd = -1;
35+
opt->logfd = STDOUT_FILENO;
3636
opt->loglevel = LOG_ERROR;
3737
opt->daemonize = 0;
3838
opt->dscp = 0;
@@ -126,16 +126,16 @@ int options_parse_args(struct Options *opt, int argc, char **argv) {
126126
}
127127
}
128128
if (opt->user) {
129-
struct passwd *p = NULL;
130-
if (!(p = getpwnam(opt->user)) || !p->pw_uid) {
129+
struct passwd *p = getpwnam(opt->user);
130+
if (!p || !p->pw_uid) {
131131
printf("Username (%s) invalid.\n", opt->user);
132132
return -1;
133133
}
134134
opt->uid = p->pw_uid;
135135
}
136136
if (opt->group) {
137-
struct group *g = NULL;
138-
if (!(g = getgrnam(opt->group)) || !g->gr_gid) {
137+
struct group *g = getgrnam(opt->group);
138+
if (!g || !g->gr_gid) {
139139
printf("Group (%s) invalid.\n", opt->group);
140140
return -1;
141141
}
@@ -154,13 +154,13 @@ int options_parse_args(struct Options *opt, int argc, char **argv) {
154154
"----------------------------\n");
155155
sleep(1);
156156
}
157-
if (opt->logfile == NULL ||
158-
!strcmp(opt->logfile, "-")) {
159-
opt->logfd = STDOUT_FILENO;
160-
} else if ((opt->logfd = open(opt->logfile,
161-
O_CREAT | O_WRONLY | O_APPEND | O_CLOEXEC,
162-
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)) <= 0) {
163-
printf("Logfile '%s' is not writable.\n", opt->logfile);
157+
if (opt->logfile != NULL && strcmp(opt->logfile, "-") != 0) {
158+
opt->logfd = open(opt->logfile,
159+
O_CREAT | O_WRONLY | O_APPEND | O_CLOEXEC,
160+
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
161+
if (opt->logfd <= 0) {
162+
printf("Could not open logfile '%s' for writing.\n", opt->logfile);
163+
}
164164
}
165165
if (opt->resolver_url == NULL ||
166166
strncmp(opt->resolver_url, "https://", 8) != 0) {

0 commit comments

Comments
 (0)