From 5bc4c0663576fd99bb06068e6dfd624690865c89 Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Sun, 25 Oct 2026 19:42:27 +0100 Subject: Be stricter about const char*, Wwrite-strings will warn in the future and we don't want to try to overwrite string literals --- Makefile | 6 +++--- opentracker.c | 2 +- ot_accesslist.c | 8 ++++---- ot_accesslist.h | 6 +++--- ot_config.c | 12 ++++++------ ot_config.h | 8 ++++---- ot_http.c | 10 +++++----- ot_http.h | 2 +- ot_stats.c | 2 +- proxy.c | 2 +- scan_urlencoded_query.h | 4 ++-- tests/runner.c | 1 + tests/stubs.c | 9 +++++++++ tests/test.h | 1 + trackerlogic.c | 2 +- trackerlogic.h | 2 +- 16 files changed, 44 insertions(+), 33 deletions(-) diff --git a/Makefile b/Makefile index 55be5ba..00a9f93 100644 --- a/Makefile +++ b/Makefile @@ -64,7 +64,7 @@ GIT_VERSION=`sh -c 'command -v git >/dev/null && test -d .git && git rev-parse H OPTS_debug=-D_DEBUG -g -ggdb # -pg -fprofile-arcs -ftest-coverage OPTS_production=-O3 -CFLAGS+=-I$(LIBOWFAT_HEADERS) -DGIT_VERSION=$(GIT_VERSION) -Wall -pipe -pthread -Wextra #-ansi -pedantic +CFLAGS+=-I$(LIBOWFAT_HEADERS) -DGIT_VERSION=$(GIT_VERSION) -Wall -pipe -pthread -Wextra -Wwrite-strings #-ansi -pedantic LDFLAGS+=-L$(LIBOWFAT_LIBRARY) -lowfat -pthread BINARY =opentracker @@ -119,8 +119,8 @@ $(OBJECTS_proxy_debug): $(HEADERS) # has to. tests/stubs.c supplies the few symbols those modules reference but # the tests never reach. TESTBIN=tests/runner -TEST_SOURCES=tests/runner.c tests/test.c tests/stubs.c tests/t_scan_urlencoded_query.c tests/t_iovec.c tests/t_vector.c tests/t_random.c tests/t_hex.c tests/t_accesslist.c tests/t_scan.c tests/t_buf.c tests/t_ip.c tests/t_socket.c -TEST_UNITS=scan_urlencoded_query.c ot_buf.c ot_ip.c ot_socket.c ot_iovec.c ot_vector.c ot_random.c ot_hex.c ot_scan.c ot_accesslist.c +TEST_SOURCES=tests/runner.c tests/test.c tests/stubs.c tests/t_scan_urlencoded_query.c tests/t_iovec.c tests/t_vector.c tests/t_random.c tests/t_hex.c tests/t_accesslist.c tests/t_scan.c tests/t_buf.c tests/t_ip.c tests/t_socket.c tests/t_mutex.c +TEST_UNITS=scan_urlencoded_query.c ot_buf.c ot_ip.c ot_socket.c ot_iovec.c ot_vector.c ot_random.c ot_hex.c ot_scan.c ot_accesslist.c ot_mutex.c TEST_HEADERS=tests/test.h $(HEADERS) # Tests reach code that sits behind feature flags, so the units under test are # built with those features enabled. A test binary is not a production binary. diff --git a/opentracker.c b/opentracker.c index 0af0d17..96a265a 100644 --- a/opentracker.c +++ b/opentracker.c @@ -305,7 +305,7 @@ static void listen_bind_one(ot_listen *entry) { #ifdef _DEBUG { - char *protos[] = {"TCP", "UDP", "UDP mcast"}; + const char *protos[] = {"TCP", "UDP", "UDP mcast"}; char _debug[512]; int off = snprintf(_debug, sizeof(_debug), "Binding socket type %s to address [", protos[entry->proto]); off += ip_format(_debug + off, entry->ip); diff --git a/ot_accesslist.c b/ot_accesslist.c index 2465ada..d7e88ee 100644 --- a/ot_accesslist.c +++ b/ot_accesslist.c @@ -32,10 +32,10 @@ /* GLOBAL VARIABLES */ #ifdef WANT_ACCESSLIST -char *g_accesslist_filename = NULL; +const char *g_accesslist_filename = NULL; #ifdef WANT_DYNAMIC_ACCESSLIST -char *g_accesslist_pipe_add = NULL; -char *g_accesslist_pipe_delete = NULL; +const char *g_accesslist_pipe_add = NULL; +const char *g_accesslist_pipe_delete = NULL; #endif static pthread_mutex_t g_accesslist_mutex; @@ -272,7 +272,7 @@ static void *accesslist_worker(void *args) { #ifdef WANT_DYNAMIC_ACCESSLIST static pthread_t thread_adder_id, thread_deleter_id; -static void *accesslist_adddel_worker(char *fifoname, ot_accesslist *_Atomic *adding_to, ot_accesslist *_Atomic *removing_from) { +static void *accesslist_adddel_worker(const char *fifoname, ot_accesslist *_Atomic *adding_to, ot_accesslist *_Atomic *removing_from) { struct stat st; if (!stat(fifoname, &st)) { diff --git a/ot_accesslist.h b/ot_accesslist.h index 30aaebd..9dfdd95 100644 --- a/ot_accesslist.h +++ b/ot_accesslist.h @@ -23,10 +23,10 @@ void accesslist_cleanup(void); line, and return how many were written to dest */ size_t accesslist_parse_hashes(ot_hash *dest, size_t capacity, const char *buf, size_t len); -extern char *g_accesslist_filename; +extern const char *g_accesslist_filename; #ifdef WANT_DYNAMIC_ACCESSLIST -extern char *g_accesslist_pipe_add; -extern char *g_accesslist_pipe_delete; +extern const char *g_accesslist_pipe_add; +extern const char *g_accesslist_pipe_delete; #endif #else diff --git a/ot_config.c b/ot_config.c index 43c07c5..e9666f9 100644 --- a/ot_config.c +++ b/ot_config.c @@ -22,10 +22,10 @@ #include "ot_stats.h" #include "trackerlogic.h" -char *g_redirecturl; -char *g_serverdir; -char *g_serveruser; -char *g_statefile; +const char *g_redirecturl; +const char *g_serverdir; +const char *g_serveruser; +const char *g_statefile; #define OT_LISTEN_INHERIT (-1) @@ -122,13 +122,13 @@ static int listen_scan_options(char *p, int *shared, int *workers) { } -char *set_config_option(char **option, char *value) { +static const char *set_config_option(const char **option, char *value) { #ifdef _DEBUG fprintf(stderr, "Setting config option: %s\n", value); #endif while (isspace(*value)) ++value; - free(*option); + free((char *)*option); return *option = strdup(value); } diff --git a/ot_config.h b/ot_config.h index ab25e2c..059573a 100644 --- a/ot_config.h +++ b/ot_config.h @@ -22,10 +22,10 @@ typedef struct { int64_t sock; } ot_listen; -extern char *g_redirecturl; -extern char *g_serverdir; -extern char *g_serveruser; -extern char *g_statefile; +extern const char *g_redirecturl; +extern const char *g_serverdir; +extern const char *g_serveruser; +extern const char *g_statefile; /* Reads the command line and every config file it names. Exits on error. */ void config_parse(int argc, char **argv); diff --git a/ot_http.c b/ot_http.c index 6448860..fc27eb8 100644 --- a/ot_http.c +++ b/ot_http.c @@ -37,9 +37,9 @@ #define OT_MAXMULTISCRAPE_COUNT 64 #define OT_BATCH_LIMIT (1024 * 1024 * 16) -extern char *g_redirecturl; +extern const char *g_redirecturl; -char *g_stats_path; +const char *g_stats_path; ssize_t g_stats_path_len; enum { SUCCESS_HTTP_HEADER_LENGTH = 80, SUCCESS_HTTP_SIZE_OFF = 17 }; @@ -125,9 +125,9 @@ static void http_senddata(const int64_t sock, struct ot_workstruct *ws) { #define HTTPERROR_404 return http_issue_error(sock, ws, CODE_HTTPERROR_404) #define HTTPERROR_500 return http_issue_error(sock, ws, CODE_HTTPERROR_500) ssize_t http_issue_error(const int64_t sock, struct ot_workstruct *ws, int code) { - char *error_code[] = {"302 Found", "400 Invalid Request", "400 Invalid Request", "400 Invalid Request", "402 Payment Required", + const char *error_code[] = {"302 Found", "400 Invalid Request", "400 Invalid Request", "400 Invalid Request", "402 Payment Required", "403 Not Modest", "403 Access Denied", "404 Not Found", "500 Internal Server Error"}; - char *title = error_code[code]; + const char *title = error_code[code]; ws->reply = ws->outbuf; if (code == CODE_HTTPERROR_302) @@ -491,7 +491,7 @@ unsigned long long numwants[201]; #endif #if defined(WANT_KEEPALIVE) || defined(WANT_IP_FROM_PROXY) -static char *http_header(char *data, size_t byte_count, char *header) { +static char *http_header(char *data, size_t byte_count, const char *header) { size_t i; long sl = strlen(header); for (i = 0; i + sl + 2 < byte_count; ++i) { diff --git a/ot_http.h b/ot_http.h index 965820f..abad83a 100644 --- a/ot_http.h +++ b/ot_http.h @@ -37,7 +37,7 @@ ssize_t http_handle_request(const int64_t s, struct ot_workstruct *ws); ssize_t http_sendiovecdata(const int64_t s, struct ot_workstruct *ws, size_t iovec_entries, struct iovec *iovector, int is_partial); ssize_t http_issue_error(const int64_t s, struct ot_workstruct *ws, int code); -extern char *g_stats_path; +extern const char *g_stats_path; extern ssize_t g_stats_path_len; #endif diff --git a/ot_stats.c b/ot_stats.c index f938e91..6589aef 100644 --- a/ot_stats.c +++ b/ot_stats.c @@ -53,7 +53,7 @@ static unsigned long long ot_full_scrape_count; static unsigned long long ot_full_scrape_request_count; static unsigned long long ot_full_scrape_size; static unsigned long long ot_failed_request_counts[CODE_HTTPERROR_COUNT]; -static char *ot_failed_request_names[] = { +static const char *ot_failed_request_names[] = { "302 Redirect", "400 Parse Error", "400 Invalid Parameter", "400 Invalid Parameter (compact=0)", "400 Not Modest", "402 Payment Required", "403 Access Denied", "404 Not found", "500 Internal Server Error"}; static unsigned long long ot_renewed[OT_PEER_TIMEOUT]; diff --git a/proxy.c b/proxy.c index a870c63..219d926 100644 --- a/proxy.c +++ b/proxy.c @@ -72,7 +72,7 @@ static void *livesync_worker(void *args); static void *streamsync_worker(void *args); static void livesync_proxytell(uint8_t prefix, uint8_t *info_hash, uint8_t *peer); -void exerr(char *message) { +void exerr(const char *message) { fprintf(stderr, "%s\n", message); exit(111); } diff --git a/scan_urlencoded_query.h b/scan_urlencoded_query.h index d0929cf..f71752d 100644 --- a/scan_urlencoded_query.h +++ b/scan_urlencoded_query.h @@ -11,8 +11,8 @@ #include typedef struct { - char *key; - int value; + const char *key; + int value; } ot_keywords; typedef enum { diff --git a/tests/runner.c b/tests/runner.c index db62246..972a56a 100644 --- a/tests/runner.c +++ b/tests/runner.c @@ -23,6 +23,7 @@ static const struct module modules[] = {{"scan_urlencoded_query", test_scan_urle {"ot_buf", test_buf}, {"ot_ip", test_ip}, {"ot_socket", test_socket}, + {"ot_mutex", test_mutex}, {"ot_accesslist", test_accesslist}, {NULL, NULL}}; diff --git a/tests/stubs.c b/tests/stubs.c index ce93071..09a7312 100644 --- a/tests/stubs.c +++ b/tests/stubs.c @@ -18,3 +18,12 @@ void free_peerlist(ot_peerlist *peer_list) { (void)peer_list; } neither of which any test drives. */ time_t g_now_seconds; volatile int g_opentracker_running; + +/* ot_mutex.c wakes the main loop through the self pipe when a result is + ready. The queue tests never get that far. */ +int g_self_pipe[2] = {-1, -1}; +long io_trywrite(long d, const char *buf, long len) { + (void)d; + (void)buf; + return len; +} diff --git a/tests/test.h b/tests/test.h index d79c4d2..9677524 100644 --- a/tests/test.h +++ b/tests/test.h @@ -86,5 +86,6 @@ void test_scan(void); void test_buf(void); void test_ip(void); void test_socket(void); +void test_mutex(void); #endif diff --git a/trackerlogic.c b/trackerlogic.c index 52c8c18..0fa7bfb 100644 --- a/trackerlogic.c +++ b/trackerlogic.c @@ -550,7 +550,7 @@ void trackerlogic_add_random_torrents(size_t amount) { } #endif -void exerr(char *message) { +void exerr(const char *message) { fprintf(stderr, "%s\n", message); exit(111); } diff --git a/trackerlogic.h b/trackerlogic.h index aa10a54..42018c3 100644 --- a/trackerlogic.h +++ b/trackerlogic.h @@ -181,7 +181,7 @@ struct ot_workstruct { void trackerlogic_init(void); void trackerlogic_deinit(void); -void exerr(char *message); +void exerr(const char *message); /* add_peer_to_torrent does only release the torrent bucket if from_sync is set, otherwise it is released in return_peers_for_torrent */ -- cgit v1.3