Skip to content

Commit 7b27ecd

Browse files
authored
Fixes 16 (#193)
* Warning fixes Compatibility with CMake < 3.10 will be removed from a future version of CMake. warning: call to '_curl_easy_setopt_err_long' declared with attribute warning: curl_easy_setopt expects a long argument * Avoid division by zero The operation of dividing by a fraction is equivalent to multiplying by its reciprocal. * clang tidy changes * Replace atoi with strtoul Safer, minus one clang tidy rule.
1 parent daa0d39 commit 7b27ecd

File tree

11 files changed

+89
-109
lines changed

11 files changed

+89
-109
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.7)
1+
cmake_minimum_required(VERSION 3.10)
22
project(HttpsDnsProxy C)
33

44
include(CheckIncludeFile)
@@ -110,7 +110,7 @@ if(USE_CLANG_TIDY)
110110
message(STATUS "clang-tidy not found.")
111111
else()
112112
message(STATUS "clang-tidy found: ${CLANG_TIDY_EXE}")
113-
set(DO_CLANG_TIDY "${CLANG_TIDY_EXE}" "-fix" "-fix-errors" "-checks=*,-cert-err34-c,-readability-identifier-length,-altera-unroll-loops,-bugprone-easily-swappable-parameters,-concurrency-mt-unsafe,-*magic-numbers,-hicpp-signed-bitwise,-readability-function-cognitive-complexity,-altera-id-dependent-backward-branch,-google-readability-todo,-misc-include-cleaner,-cast-align")
113+
set(DO_CLANG_TIDY "${CLANG_TIDY_EXE}" "-fix" "-fix-errors" "-checks=*,-readability-identifier-length,-altera-unroll-loops,-bugprone-easily-swappable-parameters,-concurrency-mt-unsafe,-*magic-numbers,-hicpp-signed-bitwise,-readability-function-cognitive-complexity,-altera-id-dependent-backward-branch,-misc-include-cleaner,-llvmlibc-restrict-system-libc-headers,-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling")
114114
endif()
115115
else()
116116
message(STATUS "Not using clang-tidy.")

src/dns_poller.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#include <netdb.h> // NOLINT(llvmlibc-restrict-system-libc-headers)
2-
#include <string.h> // NOLINT(llvmlibc-restrict-system-libc-headers)
1+
#include <netdb.h>
2+
#include <string.h>
33

