From 1f9494979268b62ed4b4588fb7d9e790c0e12100 Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Mon, 26 Oct 2026 22:32:52 +0100 Subject: replace all calls to sprintf with a format helper that advances a cursor. Also convert the meta-formatter (i.e. results printer) to this scheme --- ot_fullscrape.c | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) (limited to 'ot_fullscrape.c') diff --git a/ot_fullscrape.c b/ot_fullscrape.c index 2ba3b24..8518bd2 100644 --- a/ot_fullscrape.c +++ b/ot_fullscrape.c @@ -18,6 +18,7 @@ #endif /* Opentracker */ +#include "ot_fmt.h" #include "ot_hex.h" #include "ot_fullscrape.h" #include "ot_iovec.h" @@ -83,10 +84,15 @@ void fullscrape_deliver( int64_t sock, ot_tasktype tasktype ) { mutex_workqueue_pushtask( sock, TASK_FULLSCRAPE | tasktype ); } -static char * fullscrape_write_one( ot_tasktype mode, char *r, ot_torrent *torrent, ot_hash *hash ) { - size_t seed_count = torrent->peer_list6->seed_count + torrent->peer_list4->seed_count; - size_t peer_count = torrent->peer_list6->peer_count + torrent->peer_list4->peer_count; - size_t down_count = torrent->peer_list6->down_count + torrent->peer_list4->down_count; +static void fullscrape_write_one( ot_tasktype mode, char **cursor, ot_torrent *torrent, ot_hash *hash ) { + /* Callers guarantee this much room: the chunk writer keeps its low + watermark that far from the end, the compressors hand over a buffer of + exactly that size */ + char *r = *cursor; + const char *r_end = r + OT_SCRAPE_MAXENTRYLEN; + size_t seed_count = torrent->peer_list6->seed_count + torrent->peer_list4->seed_count; + size_t peer_count = torrent->peer_list6->peer_count + torrent->peer_list4->peer_count; + size_t down_count = torrent->peer_list6->down_count + torrent->peer_list4->down_count; switch (mode & TASK_TASK_MASK) { case TASK_FULLSCRAPE: @@ -98,18 +104,18 @@ static char * fullscrape_write_one( ot_tasktype mode, char *r, ot_torrent *torre memcpy(r, hash, sizeof(ot_hash)); r += sizeof(ot_hash); /* push rest of the scrape string */ - r += sprintf(r, "d8:completei%zde10:downloadedi%zde10:incompletei%zdee", seed_count, down_count, peer_count - seed_count); + fmt_append(&r, r_end, "d8:completei%zde10:downloadedi%zde10:incompletei%zdee", seed_count, down_count, peer_count - seed_count); break; case TASK_FULLSCRAPE_TPB_ASCII: hex_from_hash(r, *hash); r += 2 * sizeof(ot_hash); - r += sprintf(r, ":%zd:%zd\n", seed_count, peer_count - seed_count); + fmt_append(&r, r_end, ":%zd:%zd\n", seed_count, peer_count - seed_count); break; case TASK_FULLSCRAPE_TPB_ASCII_PLUS: hex_from_hash(r, *hash); r += 2 * sizeof(ot_hash); - r += sprintf(r, ":%zd:%zd:%zd\n", seed_count, peer_count - seed_count, down_count); + fmt_append(&r, r_end, ":%zd:%zd:%zd\n", seed_count, peer_count - seed_count, down_count); break; case TASK_FULLSCRAPE_TPB_BINARY: memcpy(r, *hash, sizeof(ot_hash)); @@ -120,15 +126,15 @@ static char * fullscrape_write_one( ot_tasktype mode, char *r, ot_torrent *torre break; case TASK_FULLSCRAPE_TPB_URLENCODED: r += urlencoded_format(r, *hash, sizeof(ot_hash)); - r += sprintf(r, ":%zd:%zd\n", seed_count, peer_count - seed_count); + fmt_append(&r, r_end, ":%zd:%zd\n", seed_count, peer_count - seed_count); break; case TASK_FULLSCRAPE_TRACKERSTATE: hex_from_hash(r, *hash); r += 2 * sizeof(ot_hash); - r += sprintf(r, ":%zd:%zd\n", torrent->peer_list6->base, down_count); + fmt_append(&r, r_end, ":%zd:%zd\n", torrent->peer_list6->base, down_count); break; } - return r; + *cursor = r; } static void fullscrape_make(int taskid, ot_tasktype mode) { @@ -145,7 +151,7 @@ static void fullscrape_make(int taskid, ot_tasktype mode) { re = r + OT_SCRAPE_CHUNK_SIZE - OT_SCRAPE_MAXENTRYLEN; if ((mode & TASK_TASK_MASK) == TASK_FULLSCRAPE) - r += sprintf(r, "d5:filesd"); + fmt_append(&r, (char *)iovector.iov_base + OT_SCRAPE_CHUNK_SIZE, "d5:filesd"); /* For each bucket... */ for (bucket = 0; bucket < OT_BUCKET_COUNT; ++bucket) { @@ -156,7 +162,7 @@ static void fullscrape_make(int taskid, ot_tasktype mode) { /* For each torrent in this bucket.. */ for (i = 0; i < torrents_list->size; ++i) { - r = fullscrape_write_one(mode, r, torrents + i, &torrents[i].hash); + fullscrape_write_one(mode, &r, torrents + i, &torrents[i].hash); if (r > re) { iovector.iov_len = r - (char *)iovector.iov_base; @@ -187,7 +193,7 @@ static void fullscrape_make(int taskid, ot_tasktype mode) { } if ((mode & TASK_TASK_MASK) == TASK_FULLSCRAPE) - r += sprintf(r, "ee"); + fmt_append(&r, (char *)iovector.iov_base + OT_SCRAPE_CHUNK_SIZE, "ee"); /* Send rest of data */ iovector.iov_len = r - (char *)iovector.iov_base; @@ -230,7 +236,8 @@ static void fullscrape_make_gzip(int taskid, ot_tasktype mode) { /* For each torrent in this bucket.. */ for (i = 0; i < torrents_list->size; ++i) { char compress_buffer[OT_SCRAPE_MAXENTRYLEN]; - r = fullscrape_write_one(mode, compress_buffer, torrents + i, &torrents[i].hash); + r = compress_buffer; + fullscrape_write_one(mode, &r, torrents + i, &torrents[i].hash); strm.next_in = (uint8_t *)compress_buffer; strm.avail_in = r - compress_buffer; zres = deflate(&strm, Z_NO_FLUSH); @@ -359,7 +366,8 @@ static void fullscrape_make_zstd(int taskid, ot_tasktype mode) { /* For each torrent in this bucket.. */ for (i = 0; i < torrents_list->size; ++i) { char compress_buffer[OT_SCRAPE_MAXENTRYLEN]; - r = fullscrape_write_one(mode, compress_buffer, torrents + i, &torrents[i].hash); + r = compress_buffer; + fullscrape_write_one(mode, &r, torrents + i, &torrents[i].hash); inbuf.src = compress_buffer; inbuf.size = r - compress_buffer; inbuf.pos = 0; -- cgit v1.3