From 4a4badd4af91c89438bfbd4f381436a8472d5250 Mon Sep 17 00:00:00 2001 From: Mike Gelfand Date: Fri, 25 Dec 2015 10:19:50 +0000 Subject: [PATCH] Ongoing refactoring (use size_t instead of int) --- daemon/remote.c | 4 ++-- gtk/tr-prefs.c | 2 +- libtransmission/ConvertUTF.c | 9 +++++---- libtransmission/ConvertUTF.h | 2 +- libtransmission/announcer-udp.c | 2 +- libtransmission/announcer.c | 4 ++-- libtransmission/platform.c | 2 +- libtransmission/rpc-server.c | 14 +++++++------- libtransmission/rpc-test.c | 8 ++++---- libtransmission/rpcimpl.c | 10 +++++----- libtransmission/rpcimpl.h | 4 ++-- libtransmission/transmission.h | 2 ++ libtransmission/utils-test.c | 24 +++++++++++------------ libtransmission/utils.c | 34 ++++++++++++++++----------------- libtransmission/utils.h | 12 ++++++------ libtransmission/variant.c | 12 ++++++------ libtransmission/variant.h | 2 +- qt/TrackerDelegate.cc | 2 +- utils/edit.c | 2 +- 19 files changed, 77 insertions(+), 74 deletions(-) diff --git a/daemon/remote.c b/daemon/remote.c index 5187ceafe..ea9e1cbf0 100644 --- a/daemon/remote.c +++ b/daemon/remote.c @@ -577,7 +577,7 @@ addDays (tr_variant * args, const tr_quark key, const char * arg) int valueCount; int * values; - values = tr_parseNumberRange (arg, -1, &valueCount); + values = tr_parseNumberRange (arg, TR_BAD_SIZE, &valueCount); for (i=0; i 7) @@ -615,7 +615,7 @@ addFiles (tr_variant * args, { int i; int valueCount; - int * values = tr_parseNumberRange (arg, -1, &valueCount); + int * values = tr_parseNumberRange (arg, TR_BAD_SIZE, &valueCount); for (i=0; i #endif #include /* strlen () */ + +#include "transmission.h" + #include "ConvertUTF.h" static const int halfShift = 10; /* used for shifting by 10 bits */ @@ -350,7 +353,7 @@ Boolean isLegalUTF8Sequence (const UTF8 *source, const UTF8 *sourceEnd) { * In addition to knowing if the sequence is legal, it also tells you the last good character. */ Boolean -tr_utf8_validate (const char * str, int max_len, const char ** end) +tr_utf8_validate (const char * str, size_t max_len, const char ** end) { const UTF8* source = (const UTF8*) str; const UTF8* sourceEnd; @@ -361,7 +364,7 @@ tr_utf8_validate (const char * str, int max_len, const char ** end) if (str == NULL) return false; - sourceEnd = source + ((max_len < 0) ? strlen (str) : (size_t)max_len); + sourceEnd = source + (max_len == TR_BAD_SIZE ? strlen (str) : max_len); if (source == sourceEnd) { @@ -390,8 +393,6 @@ tr_utf8_validate (const char * str, int max_len, const char ** end) return true; } } - - } diff --git a/libtransmission/ConvertUTF.h b/libtransmission/ConvertUTF.h index 8a57fa979..cd21fa931 100644 --- a/libtransmission/ConvertUTF.h +++ b/libtransmission/ConvertUTF.h @@ -151,7 +151,7 @@ Boolean isLegalUTF8Sequence (const UTF8 *source, const UTF8 *sourceEnd); /* intended to work the same as g_utf8_validate */ -Boolean tr_utf8_validate (const char * str, int max_len, const char ** end); +Boolean tr_utf8_validate (const char * str, size_t max_len, const char ** end); #ifdef __cplusplus diff --git a/libtransmission/announcer-udp.c b/libtransmission/announcer-udp.c index 00107cdf9..c3aec353b 100644 --- a/libtransmission/announcer-udp.c +++ b/libtransmission/announcer-udp.c @@ -763,7 +763,7 @@ tau_session_get_tracker (struct tr_announcer_udp * tau, const char * url) struct tau_tracker * tracker = NULL; /* see if we've already got a tracker that matches this host + port */ - tr_urlParse (url, -1, NULL, &host, &port, NULL); + tr_urlParse (url, TR_BAD_SIZE, NULL, &host, &port, NULL); key = tr_strdup_printf ("%s:%d", host, port); for (i=0, n=tr_ptrArraySize (&tau->trackers); !tracker && itrackers, i); diff --git a/libtransmission/announcer.c b/libtransmission/announcer.c index 4af98096b..44eeb9fb2 100644 --- a/libtransmission/announcer.c +++ b/libtransmission/announcer.c @@ -229,7 +229,7 @@ getKey (const char * url) char * host = NULL; int port = 0; - tr_urlParse (url, -1, &scheme, &host, &port, NULL); + tr_urlParse (url, TR_BAD_SIZE, &scheme, &host, &port, NULL); ret = tr_strdup_printf ("%s://%s:%d", (scheme?scheme:"invalid"), (host?host:"invalid"), port); tr_free (host); @@ -583,7 +583,7 @@ filter_trackers (tr_tracker_info * input, int input_count, int * setme_count) char * host; char * path; bool is_duplicate = false; - tr_urlParse (input[i].announce, -1, &scheme, &host, &port, &path); + tr_urlParse (input[i].announce, TR_BAD_SIZE, &scheme, &host, &port, &path); /* weed out one common source of duplicates: * "http://tracker/announce" + diff --git a/libtransmission/platform.c b/libtransmission/platform.c index ec700f271..7b7c3c273 100644 --- a/libtransmission/platform.c +++ b/libtransmission/platform.c @@ -553,7 +553,7 @@ tr_getWebClientDir (const tr_session * session UNUSED) if (end) { if ((end - tmp) > 1) - tr_list_append (&candidates, tr_strndup (tmp, end - tmp)); + tr_list_append (&candidates, tr_strndup (tmp, (size_t) (end - tmp))); tmp = (char *) end + 1; } else if (tmp && *tmp) diff --git a/libtransmission/rpc-server.c b/libtransmission/rpc-server.c index df7e69e50..cd3e0290c 100644 --- a/libtransmission/rpc-server.c +++ b/libtransmission/rpc-server.c @@ -130,9 +130,9 @@ send_simple_response (struct evhttp_request * req, struct tr_mimepart { char * headers; - int headers_len; + size_t headers_len; char * body; - int body_len; + size_t body_len; }; static void @@ -176,9 +176,9 @@ extract_parts_from_multipart (const struct evkeyvalq * headers, if (rnrn) { struct tr_mimepart * p = tr_new (struct tr_mimepart, 1); - p->headers_len = rnrn - part; + p->headers_len = (size_t) (rnrn - part); p->headers = tr_strndup (part, p->headers_len); - p->body_len = (part+part_len) - (rnrn + 4); + p->body_len = (size_t) ((part + part_len) - (rnrn + 4)); p->body = tr_strndup (rnrn+4, p->body_len); tr_ptrArrayAppend (setme_parts, p); } @@ -221,7 +221,7 @@ handle_upload (struct evhttp_request * req, { const struct tr_mimepart * p = tr_ptrArrayNth (&parts, i); const char * ours = get_current_session_id (server); - const int ourlen = strlen (ours); + const size_t ourlen = strlen (ours); hasSessionId = ourlen<=p->body_len && !memcmp (p->body, ours, ourlen); } @@ -237,7 +237,7 @@ handle_upload (struct evhttp_request * req, else for (i=0; ibody_len; + size_t body_len = p->body_len; tr_variant top, *args; tr_variant test; bool have_source = false; @@ -563,7 +563,7 @@ handle_rpc (struct evhttp_request * req, struct tr_rpc_server * server) struct rpc_response_data * data = tr_new0 (struct rpc_response_data, 1); data->req = req; data->server = server; - tr_rpc_request_exec_uri (server->session, q+1, -1, rpc_response_func, data); + tr_rpc_request_exec_uri (server->session, q + 1, TR_BAD_SIZE, rpc_response_func, data); } else { diff --git a/libtransmission/rpc-test.c b/libtransmission/rpc-test.c index 7330f4b88..2f3f0b73c 100644 --- a/libtransmission/rpc-test.c +++ b/libtransmission/rpc-test.c @@ -22,7 +22,7 @@ test_list (void) const char * str; tr_variant top; - tr_rpc_parse_list_str (&top, "12", -1); + tr_rpc_parse_list_str (&top, "12", TR_BAD_SIZE); check (tr_variantIsInt (&top)); check (tr_variantGetInt (&top, &i)); check_int_eq (12, i); @@ -34,7 +34,7 @@ test_list (void) check_int_eq (1, i); tr_variantFree (&top); - tr_rpc_parse_list_str (&top, "6,7", -1); + tr_rpc_parse_list_str (&top, "6,7", TR_BAD_SIZE); check (tr_variantIsList (&top)); check (tr_variantListSize (&top) == 2); check (tr_variantGetInt (tr_variantListChild (&top, 0), &i)); @@ -43,14 +43,14 @@ test_list (void) check_int_eq (7, i); tr_variantFree (&top); - tr_rpc_parse_list_str (&top, "asdf", -1); + tr_rpc_parse_list_str (&top, "asdf", TR_BAD_SIZE); check (tr_variantIsString (&top)); check (tr_variantGetStr (&top, &str, &len)); check_int_eq (4, len); check_streq ("asdf", str); tr_variantFree (&top); - tr_rpc_parse_list_str (&top, "1,3-5", -1); + tr_rpc_parse_list_str (&top, "1,3-5", TR_BAD_SIZE); check (tr_variantIsList (&top)); check (tr_variantListSize (&top) == 4); check (tr_variantGetInt (tr_variantListChild (&top, 0), &i)); diff --git a/libtransmission/rpcimpl.c b/libtransmission/rpcimpl.c index cb09f925d..042818424 100644 --- a/libtransmission/rpcimpl.c +++ b/libtransmission/rpcimpl.c @@ -2299,7 +2299,7 @@ tr_rpc_request_exec_json (tr_session * session, void tr_rpc_parse_list_str (tr_variant * setme, const char * str, - int len) + size_t len) { int valueCount; @@ -2329,14 +2329,14 @@ tr_rpc_parse_list_str (tr_variant * setme, void tr_rpc_request_exec_uri (tr_session * session, const void * request_uri, - int request_len, + size_t request_uri_len, tr_rpc_response_func callback, void * callback_user_data) { const char * pch; tr_variant top; tr_variant * args; - char * request = tr_strndup (request_uri, request_len); + char * request = tr_strndup (request_uri, request_uri_len); tr_variantInitDict (&top, 3); args = tr_variantDictAddDict (&top, TR_KEY_arguments, 0); @@ -2349,11 +2349,11 @@ tr_rpc_request_exec_uri (tr_session * session, const char * next = strchr (pch, '&'); if (delim) { - char * key = tr_strndup (pch, delim - pch); + char * key = tr_strndup (pch, (size_t) (delim - pch)); int isArg = strcmp (key, "method") && strcmp (key, "tag"); tr_variant * parent = isArg ? args : ⊤ - tr_rpc_parse_list_str (tr_variantDictAdd (parent, tr_quark_new (key, delim-pch)), + tr_rpc_parse_list_str (tr_variantDictAdd (parent, tr_quark_new (key, (size_t) (delim - pch))), delim + 1, next ? (size_t)(next - (delim + 1)) : strlen (delim + 1)); tr_free (key); diff --git a/libtransmission/rpcimpl.h b/libtransmission/rpcimpl.h index 147ae75ba..cdc65fa76 100644 --- a/libtransmission/rpcimpl.h +++ b/libtransmission/rpcimpl.h @@ -34,13 +34,13 @@ void tr_rpc_request_exec_json (tr_session * session, /* see the RPC spec's "Request URI Notation" section */ void tr_rpc_request_exec_uri (tr_session * session, const void * request_uri, - int request_len, + size_t request_uri_len, tr_rpc_response_func callback, void * callback_user_data); void tr_rpc_parse_list_str (tr_variant * setme, const char * list_str, - int list_str_len); + size_t list_str_len); #ifdef __cplusplus } diff --git a/libtransmission/transmission.h b/libtransmission/transmission.h index 860548389..06a0834c1 100644 --- a/libtransmission/transmission.h +++ b/libtransmission/transmission.h @@ -89,6 +89,8 @@ extern "C" { #define SHA_DIGEST_LENGTH 20 #define TR_INET6_ADDRSTRLEN 46 + +#define TR_BAD_SIZE ((size_t) -1) typedef uint32_t tr_file_index_t; typedef uint32_t tr_piece_index_t; diff --git a/libtransmission/utils-test.c b/libtransmission/utils-test.c index f7c6f5428..deb679160 100644 --- a/libtransmission/utils-test.c +++ b/libtransmission/utils-test.c @@ -105,7 +105,7 @@ test_utf8 (void) char * out; in = "hello world"; - out = tr_utf8clean (in, -1); + out = tr_utf8clean (in, TR_BAD_SIZE); check_streq (in, out); tr_free (out); @@ -119,14 +119,14 @@ test_utf8 (void) out = tr_utf8clean (in, 17); check (out != NULL); check ((strlen (out) == 17) || (strlen (out) == 33)); - check (tr_utf8_validate (out, -1, NULL)); + check (tr_utf8_validate (out, TR_BAD_SIZE, NULL)); tr_free (out); /* same string, but utf-8 clean */ in = "Трудно быть Богом"; - out = tr_utf8clean (in, -1); + out = tr_utf8clean (in, TR_BAD_SIZE); check (out != NULL); - check (tr_utf8_validate (out, -1, NULL)); + check (tr_utf8_validate (out, TR_BAD_SIZE, NULL)); check_streq (in, out); tr_free (out); @@ -140,7 +140,7 @@ test_numbers (void) int count; int * numbers; - numbers = tr_parseNumberRange ("1-10,13,16-19", -1, &count); + numbers = tr_parseNumberRange ("1-10,13,16-19", TR_BAD_SIZE, &count); check_int_eq (15, count); check_int_eq (1, numbers[0]); check_int_eq (6, numbers[5]); @@ -150,22 +150,22 @@ test_numbers (void) check_int_eq (19, numbers[14]); tr_free (numbers); - numbers = tr_parseNumberRange ("1-5,3-7,2-6", -1, &count); + numbers = tr_parseNumberRange ("1-5,3-7,2-6", TR_BAD_SIZE, &count); check (count == 7); check (numbers != NULL); for (i=0; istr.buf)) + if (len < sizeof (str->str.buf)) { str->type = TR_STRING_TYPE_BUF; memcpy (str->str.buf, bytes, len); @@ -476,7 +476,7 @@ tr_variantInitQuark (tr_variant * v, const tr_quark q) } void -tr_variantInitStr (tr_variant * v, const void * str, int len) +tr_variantInitStr (tr_variant * v, const void * str, size_t len) { tr_variantInit (v, TR_VARIANT_TYPE_STR); tr_variant_string_set_string (&v->val.s, str, len); @@ -598,7 +598,7 @@ tr_variantListAddStr (tr_variant * list, const char * val) { tr_variant * child = tr_variantListAdd (list); - tr_variantInitStr (child, val, -1); + tr_variantInitStr (child, val, TR_BAD_SIZE); return child; } @@ -727,7 +727,7 @@ tr_variantDictAddStr (tr_variant * dict, const char * val) { tr_variant * child = dictFindOrAdd (dict, key, TR_VARIANT_TYPE_STR); - tr_variantInitStr (child, val, -1); + tr_variantInitStr (child, val, TR_BAD_SIZE); return child; } diff --git a/libtransmission/variant.h b/libtransmission/variant.h index 60ec5ed72..e46b78149 100644 --- a/libtransmission/variant.h +++ b/libtransmission/variant.h @@ -206,7 +206,7 @@ bool tr_variantGetStr (const tr_variant * variant, void tr_variantInitStr (tr_variant * initme, const void * str, - int str_len); + size_t str_len); void tr_variantInitQuark (tr_variant * initme, const tr_quark quark); diff --git a/qt/TrackerDelegate.cc b/qt/TrackerDelegate.cc index 23018734e..f538d8837 100644 --- a/qt/TrackerDelegate.cc +++ b/qt/TrackerDelegate.cc @@ -193,7 +193,7 @@ TrackerDelegate::getText (const TrackerInfo& inf) const str += inf.st.isBackup ? QLatin1String ("") : QLatin1String (""); char * host = NULL; int port = 0; - tr_urlParse (inf.st.announce.toUtf8().constData(), -1, NULL, &host, &port, NULL); + tr_urlParse (inf.st.announce.toUtf8().constData(), TR_BAD_SIZE, NULL, &host, &port, NULL); str += QString::fromLatin1 ("%1:%2").arg (QString::fromUtf8 (host)).arg (port); tr_free (host); if (!key.isEmpty()) str += QLatin1String (" - ") + key; diff --git a/utils/edit.c b/utils/edit.c index 6c2b186ef..0ce6eaa5c 100644 --- a/utils/edit.c +++ b/utils/edit.c @@ -211,7 +211,7 @@ replaceURL (tr_variant * metainfo, const char * in, const char * out) char * newstr = replaceSubstr (str, in, out); printf ("\tReplaced in \"announce-list\" tier %d: \"%s\" --> \"%s\"\n", tierCount, str, newstr); tr_variantFree (node); - tr_variantInitStr (node, newstr, -1); + tr_variantInitStr (node, newstr, TR_BAD_SIZE); tr_free (newstr); changed = true; } -- 2.40.0