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 --- trackerlogic.c | 55 +++++++++++++++++++++++++++---------------------------- 1 file changed, 27 insertions(+), 28 deletions(-) (limited to 'trackerlogic.c') diff --git a/trackerlogic.c b/trackerlogic.c index 9705bd6..1f38f07 100644 --- a/trackerlogic.c +++ b/trackerlogic.c @@ -14,6 +14,7 @@ /* Opentracker */ #include "ot_accesslist.h" #include "ot_clean.h" +#include "ot_fmt.h" #include "ot_fullscrape.h" #include "ot_http.h" #include "ot_ip.h" @@ -299,11 +300,12 @@ static size_t return_peers_for_torrent_udp(struct ot_workstruct *ws, ot_torrent } static size_t return_peers_for_torrent_tcp(struct ot_workstruct *ws, ot_torrent *torrent, size_t amount, char *reply) { - char *r = reply; - int erval = OT_CLIENT_REQUEST_INTERVAL_RANDOM; - size_t seed_count = torrent->peer_list6->seed_count + torrent->peer_list4->seed_count; - size_t down_count = torrent->peer_list6->down_count + torrent->peer_list4->down_count; - size_t peer_count = torrent->peer_list6->peer_count + torrent->peer_list4->peer_count - seed_count; + char *r = reply; + const char *r_end = ws->outbuf + G_OUTBUF_SIZE; + int erval = OT_CLIENT_REQUEST_INTERVAL_RANDOM; + size_t seed_count = torrent->peer_list6->seed_count + torrent->peer_list4->seed_count; + size_t down_count = torrent->peer_list6->down_count + torrent->peer_list4->down_count; + size_t peer_count = torrent->peer_list6->peer_count + torrent->peer_list4->peer_count - seed_count; /* Simple case: amount of peers in both lists is less than requested, here we return all results */ size_t amount_v4 = torrent->peer_list4->peer_count; @@ -341,11 +343,11 @@ static size_t return_peers_for_torrent_tcp(struct ot_workstruct *ws, ot_torrent ++amount_v4; } - r += - sprintf(r, "d8:completei%zde10:downloadedi%zde10:incompletei%zde8:intervali%ie12:min intervali%ie", seed_count, down_count, peer_count, erval, erval / 2); + fmt_append(&r, r_end, "d8:completei%zde10:downloadedi%zde10:incompletei%zde8:intervali%ie12:min intervali%ie", seed_count, down_count, peer_count, erval, + erval / 2); if (amount_v4) { - r += sprintf(r, PEERS_BENCODED4 "%zd:", OT_PEER_COMPARE_SIZE4 * amount_v4); + fmt_append(&r, r_end, PEERS_BENCODED4 "%zd:", OT_PEER_COMPARE_SIZE4 * amount_v4); if (amount_v4 == torrent->peer_list4->peer_count) r += return_peers_all(torrent->peer_list4, OT_PEER_SIZE4, r); else @@ -353,14 +355,14 @@ static size_t return_peers_for_torrent_tcp(struct ot_workstruct *ws, ot_torrent } if (amount_v6) { - r += sprintf(r, PEERS_BENCODED6 "%zd:", OT_PEER_COMPARE_SIZE6 * amount_v6); + fmt_append(&r, r_end, PEERS_BENCODED6 "%zd:", OT_PEER_COMPARE_SIZE6 * amount_v6); if (amount_v6 == torrent->peer_list6->peer_count) r += return_peers_all(torrent->peer_list6, OT_PEER_SIZE6, r); else r += return_peers_selection(ws, torrent->peer_list6, OT_PEER_SIZE6, amount_v6, r); } - *r++ = 'e'; + fmt_append(&r, r_end, "e"); return r - reply; } @@ -403,12 +405,11 @@ size_t return_udp_scrape_for_torrent(ot_hash const hash, char *reply) { } /* Fetches scrape info for a specific torrent */ -size_t return_tcp_scrape_for_torrent(ot_hash const *hash_list, size_t amount, char *reply) { - char *r = reply; - int exactmatch; +void return_tcp_scrape_for_torrent(ot_hash const *hash_list, size_t amount, char **reply, const char *reply_end) { + int exactmatch; size_t i; - r += sprintf(r, "d5:filesd"); + fmt_append(reply, reply_end, "d5:filesd"); for (i = 0; i < amount; ++i) { int delta_torrentcount = 0; @@ -421,22 +422,17 @@ size_t return_tcp_scrape_for_torrent(ot_hash const *hash_list, size_t amount, ch vector_remove_torrent(torrents_list, torrent); delta_torrentcount = -1; } else { - *r++ = '2'; - *r++ = '0'; - *r++ = ':'; - memcpy(r, hash, sizeof(ot_hash)); - r += sizeof(ot_hash); - r += sprintf(r, "d8:completei%zde10:downloadedi%zde10:incompletei%zdee", torrent->peer_list6->seed_count + torrent->peer_list4->seed_count, - torrent->peer_list6->down_count + torrent->peer_list4->down_count, - torrent->peer_list6->peer_count + torrent->peer_list4->peer_count - torrent->peer_list6->seed_count - torrent->peer_list4->seed_count); + fmt_append(reply, reply_end, "20:"); + fmt_bytes(reply, reply_end, hash, sizeof(ot_hash)); + fmt_append(reply, reply_end, "d8:completei%zde10:downloadedi%zde10:incompletei%zdee", + torrent->peer_list6->seed_count + torrent->peer_list4->seed_count, torrent->peer_list6->down_count + torrent->peer_list4->down_count, + torrent->peer_list6->peer_count + torrent->peer_list4->peer_count - torrent->peer_list6->seed_count - torrent->peer_list4->seed_count); } } mutex_bucket_unlock_by_hash(*hash, delta_torrentcount); } - *r++ = 'e'; - *r++ = 'e'; - return r - reply; + fmt_append(reply, reply_end, "ee"); } static ot_peerlist dummy_list; @@ -472,9 +468,12 @@ size_t remove_peer_from_torrent(PROTO_FLAG proto, struct ot_workstruct *ws) { } if (proto == FLAG_TCP) { - int erval = OT_CLIENT_REQUEST_INTERVAL_RANDOM; - ws->reply_size = sprintf(ws->reply, "d8:completei%zde10:incompletei%zde8:intervali%ie12:min intervali%ie%s0:e", seed_count, peer_count - seed_count, erval, - erval / 2, peer_size == OT_PEER_SIZE6 ? PEERS_BENCODED6 : PEERS_BENCODED4); + int erval = OT_CLIENT_REQUEST_INTERVAL_RANDOM; + char *r = ws->reply; + + fmt_append(&r, ws->outbuf + G_OUTBUF_SIZE, "d8:completei%zde10:incompletei%zde8:intervali%ie12:min intervali%ie%s0:e", seed_count, peer_count - seed_count, + erval, erval / 2, peer_size == OT_PEER_SIZE6 ? PEERS_BENCODED6 : PEERS_BENCODED4); + ws->reply_size = r - ws->reply; } /* Handle UDP reply */ -- cgit v1.3