diff --git a/gtk/hig.c b/gtk/hig.c index 8b92111e7..bfd994b62 100644 --- a/gtk/hig.c +++ b/gtk/hig.c @@ -84,14 +84,13 @@ void hig_workarea_add_label_w(GtkWidget* t, guint row, GtkWidget* w) gtk_widget_set_margin_left(w, 18); #endif - if (GTK_IS_MISC(w)) - { - g_object_set(w, "halign", GTK_ALIGN_START, "valign", GTK_ALIGN_CENTER, NULL); - } - if (GTK_IS_LABEL(w)) { - gtk_label_set_use_markup(GTK_LABEL(w), TRUE); + g_object_set(w, + "halign", GTK_ALIGN_START, + "valign", GTK_ALIGN_CENTER, + "use-markup", TRUE, + NULL); } gtk_grid_attach(GTK_GRID(t), w, 0, row, 1, 1); @@ -99,9 +98,12 @@ void hig_workarea_add_label_w(GtkWidget* t, guint row, GtkWidget* w) static void hig_workarea_add_tall_control(GtkWidget* t, guint row, GtkWidget* control) { - if (GTK_IS_MISC(control)) + if (GTK_IS_LABEL(control)) { - g_object_set(control, "halign", GTK_ALIGN_START, "valign", GTK_ALIGN_CENTER, NULL); + g_object_set(control, + "halign", GTK_ALIGN_START, + "valign", GTK_ALIGN_CENTER, + NULL); } g_object_set(control, "expand", TRUE, NULL); @@ -110,9 +112,12 @@ static void hig_workarea_add_tall_control(GtkWidget* t, guint row, GtkWidget* co static void hig_workarea_add_control(GtkWidget* t, guint row, GtkWidget* control) { - if (GTK_IS_MISC(control)) + if (GTK_IS_LABEL(control)) { - g_object_set(control, "halign", GTK_ALIGN_START, "valign", GTK_ALIGN_CENTER, NULL); + g_object_set(control, + "halign", GTK_ALIGN_START, + "valign", GTK_ALIGN_CENTER, + NULL); } gtk_widget_set_hexpand(control, TRUE); diff --git a/gtk/tr-icon.c b/gtk/tr-icon.c index 070125ac6..02df1f9f2 100644 --- a/gtk/tr-icon.c +++ b/gtk/tr-icon.c @@ -46,6 +46,10 @@ static void popup(GtkStatusIcon* self, guint button, guint when, gpointer data) #if GTK_CHECK_VERSION(3, 22, 0) gtk_menu_popup_at_pointer(GTK_MENU(w), NULL); + + TR_UNUSED(self); + TR_UNUSED(button); + TR_UNUSED(when); #else gtk_menu_popup(GTK_MENU(w), NULL, NULL, gtk_status_icon_position_menu, self, button, when); #endif diff --git a/libtransmission/announcer.c b/libtransmission/announcer.c index 19332a718..09a047d76 100644 --- a/libtransmission/announcer.c +++ b/libtransmission/announcer.c @@ -1931,8 +1931,9 @@ static void copy_tier_attributes(struct tr_torrent_tiers* tt, tr_tier const* src { for (int j = 0; !found && j < tt->tiers[i].tracker_count; ++j) { - if ((found = tr_strcmp0(src->currentTracker->announce, tt->tiers[i].trackers[j].announce) == 0)) + if ((tr_strcmp0(src->currentTracker->announce, tt->tiers[i].trackers[j].announce) == 0)) { + found = true; copy_tier_attributes_impl(&tt->tiers[i], j, src); } } diff --git a/libtransmission/fdlimit.c b/libtransmission/fdlimit.c index bfdd49254..c6b014981 100644 --- a/libtransmission/fdlimit.c +++ b/libtransmission/fdlimit.c @@ -445,11 +445,11 @@ tr_sys_file_t tr_fdFileGetCached(tr_session* s, int torrent_id, tr_file_index_t bool tr_fdFileGetCachedMTime(tr_session* s, int torrent_id, tr_file_index_t i, time_t* mtime) { - bool success; - tr_sys_path_info info; - struct tr_cached_file* o = fileset_lookup(get_fileset(s), torrent_id, i); + struct tr_cached_file const* o = fileset_lookup(get_fileset(s), torrent_id, i); + tr_sys_path_info info = { 0 }; + bool const success = o != NULL && tr_sys_file_get_info(o->fd, &info, NULL); - if ((success = o != NULL && tr_sys_file_get_info(o->fd, &info, NULL))) + if (success) { *mtime = info.last_modified_at; } diff --git a/libtransmission/net.c b/libtransmission/net.c index f4abf48f4..b82a07f46 100644 --- a/libtransmission/net.c +++ b/libtransmission/net.c @@ -114,22 +114,20 @@ char const* tr_address_to_string(tr_address const* addr) bool tr_address_from_string(tr_address* dst, char const* src) { - bool ok; + bool success = false; - if ((ok = evutil_inet_pton(AF_INET, src, &dst->addr) == 1)) + if (evutil_inet_pton(AF_INET, src, &dst->addr) == 1) { dst->type = TR_AF_INET; + success = true; } - - if (!ok) /* try IPv6 */ + else if (evutil_inet_pton(AF_INET6, src, &dst->addr) == 1) { - if ((ok = evutil_inet_pton(AF_INET6, src, &dst->addr) == 1)) - { - dst->type = TR_AF_INET6; - } + dst->type = TR_AF_INET6; + success = true; } - return ok; + return success; } /* diff --git a/libtransmission/peer-io.c b/libtransmission/peer-io.c index 26c7abfa9..86d4c5a3e 100644 --- a/libtransmission/peer-io.c +++ b/libtransmission/peer-io.c @@ -1055,13 +1055,15 @@ void tr_peerIoSetPeersId(tr_peerIo* io, uint8_t const* peer_id) { TR_ASSERT(tr_isPeerIo(io)); - if ((io->peerIdIsSet = peer_id != NULL)) + if (peer_id == NULL) { - memcpy(io->peerId, peer_id, 20); + memset(io->peerId, '\0', sizeof(io->peerId)); + io->peerIdIsSet = false; } else { - memset(io->peerId, '\0', 20); + memcpy(io->peerId, peer_id, sizeof(io->peerId)); + io->peerIdIsSet = true; } } diff --git a/libtransmission/port-forwarding.c b/libtransmission/port-forwarding.c index e038dfb93..f6e60dd63 100644 --- a/libtransmission/port-forwarding.c +++ b/libtransmission/port-forwarding.c @@ -226,14 +226,16 @@ static void start_timer(tr_shared* s) set_evtimer_from_status(s); } -void tr_sharedTraversalEnable(tr_shared* s, bool isEnabled) +void tr_sharedTraversalEnable(tr_shared* s, bool enable) { - if ((s->isEnabled = isEnabled)) + if (enable) { + s->isEnabled = true; start_timer(s); } else { + s->isEnabled = false; stop_forwarding(s); } } diff --git a/libtransmission/torrent-magnet.c b/libtransmission/torrent-magnet.c index 33e820f26..2e85dafcf 100644 --- a/libtransmission/torrent-magnet.c +++ b/libtransmission/torrent-magnet.c @@ -6,6 +6,7 @@ * */ +#include /* INT_MAX */ #include /* memcpy(), memset(), memcmp() */ #include @@ -268,7 +269,6 @@ void tr_torrentSetMetadataPiece(tr_torrent* tor, int piece, void const* data, in if (m->piecesNeededCount == 0) { bool success = false; - bool checksumPassed = false; bool metainfoParsed = false; uint8_t sha1[SHA_DIGEST_LENGTH]; @@ -276,14 +276,16 @@ void tr_torrentSetMetadataPiece(tr_torrent* tor, int piece, void const* data, in dbgmsg(tor, "metainfo piece %d was the last one", piece); tr_sha1(sha1, m->metadata, m->metadata_size, NULL); - if ((checksumPassed = memcmp(sha1, tor->info.hash, SHA_DIGEST_LENGTH) == 0)) + bool const checksumPassed = memcmp(sha1, tor->info.hash, SHA_DIGEST_LENGTH) == 0; + if (checksumPassed) { /* checksum passed; now try to parse it as benc */ tr_variant infoDict; int const err = tr_variantFromBenc(&infoDict, m->metadata, m->metadata_size); dbgmsg(tor, "err is %d", err); - if ((metainfoParsed = err == 0)) + metainfoParsed = err == 0; + if (metainfoParsed) { /* yay we have bencoded metainfo... merge it into our .torrent file */ tr_variant newMetainfo; diff --git a/libtransmission/torrent.c b/libtransmission/torrent.c index 334796ee9..d720a309d 100644 --- a/libtransmission/torrent.c +++ b/libtransmission/torrent.c @@ -3511,7 +3511,7 @@ bool tr_torrentFindFile2(tr_torrent const* tor, tr_file_index_t fileNum, char co tr_file const* file; char const* b = NULL; char const* s = NULL; - tr_sys_path_info file_info; + tr_sys_path_info file_info = { 0 }; file = &tor->info.files[fileNum]; diff --git a/libtransmission/tr-dht.c b/libtransmission/tr-dht.c index f8b21a71e..67938e2b9 100644 --- a/libtransmission/tr-dht.c +++ b/libtransmission/tr-dht.c @@ -326,8 +326,8 @@ int tr_dhtInit(tr_session* ss) uint8_t* nodes = NULL; uint8_t* nodes6 = NULL; uint8_t const* raw; - size_t len; - size_t len6; + size_t len = 0; + size_t len6 = 0; struct bootstrap_closure* cl; if (session_ != NULL) /* already initialized */ diff --git a/libtransmission/tr-lpd.c b/libtransmission/tr-lpd.c index 94d79069a..b88182d17 100644 --- a/libtransmission/tr-lpd.c +++ b/libtransmission/tr-lpd.c @@ -57,6 +57,8 @@ typedef uint16_t in_port_t; /* all missing */ #include "utils.h" #include "version.h" +#define SIZEOF_HASH_STRING (sizeof(((struct tr_info*)0)->hashString)) + /** * @brief Local Peer Discovery * @file tr-lpd.c @@ -80,7 +82,6 @@ static tr_socket_t lpd_socket2; /**hashString[0]) == sizeof(char), ""); + struct ip_mreq mcastReq; int const opt_on = 1; int const opt_off = 0; @@ -454,21 +461,6 @@ bool tr_lpdEnabled(tr_session const* ss) return ss != NULL && ss == session; } -/** -* @cond -* @brief Performs some (internal) software consistency checks at compile time. -* @remark Declared inline for the compiler not to allege us of feeding unused -* functions. In any other respect, lpd_consistencyCheck is an orphaned function. -*/ -static inline void lpd_consistencyCheck(void) -{ - /* if the following check fails, the definition of a hash string has changed - * without our knowledge; revise string handling in functions tr_lpdSendAnnounce - * and tr_lpdConsiderAnnounce. However, the code is designed to function as long - * as interfaces to the rest of the lib remain compatible with char* strings. */ - TR_STATIC_ASSERT(sizeof(lpd_torStaticType->info.hashString[0]) == sizeof(char), ""); -} - /** * @endcond */ @@ -497,7 +489,7 @@ bool tr_lpdSendAnnounce(tr_torrent const* t) CRLF CRLF; - char hashString[lengthof(t->info.hashString)]; + char hashString[SIZEOF_HASH_STRING]; char query[lpd_maxDatagramLength + 1] = { 0 }; if (t == NULL) @@ -553,7 +545,7 @@ static int tr_lpdConsiderAnnounce(tr_pex* peer, char const* const msg) enum { maxValueLen = 25, - maxHashLen = lengthof(lpd_torStaticType->info.hashString) + maxHashLen = SIZEOF_HASH_STRING }; struct lpd_protocolVersion ver = { .major = -1, .minor = -1 }; diff --git a/libtransmission/tr-lpd.h b/libtransmission/tr-lpd.h index e93e612a9..89e61abfc 100644 --- a/libtransmission/tr-lpd.h +++ b/libtransmission/tr-lpd.h @@ -31,16 +31,5 @@ void tr_lpdUninit(tr_session*); bool tr_lpdEnabled(tr_session const*); bool tr_lpdSendAnnounce(tr_torrent const*); -/** -* @defgroup Preproc Helper macros -* @{ -* -* @def lengthof -* @brief returns the static length of a C array type -* @note A lower case macro name is tolerable here since this definition of lengthof() -* is intimately related to sizeof semantics. -* Meaningful return values are only guaranteed for true array types. */ -#define lengthof(arr) (sizeof(*(arr)) > 0 ? sizeof(arr) / sizeof(*(arr)) : 0) - /** * @} */ diff --git a/libtransmission/tr-macros.h b/libtransmission/tr-macros.h index b183f0c6d..c174cf258 100644 --- a/libtransmission/tr-macros.h +++ b/libtransmission/tr-macros.h @@ -55,7 +55,8 @@ **** ***/ -#define TR_UNUSED(x) (void)(x) +// http://cnicholson.net/2009/02/stupid-c-tricks-adventures-in-assert/ +#define TR_UNUSED(x) do { ((void)sizeof(x)); } while(0) /*** **** @@ -138,7 +139,7 @@ #elif __has_feature(c_static_assert) || __has_extension(c_static_assert) || TR_GNUC_CHECK_VERSION(4, 6) #define TR_STATIC_ASSERT _Static_assert #else -#define TR_STATIC_ASSERT(x, msg) (void)(x) +#define TR_STATIC_ASSERT(x, msg) do { ((void)sizeof(x)); } while(0) #endif /* Sometimes the system defines MAX/MIN, sometimes not. diff --git a/libtransmission/watchdir.c b/libtransmission/watchdir.c index a923ba536..7014b861d 100644 --- a/libtransmission/watchdir.c +++ b/libtransmission/watchdir.c @@ -55,15 +55,12 @@ struct tr_watchdir static bool is_regular_file(char const* dir, char const* name) { char* const path = tr_buildPath(dir, name, NULL); - tr_sys_path_info path_info; + tr_sys_path_info path_info = { 0 }; tr_error* error = NULL; - bool ret; - if ((ret = tr_sys_path_get_info(path, 0, &path_info, &error))) - { - ret = path_info.type == TR_SYS_PATH_IS_FILE; - } - else + bool const ret = tr_sys_path_get_info(path, 0, &path_info, &error) && (path_info.type == TR_SYS_PATH_IS_FILE); + + if (error != NULL) { if (!TR_ERROR_IS_ENOENT(error->code)) { diff --git a/libtransmission/web.c b/libtransmission/web.c index 7872f64d5..b89185c48 100644 --- a/libtransmission/web.c +++ b/libtransmission/web.c @@ -154,6 +154,9 @@ static int sockoptfunction(void* vtask, curl_socket_t fd, curlsocktype purpose) static CURLcode ssl_context_func(CURL* curl, void* ssl_ctx, void* user_data) { + TR_UNUSED(curl); + TR_UNUSED(user_data); + tr_x509_store_t const cert_store = tr_ssl_get_x509_store(ssl_ctx); if (cert_store == NULL) { @@ -205,11 +208,6 @@ static CURLcode ssl_context_func(CURL* curl, void* ssl_ctx, void* user_data) CertCloseStore(sys_cert_store, 0); } -#else - - TR_UNUSED(curl); - TR_UNUSED(user_data); - #endif return CURLE_OK; @@ -599,9 +597,18 @@ void tr_webClose(tr_session* session, tr_web_close_mode close_mode) } } -void tr_webGetTaskInfo(struct tr_web_task* task, tr_web_task_info info, void* dst) +long tr_webGetTaskResponseCode(struct tr_web_task* task) { - curl_easy_getinfo(task->curl_easy, (CURLINFO)info, dst); + long code = 0; + curl_easy_getinfo(task->curl_easy, CURLINFO_RESPONSE_CODE, &code); + return code; +} + +char const* tr_webGetTaskRealUrl(struct tr_web_task* task) +{ + char* url = NULL; + curl_easy_getinfo(task->curl_easy, CURLINFO_EFFECTIVE_URL, &url); + return url; } /***** diff --git a/libtransmission/web.h b/libtransmission/web.h index 39677fd06..6cf6d9b5a 100644 --- a/libtransmission/web.h +++ b/libtransmission/web.h @@ -8,8 +8,6 @@ #pragma once -#include - #include "tr-macros.h" TR_BEGIN_DECLS @@ -17,14 +15,6 @@ TR_BEGIN_DECLS struct tr_address; struct tr_web_task; -typedef enum -{ - TR_WEB_GET_CODE = CURLINFO_RESPONSE_CODE, - TR_WEB_GET_REDIRECTS = CURLINFO_REDIRECT_COUNT, - TR_WEB_GET_REAL_URL = CURLINFO_EFFECTIVE_URL -} -tr_web_task_info; - typedef enum { TR_WEB_CLOSE_WHEN_IDLE, @@ -49,7 +39,9 @@ struct evbuffer; struct tr_web_task* tr_webRunWebseed(tr_torrent* tor, char const* url, char const* range, tr_web_done_func done_func, void* done_func_user_data, struct evbuffer* buffer); -void tr_webGetTaskInfo(struct tr_web_task* task, tr_web_task_info info, void* dst); +long tr_webGetTaskResponseCode(struct tr_web_task* task); + +char const* tr_webGetTaskRealUrl(struct tr_web_task* task); void tr_http_escape(struct evbuffer* out, char const* str, size_t len, bool escape_slashes); diff --git a/libtransmission/webseed.c b/libtransmission/webseed.c index f33ff6bd8..503635dee 100644 --- a/libtransmission/webseed.c +++ b/libtransmission/webseed.c @@ -242,19 +242,14 @@ static void on_content_changed(struct evbuffer* buf, struct evbuffer_cb_info con if (task->response_code == 0) { - tr_webGetTaskInfo(task->web_task, TR_WEB_GET_CODE, &task->response_code); + task->response_code = tr_webGetTaskResponseCode(task->web_task); if (task->response_code == 206) { - char const* url; struct connection_succeeded_data* data; - - url = NULL; - tr_webGetTaskInfo(task->web_task, TR_WEB_GET_REAL_URL, &url); - data = tr_new(struct connection_succeeded_data, 1); data->webseed = w; - data->real_url = tr_strdup(url); + data->real_url = tr_strdup(tr_webGetTaskRealUrl(task->web_task)); data->piece_index = task->piece_index; data->piece_offset = task->piece_offset + task->blocks_done * task->block_size + len - 1;