44
#include "dns_poller.h"
55
#include "logging.h"
@@ -38,7 +38,6 @@ static void sock_state_cb(void *data, int fd, int read, int write) {
3838
FLOG("c-ares needed more IO event handler, than the number of provided nameservers: %u", d->io_events_count);
3939
}
4040
DLOG("Reserved new io event: %p", io_event_ptr);
41-
// NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
4241
ev_io_init(io_event_ptr, sock_cb, fd,
4342
(read ? EV_READ : 0) | (write ? EV_WRITE : 0));
4443
ev_io_start(d->loop, io_event_ptr);
@@ -119,7 +118,6 @@ static ev_tstamp get_timeout(dns_poller_t *d)
119118
static struct timeval max_tv = {.tv_sec = 5, .tv_usec = 0};
120119
struct timeval tv;
121120
struct timeval *tvp = ares_timeout(d->ares, &max_tv, &tv);
122-
// NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
123121
ev_tstamp after = (double)tvp->tv_sec + (double)tvp->tv_usec * 1e-6;
124122
return after > 0.1 ? after : 0.1;
125123
}
@@ -147,7 +145,7 @@ static void timer_cb(struct ev_loop __attribute__((unused)) *loop,
147145
d->request_ongoing = 1;
148146

149147
struct ares_addrinfo_hints hints;
150-
memset(&hints, 0, sizeof(hints)); // NOLINT(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
148+
memset(&hints, 0, sizeof(hints));
151149
hints.ai_flags = ARES_AI_CANONNAME;
152150
hints.ai_family = d->family;
153151
hints.ai_socktype = SOCK_STREAM;
@@ -199,8 +197,6 @@ void dns_poller_init(dns_poller_t *d, struct ev_loop *loop,
199197
d->polling_interval = bootstrap_dns_polling_interval;
200198
d->request_ongoing = 0;
201199
d->cb_data = cb_data;
202-
203-
// NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
204200
ev_timer_init(&d->timer, timer_cb, 0, 0);
205201
d->timer.data = d;
206202
ev_timer_start(d->loop, &d->timer);

src/dns_server.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
#include <ares.h> // NOLINT(llvmlibc-restrict-system-libc-headers)
2-
#include <ares_dns_record.h> // NOLINT(llvmlibc-restrict-system-libc-headers)
3-
#include <errno.h> // NOLINT(llvmlibc-restrict-system-libc-headers)
1+
#include <ares.h>
2+
#include <ares_dns_record.h>
3+
#include <errno.h>
44
#include <stdint.h>
5-
#include <string.h> // NOLINT(llvmlibc-restrict-system-libc-headers)
6-
#include <unistd.h> // NOLINT(llvmlibc-restrict-system-libc-headers)
5+
#include <string.h>
6+
#include <unistd.h>
77

88
#include "dns_server.h"
99
#include "logging.h"
@@ -67,7 +67,7 @@ static void watcher_cb(struct ev_loop __attribute__((unused)) *loop,
6767
if (dns_req == NULL) {
6868
FLOG("Out of mem");
6969
}
70-
memcpy(dns_req, tmp_buf, (size_t)len); // NOLINT(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
70+
memcpy(dns_req, tmp_buf, (size_t)len);
7171

7272
d->cb(d, 0, d->cb_data, (struct sockaddr*)&tmp_raddr, dns_req, (size_t)len);
7373
}
@@ -80,8 +80,6 @@ void dns_server_init(dns_server_t *d, struct ev_loop *loop,
8080
d->addrlen = listen_addrinfo->ai_addrlen;
8181
d->cb = cb;
8282
d->cb_data = data;
83-
84-
// NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
8583
ev_io_init(&d->watcher, watcher_cb, d->sock, EV_READ);
8684
d->watcher.data = d;
8785
ev_io_start(d->loop, &d->watcher);
@@ -145,7 +143,7 @@ static void truncate_dns_response(char *buf, size_t *buflen, const uint16_t size
145143

146144
// rough estimate to reach size limit
147145
size_t answers = ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ANSWER);
148-
size_t answers_to_keep = (size_limit - DNS_HEADER_LENGTH) / (old_size / answers);
146+
size_t answers_to_keep = ((size_limit - DNS_HEADER_LENGTH) * answers) / old_size;
149147
answers_to_keep = answers_to_keep > 0 ? answers_to_keep : 1; // try to keep 1 answer
150148

151149
// remove answer records until fit size limit or running out of answers
@@ -185,7 +183,7 @@ static void truncate_dns_response(char *buf, size_t *buflen, const uint16_t size
185183
ares_dns_record_destroy(dnsrec);
186184

187185
if (new_resp != NULL && new_resp_len < old_size) {
188-
memcpy(buf, new_resp, new_resp_len); // NOLINT(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
186+
memcpy(buf, new_resp, new_resp_len);
189187
*buflen = new_resp_len;
190188
buf[2] |= 0x02; // set truncation flag
191189
ILOG("%04hX: DNS response size truncated from %u to %u to keep %u limit",
@@ -196,6 +194,10 @@ static void truncate_dns_response(char *buf, size_t *buflen, const uint16_t size
196194

197195
void dns_server_respond(dns_server_t *d, struct sockaddr *raddr,
198196
const char *dns_req, const size_t dns_req_len, char *dns_resp, size_t dns_resp_len) {
197+
if (dns_resp_len < DNS_HEADER_LENGTH) {
198+
WLOG("Malformed response received, invalid length: %u", dns_resp_len);
199+
return;
200+
}
199201
if (dns_resp_len > DNS_SIZE_LIMIT) {
200202
const uint16_t udp_size = get_edns_udp_size(dns_req, dns_req_len);
201203
if (dns_resp_len > udp_size) {

src/dns_server_tcp.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
22
#define _GNU_SOURCE // needed for having accept4()
33

4-
#include <errno.h> // NOLINT(llvmlibc-restrict-system-libc-headers)
5-
#include <fcntl.h> // NOLINT(llvmlibc-restrict-system-libc-headers)
6-
#include <unistd.h> // NOLINT(llvmlibc-restrict-system-libc-headers)
4+
#include <errno.h>
5+
#include <fcntl.h>
6+
#include <unistd.h>
77

88
#include "dns_server_tcp.h"
99
#include "logging.h"
@@ -104,10 +104,10 @@ static int get_dns_request(struct tcp_client_s *client,
104104
if (*dns_req == NULL) {
105105
FLOG_CLIENT("Out of mem");
106106
}
107-
memcpy(*dns_req, client->input_buffer + sizeof(uint16_t), *req_size); // NOLINT(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
107+
memcpy(*dns_req, client->input_buffer + sizeof(uint16_t), *req_size);
108108
// move down data of next request(s) if any
109109
client->input_buffer_used -= data_size;
110-
memmove(client->input_buffer, client->input_buffer + data_size, client->input_buffer_used); // NOLINT(clang-diagnostic-format-nonliteral,clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
110+
memmove(client->input_buffer, client->input_buffer + data_size, client->input_buffer_used);
111111
return 1;
112112
}
113113

@@ -150,7 +150,7 @@ static void read_cb(struct ev_loop __attribute__((unused)) *loop,
150150
FLOG_CLIENT("Out of mem");
151151
}
152152
}
153-
memcpy(client->input_buffer + client->input_buffer_used, buf, (size_t)len); // NOLINT(clang-diagnostic-format-nonliteral,clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
153+
memcpy(client->input_buffer + client->input_buffer_used, buf, (size_t)len);
154154
client->input_buffer_used = needed_space;
155155

156156
// Split requests
@@ -208,13 +208,12 @@ static void accept_cb(struct ev_loop __attribute__((unused)) *loop,
208208
client->d = d;
209209
client->id = d->client_id;
210210
client->sock = client_sock;
211-
memcpy(&client->raddr, &client_addr, client_addr_len); // NOLINT(clang-diagnostic-format-nonliteral,clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
211+
memcpy(&client->raddr, &client_addr, client_addr_len);
212212
client->addr_len = client_addr_len;
213213
client->input_buffer = NULL;
214214
client->next = d->clients;
215215
d->clients = client;
216216

217-
// NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
218217
ev_io_init(&client->read_watcher, read_cb, client->sock, EV_READ);
219218
client->read_watcher.data = client;
220219
ev_io_start(d->loop, &client->read_watcher);
@@ -294,7 +293,6 @@ dns_server_tcp_t * dns_server_tcp_create(
294293
d->client_limit = tcp_client_limit;
295294
d->clients = NULL;
296295

297-
// NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
298296
ev_io_init(&d->accept_watcher, accept_cb, d->sock, EV_READ);
299297
d->accept_watcher.data = d;
300298
ev_io_start(d->loop, &d->accept_watcher);

src/https_client.c

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
#include <ctype.h> // NOLINT(llvmlibc-restrict-system-libc-headers)
2-
#include <errno.h> // NOLINT(llvmlibc-restrict-system-libc-headers)
3-
#include <ev.h> // NOLINT(llvmlibc-restrict-system-libc-headers)
4-
#include <math.h> // NOLINT(llvmlibc-restrict-system-libc-headers)
5-
#include <netinet/in.h> // NOLINT(llvmlibc-restrict-system-libc-headers)
1+
#include <ctype.h>
2+
#include <errno.h>
3+
#include <ev.h>
4+
#include <math.h>
5+
#include <netinet/in.h>
66
#include <stdint.h>
7-
#include <stdio.h> // NOLINT(llvmlibc-restrict-system-libc-headers)
8-
#include <string.h> // NOLINT(llvmlibc-restrict-system-libc-headers)
9-
#include <sys/socket.h> // NOLINT(llvmlibc-restrict-system-libc-headers)
10-
#include <unistd.h> // NOLINT(llvmlibc-restrict-system-libc-headers)
7+
#include <stdio.h>
8+
#include <string.h>
9+
#include <sys/socket.h>
10+
#include <unistd.h>
1111

1212
#include "https_client.h"
1313
#include "logging.h"
@@ -69,7 +69,6 @@ static size_t write_buffer(void *buf, size_t size, size_t nmemb, void *userp) {
6969
return 0;
7070
}
7171
ctx->buf = new_buf;
72-
// NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
7372
memcpy(&(ctx->buf[ctx->buflen]), buf, write_size);
7473
ctx->buflen = new_size;
7574
// We always expect to receive valid non-null ASCII but just to be safe...
@@ -153,21 +152,16 @@ static void https_log_data(int level, struct https_fetch_ctx *ctx,
153152
char str[width + 1];
154153
size_t hex_off = 0;
155154
size_t str_off = 0;
156-
// NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
157155
memset(hex, 0, sizeof(hex));
158-
// NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
159156
memset(str, 0, sizeof(str));
160157

161158
for (size_t c = 0; c < width; c++) {
162159
if (i+c < size) {
163-
// NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
164160
hex_off += (size_t)snprintf(hex + hex_off, sizeof(hex) - hex_off,
165161
"%02x ", (unsigned char)ptr[i+c]);
166-
// NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
167162
str_off += (size_t)snprintf(str + str_off, sizeof(str) - str_off,
168163
"%c", isprint(ptr[i+c]) ? ptr[i+c] : '.');
169164
} else {
170-
// NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
171165
hex_off += (size_t)snprintf(hex + hex_off, sizeof(hex) - hex_off, " ");
172166
}
173167
}
@@ -313,14 +307,14 @@ static void https_fetch_ctx_init(https_client_t *client,
313307
ASSERT_CURL_EASY_SETOPT(ctx, CURLOPT_POSTFIELDS, data);
314308
ASSERT_CURL_EASY_SETOPT(ctx, CURLOPT_WRITEFUNCTION, &write_buffer);
315309
ASSERT_CURL_EASY_SETOPT(ctx, CURLOPT_WRITEDATA, ctx);
316-
ASSERT_CURL_EASY_SETOPT(ctx, CURLOPT_MAXAGE_CONN, client->opt->max_idle_time);
317-
ASSERT_CURL_EASY_SETOPT(ctx, CURLOPT_PIPEWAIT, client->opt->use_http_version > 1);
310+
ASSERT_CURL_EASY_SETOPT(ctx, CURLOPT_MAXAGE_CONN, (long)client->opt->max_idle_time);
311+
ASSERT_CURL_EASY_SETOPT(ctx, CURLOPT_PIPEWAIT, (long)(client->opt->use_http_version > 1));
318312
ASSERT_CURL_EASY_SETOPT(ctx, CURLOPT_USERAGENT, "https_dns_proxy/0.4");
319-
ASSERT_CURL_EASY_SETOPT(ctx, CURLOPT_FOLLOWLOCATION, 0);
320-
ASSERT_CURL_EASY_SETOPT(ctx, CURLOPT_NOSIGNAL, 0);
321-
ASSERT_CURL_EASY_SETOPT(ctx, CURLOPT_TIMEOUT, client->connections > 0 ? 5 : 10 /* seconds */);
313+
ASSERT_CURL_EASY_SETOPT(ctx, CURLOPT_FOLLOWLOCATION, 0L);
314+
ASSERT_CURL_EASY_SETOPT(ctx, CURLOPT_NOSIGNAL, 0L);
315+
ASSERT_CURL_EASY_SETOPT(ctx, CURLOPT_TIMEOUT, client->connections > 0 ? 5L : 10L /* seconds */);
322316
// We know Google supports this, so force it.
323-
ASSERT_CURL_EASY_SETOPT(ctx, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
317+
ASSERT_CURL_EASY_SETOPT(ctx, CURLOPT_SSLVERSION, (long)CURL_SSLVERSION_TLSv1_2);
324318
ASSERT_CURL_EASY_SETOPT(ctx, CURLOPT_ERRORBUFFER, ctx->curl_errbuf); // zeroed by calloc
325319
if (client->opt->curl_proxy) {
326320
DLOG_REQ("Using curl proxy: %s", client->opt->curl_proxy);
@@ -639,7 +633,6 @@ static int multi_sock_cb(CURL *curl, curl_socket_t sock, int what,
639633
return -1;
640634
}
641635
DLOG("Reserved new io event: %p", io_event_ptr);
642-
// NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
643636
ev_io_init(io_event_ptr, sock_cb, sock,
644637
((what & CURL_POLL_IN) ? EV_READ : 0) |
645638
((what & CURL_POLL_OUT) ? EV_WRITE : 0));
@@ -652,7 +645,6 @@ static int multi_timer_cb(CURLM __attribute__((unused)) *multi,
652645
GET_PTR(https_client_t, c, userp);
653646
ev_timer_stop(c->loop, &c->timer);
654647
if (timeout_ms >= 0) {
655-
// NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
656648
ev_timer_init(&c->timer, timer_cb, (double)timeout_ms / 1000.0, 0);
657649
ev_timer_start(c->loop, &c->timer);
658650
}
@@ -681,7 +673,6 @@ static void reset_timer_cb(struct ev_loop __attribute__((unused)) *loop,
681673

682674
void https_client_init(https_client_t *c, options_t *opt,
683675
stat_t *stat, struct ev_loop *loop) {
684-
// NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
685676
memset(c, 0, sizeof(*c));
686677
c->loop = loop;
687678
c->fetches = NULL;

src/logging.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#include <inttypes.h>
22
#include <stdarg.h>
33
#include <stdint.h>
4-
#include <stdio.h> // NOLINT(llvmlibc-restrict-system-libc-headers)
5-
#include <sys/time.h> // NOLINT(llvmlibc-restrict-system-libc-headers)
6-
#include <unistd.h> // NOLINT(llvmlibc-restrict-system-libc-headers)
4+
#include <stdio.h>
5+
#include <sys/time.h>
6+
#include <unistd.h>
77

88
#include "logging.h"
99
#include "ring_buffer.h"
@@ -56,7 +56,6 @@ void logging_events_init(struct ev_loop *loop) {
5656
/* don't start timer if we will never write messages that are not flushed */
5757
if (loglevel < LOG_FLUSH_LEVEL) {
5858
DLOG("starting periodic log flush timer");
59-
// NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
6059
ev_timer_init(&logging_timer, logging_timer_cb, 0, 10);
6160
ev_timer_start(loop, &logging_timer);
6261
}
@@ -114,8 +113,6 @@ void _log(const char *file, int line, int severity, const char *fmt, ...) {
114113

115114
char buff[LOG_LINE_SIZE];
116115
uint32_t buff_pos = 0;
117-
118-
// NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
119116
int chars = snprintf(buff, LOG_LINE_SIZE, "%s %8"PRIu64".%06"PRIu64" %s:%d ",
120117
SeverityStr[severity], (uint64_t)tv.tv_sec, (uint64_t)tv.tv_usec, file, line);
121118
if (chars < 0 || chars >= LOG_LINE_SIZE/2) {
@@ -125,8 +122,7 @@ void _log(const char *file, int line, int severity, const char *fmt, ...) {
125122

126123
va_list args;
127124
va_start(args, fmt);
128-
// NOLINTNEXTLINE(clang-diagnostic-format-nonliteral,clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
129-
chars = vsnprintf(buff + buff_pos, LOG_LINE_SIZE - buff_pos, fmt, args);
125+
chars = vsnprintf(buff + buff_pos, LOG_LINE_SIZE - buff_pos, fmt, args); // NOLINT(clang-diagnostic-format-nonliteral)
130126
va_end(args);
131127

132128
if (chars < 0) {
@@ -145,8 +141,6 @@ void _log(const char *file, int line, int severity, const char *fmt, ...) {
145141
if (severity < loglevel) {
146142
return;
147143
}
148-
149-
// NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
150144
(void)fprintf(logfile, "%s\n", buff);
151145

152146
if (severity >= LOG_FLUSH_LEVEL) {

src/logging.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#ifndef _LOGGING_H_
22
#define _LOGGING_H_
33

4-
#include <stdio.h> // NOLINT(llvmlibc-restrict-system-libc-headers)
5-
#include <stdlib.h> // NOLINT(llvmlibc-restrict-system-libc-headers)
6-
#include <ev.h> // NOLINT(llvmlibc-restrict-system-libc-headers)
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
#include <ev.h>
77

88
#ifdef __cplusplus
99
extern "C" {

0 commit comments

Comments
 (0)