]> granicus.if.org Git - transmission/commitdiff
Extract assignments from expression
authorMike Gelfand <mikedld@mikedld.com>
Sun, 17 Mar 2019 05:00:15 +0000 (08:00 +0300)
committerMike Gelfand <mikedld@mikedld.com>
Sun, 17 Mar 2019 05:00:15 +0000 (08:00 +0300)
Assignments explicitly enclosed in parentheses are ignored.

* MISRA C:2004, 13.1 - Assignment operators shall not be used in expressions
  that yield a Boolean value
* MISRA C++:2008, 6-2-1 - Assignment operators shall not be used in
  sub-expressions
* MISRA C:2012, 13.4 - The result of an assignment operator should not be used
* MITRE, CWE-481 - Assigning instead of Comparing
* CERT, EXP45-C. - Do not perform assignments in selection statements
* CERT, EXP51-J. - Do not perform assignments in conditional expressions

15 files changed:
gtk/stats.c
gtk/tr-core.c
gtk/util.c
libtransmission/announcer.c
libtransmission/metainfo.c
libtransmission/peer-mgr.c
libtransmission/peer-msgs.c
libtransmission/rpcimpl.c
libtransmission/torrent-magnet.c
libtransmission/utils-test.c
libtransmission/variant-json.c
libtransmission/variant.c
libtransmission/web.c
utils/edit.c
utils/show.c

index c2f74168a0f5baae457581ca3a74aef2b8a791ea..bbc3cb3e9cfa7b0fd321b005742444a444022cd7 100644 (file)
@@ -144,12 +144,15 @@ GtkWidget* gtr_stats_dialog_new(GtkWindow* parent, TrCore* core)
     ui->one_time_lb = GTK_LABEL(l);
     gtk_label_set_single_line_mode(ui->one_time_lb, TRUE);
     hig_workarea_add_row(t, &row, _("Duration:"), l, NULL);
+
     hig_workarea_add_section_divider(t, &row);
+
     hig_workarea_add_section_title(t, &row, _("Total"));
     l = gtk_label_new(_("Started %'d time"));
     ui->all_sessions_lb = GTK_LABEL(l);
     gtk_label_set_single_line_mode(ui->all_sessions_lb, TRUE);
-    hig_workarea_add_label_w(t, row++, l);
+    hig_workarea_add_label_w(t, row, l);
+    ++row;
     l = gtk_label_new(NULL);
     ui->all_up_lb = GTK_LABEL(l);
     gtk_label_set_single_line_mode(ui->all_up_lb, TRUE);
@@ -166,6 +169,7 @@ GtkWidget* gtr_stats_dialog_new(GtkWindow* parent, TrCore* core)
     ui->all_time_lb = GTK_LABEL(l);
     gtk_label_set_single_line_mode(ui->all_time_lb, TRUE);
     hig_workarea_add_row(t, &row, _("Duration:"), l, NULL);
+
     gtr_dialog_set_content(GTK_DIALOG(d), t);
 
     updateStats(ui);
index 6ff817323d3a0b02dfbece41df90ba8ddfef4e13..689d1a8705b625a40cc100340f5307962f19a40d 100644 (file)
@@ -1853,9 +1853,10 @@ static void on_port_test_response(TrCore* core, tr_variant* response, gpointer u
 
 void gtr_core_port_test(TrCore* core)
 {
-    int const tag = nextTag++;
-    tr_variant request;
+    int const tag = nextTag;
+    ++nextTag;
 
+    tr_variant request;
     tr_variantInitDict(&request, 2);
     tr_variantDictAddStr(&request, TR_KEY_method, "port-test");
     tr_variantDictAddInt(&request, TR_KEY_tag, tag);
@@ -1888,9 +1889,10 @@ static void on_blocklist_response(TrCore* core, tr_variant* response, gpointer d
 
 void gtr_core_blocklist_update(TrCore* core)
 {
-    int const tag = nextTag++;
-    tr_variant request;
+    int const tag = nextTag;
+    ++nextTag;
 
+    tr_variant request;
     tr_variantInitDict(&request, 2);
     tr_variantDictAddStr(&request, TR_KEY_method, "blocklist-update");
     tr_variantDictAddInt(&request, TR_KEY_tag, tag);
@@ -1904,7 +1906,9 @@ void gtr_core_blocklist_update(TrCore* core)
 
 void gtr_core_exec(TrCore* core, tr_variant const* top)
 {
-    int const tag = nextTag++;
+    int const tag = nextTag;
+    ++nextTag;
+
     core_send_rpc_request(core, top, tag, NULL, NULL);
 }
 
index 03185c75f192d2b51f8d58efa9ea00cead956321..13c55ec074dfa20bea44b4b5b6da640fe8cf99b2 100644 (file)
@@ -445,7 +445,7 @@ void gtr_combo_box_set_active_enum(GtkComboBox* combo_box, int value)
     /* find the one to select */
     i = 0;
 
-    while ((gtk_tree_model_iter_nth_child(model, &iter, NULL, i++)))
+    while ((gtk_tree_model_iter_nth_child(model, &iter, NULL, i)))
     {
         gtk_tree_model_get(model, &iter, column, &currentValue, -1);
 
@@ -454,6 +454,8 @@ void gtr_combo_box_set_active_enum(GtkComboBox* combo_box, int value)
             gtk_combo_box_set_active_iter(combo_box, &iter);
             return;
         }
+
+        ++i;
     }
 }
 
index e59943afde526f71c15da1116c9b9bc60b9ae4a4..193d716a15b750995640618902ac1a8d7beb8100 100644 (file)
@@ -920,7 +920,8 @@ static tr_announce_event tier_announce_event_pull(tr_tier* tier)
 {
     tr_announce_event const e = tier->announce_events[0];
 
-    tr_removeElementFromArray(tier->announce_events, 0, sizeof(tr_announce_event), tier->announce_event_count--);
+    tr_removeElementFromArray(tier->announce_events, 0, sizeof(tr_announce_event), tier->announce_event_count);
+    --tier->announce_event_count;
 
     return e;
 }
index ed1ca9d23b903399e4a74e9ff60ce2c75b4b7bfc..f96e605d5b035d7456b47435da74f47aad5496fa 100644 (file)
@@ -241,14 +241,17 @@ static char* tr_convertAnnounceToScrape(char const* announce)
      * it will be taken as a sign that that tracker doesn't support
      * the scrape convention. If it does, substitute 'scrape' for
      * 'announce' to find the scrape page. */
-    if ((s = strrchr(announce, '/')) != NULL && strncmp(++s, "announce", 8) == 0)
+    if ((s = strrchr(announce, '/')) != NULL && strncmp(s + 1, "announce", 8) == 0)
     {
         char const* prefix = announce;
-        size_t const prefix_len = s - announce;
-        char const* suffix = s + 8;
+        size_t const prefix_len = s + 1 - announce;
+        char const* suffix = s + 1 + 8;
         size_t const suffix_len = strlen(suffix);
         size_t const alloc_len = prefix_len + 6 + suffix_len + 1;
-        char* walk = scrape = tr_new(char, alloc_len);
+
+        scrape = tr_new(char, alloc_len);
+
+        char* walk = scrape;
         memcpy(walk, prefix, prefix_len);
         walk += prefix_len;
         memcpy(walk, "scrape", 6);
@@ -256,7 +259,8 @@ static char* tr_convertAnnounceToScrape(char const* announce)
         memcpy(walk, suffix, suffix_len);
         walk += suffix_len;
         *walk++ = '\0';
-        TR_ASSERT(walk - scrape == (int)alloc_len);
+
+        TR_ASSERT((size_t)(walk - scrape) == alloc_len);
     }
     /* Some torrents with UDP announce URLs don't have /announce. */
     else if (strncmp(announce, "udp:", 4) == 0)
index 058ba5f09b9b0d9d0ed22285f0c269ac4dd6f914..f22e88ba62709f71ad4f63d3d22bb680d306ea42 100644 (file)
@@ -808,7 +808,8 @@ static void requestListRemove(tr_swarm* s, tr_block_index_t block, tr_peer const
 
         decrementPendingReqCount(b);
 
-        tr_removeElementFromArray(s->requests, pos, sizeof(struct block_request), s->requestCount--);
+        tr_removeElementFromArray(s->requests, pos, sizeof(struct block_request), s->requestCount);
+        --s->requestCount;
 
         // fprintf(stderr, "removing request of block %lu from peer %s... there are now %d block requests left\n", (unsigned long)block,
         //     tr_atomAddrStr(peer->atom), t->requestCount);
@@ -1162,7 +1163,8 @@ static void pieceListRemovePiece(tr_swarm* s, tr_piece_index_t piece)
     {
         int const pos = p - s->pieces;
 
-        tr_removeElementFromArray(s->pieces, pos, sizeof(struct weighted_piece), s->pieceCount--);
+        tr_removeElementFromArray(s->pieces, pos, sizeof(struct weighted_piece), s->pieceCount);
+        --s->pieceCount;
 
         if (s->pieceCount == 0)
         {
@@ -1208,11 +1210,13 @@ static void pieceListResortPiece(tr_swarm* s, struct weighted_piece* p)
         bool exact;
         struct weighted_piece const tmp = *p;
 
-        tr_removeElementFromArray(s->pieces, pos, sizeof(struct weighted_piece), s->pieceCount--);
+        tr_removeElementFromArray(s->pieces, pos, sizeof(struct weighted_piece), s->pieceCount);
+        --s->pieceCount;
 
         pos = tr_lowerBound(&tmp, s->pieces, s->pieceCount, sizeof(struct weighted_piece), comparePieceByWeight, &exact);
 
-        memmove(&s->pieces[pos + 1], &s->pieces[pos], sizeof(struct weighted_piece) * (s->pieceCount++ - pos));
+        memmove(&s->pieces[pos + 1], &s->pieces[pos], sizeof(struct weighted_piece) * (s->pieceCount - pos));
+        ++s->pieceCount;
 
         s->pieces[pos] = tmp;
     }
index 9b41bfe2be9625bb4f299a1bfbd1729243ffead0..b527ba575d10e59b95a4497ed068543d1ddb8f55 100644 (file)
@@ -760,7 +760,8 @@ static bool popNextMetadataRequest(tr_peerMsgs* msgs, int* piece)
 
     *piece = msgs->peerAskedForMetadata[0];
 
-    tr_removeElementFromArray(msgs->peerAskedForMetadata, 0, sizeof(int), msgs->peerAskedForMetadataCount--);
+    tr_removeElementFromArray(msgs->peerAskedForMetadata, 0, sizeof(int), msgs->peerAskedForMetadataCount);
+    --msgs->peerAskedForMetadataCount;
 
     return true;
 }
@@ -774,7 +775,8 @@ static bool popNextRequest(tr_peerMsgs* msgs, struct peer_request* setme)
 
     *setme = msgs->peerAskedFor[0];
 
-    tr_removeElementFromArray(msgs->peerAskedFor, 0, sizeof(struct peer_request), msgs->peer.pendingReqsToClient--);
+    tr_removeElementFromArray(msgs->peerAskedFor, 0, sizeof(struct peer_request), msgs->peer.pendingReqsToClient);
+    --msgs->peer.pendingReqsToClient;
 
     return true;
 }
@@ -1630,7 +1632,8 @@ static int readBtMessage(tr_peerMsgs* msgs, struct evbuffer* inbuf, size_t inlen
                 if (req->index == r.index && req->offset == r.offset && req->length == r.length)
                 {
                     tr_removeElementFromArray(msgs->peerAskedFor, i, sizeof(struct peer_request),
-                        msgs->peer.pendingReqsToClient--);
+                        msgs->peer.pendingReqsToClient);
+                    --msgs->peer.pendingReqsToClient;
                     break;
                 }
             }
index b59a0d024da73b1bee446177ac610d40533c28f5..94f0624f46d157c07f845fdc7bbbff8afb74874e 100644 (file)
@@ -910,7 +910,7 @@ static char const* torrentGet(tr_session* session, tr_variant* args_in, tr_varia
         int const interval = RECENTLY_ACTIVE_SECONDS;
         tr_variant* removed_out = tr_variantDictAddList(args_out, TR_KEY_removed, 0);
 
-        while ((d = tr_variantListChild(&session->removedTorrents, n++)) != NULL)
+        while ((d = tr_variantListChild(&session->removedTorrents, n)) != NULL)
         {
             int64_t date;
             int64_t id;
@@ -921,6 +921,8 @@ static char const* torrentGet(tr_session* session, tr_variant* args_in, tr_varia
             {
                 tr_variantListAddInt(removed_out, id);
             }
+
+            ++n;
         }
     }
 
@@ -1154,7 +1156,7 @@ static char const* addTrackerUrls(tr_torrent* tor, tr_variant* urls)
     /* and add the new ones */
     i = 0;
 
-    while ((val = tr_variantListChild(urls, i++)) != NULL)
+    while ((val = tr_variantListChild(urls, i)) != NULL)
     {
         char const* announce = NULL;
 
@@ -1166,6 +1168,8 @@ static char const* addTrackerUrls(tr_torrent* tor, tr_variant* urls)
             ++n;
             changed = true;
         }
+
+        ++i;
     }
 
     if (!changed)
@@ -1250,7 +1254,7 @@ static char const* removeTrackers(tr_torrent* tor, tr_variant* ids)
     /* remove the ones specified in the urls list */
     i = 0;
 
-    while ((val = tr_variantListChild(ids, i++)) != NULL)
+    while ((val = tr_variantListChild(ids, i)) != NULL)
     {
         int64_t pos;
 
@@ -1258,9 +1262,11 @@ static char const* removeTrackers(tr_torrent* tor, tr_variant* ids)
         {
             tids[t++] = pos;
         }
+
+        ++i;
     }
 
-    /* sort trackerIds and remove from largest to smallest so there is no need to recacluate array indicies */
+    /* sort trackerIds and remove from largest to smallest so there is no need to recalculate array indicies */
     qsort(tids, t, sizeof(int), compareInt);
 
     while (t-- != 0)
@@ -1271,7 +1277,9 @@ static char const* removeTrackers(tr_torrent* tor, tr_variant* ids)
             continue;
         }
 
-        tr_removeElementFromArray(trackers, tids[t], sizeof(tr_tracker_info), n--);
+        tr_removeElementFromArray(trackers, tids[t], sizeof(tr_tracker_info), n);
+        --n;
+
         dup = tids[t];
         changed = true;
     }
index 8edd40489956a3654b5d7d366f609235cdbe299e..aaad93cacb50856669cbb23e703400d19c0bf785 100644 (file)
@@ -259,7 +259,8 @@ void tr_torrentSetMetadataPiece(tr_torrent* tor, int piece, void const* data, in
 
     memcpy(m->metadata + offset, data, len);
 
-    tr_removeElementFromArray(m->piecesNeeded, neededPieceIndex, sizeof(struct metadata_node), m->piecesNeededCount--);
+    tr_removeElementFromArray(m->piecesNeeded, neededPieceIndex, sizeof(struct metadata_node), m->piecesNeededCount);
+    --m->piecesNeededCount;
 
     dbgmsg(tor, "saving metainfo piece %d... %d remain", piece, m->piecesNeededCount);
 
@@ -367,12 +368,10 @@ bool tr_torrentGetNextMetadataRequest(tr_torrent* tor, time_t now, int* setme_pi
 
     if (m != NULL && m->piecesNeededCount > 0 && m->piecesNeeded[0].requestedAt + MIN_REPEAT_INTERVAL_SECS < now)
     {
-        int i;
         int const piece = m->piecesNeeded[0].piece;
+        tr_removeElementFromArray(m->piecesNeeded, 0, sizeof(struct metadata_node), m->piecesNeededCount);
 
-        tr_removeElementFromArray(m->piecesNeeded, 0, sizeof(struct metadata_node), m->piecesNeededCount--);
-
-        i = m->piecesNeededCount++;
+        int i = m->piecesNeededCount - 1;
         m->piecesNeeded[i].piece = piece;
         m->piecesNeeded[i].requestedAt = now;
 
index 7660a25485e35f4c9809f5631d37cd3c2ddabad1..5a17538300d7624a1d8134d98568c0bb55f0a54a 100644 (file)
@@ -314,14 +314,16 @@ static int test_array(void)
     size_t array[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
     size_t n = TR_N_ELEMENTS(array);
 
-    tr_removeElementFromArray(array, 5U, sizeof(size_t), n--);
+    tr_removeElementFromArray(array, 5U, sizeof(size_t), n);
+    --n;
 
     for (size_t i = 0; i < n; ++i)
     {
         check_int(array[i], ==, i < 5 ? i : i + 1);
     }
 
-    tr_removeElementFromArray(array, 0U, sizeof(size_t), n--);
+    tr_removeElementFromArray(array, 0U, sizeof(size_t), n);
+    --n;
 
     for (size_t i = 0; i < n; ++i)
     {
@@ -329,7 +331,7 @@ static int test_array(void)
     }
 
     tr_removeElementFromArray(array, n - 1, sizeof(size_t), n);
-    n--;
+    --n;
 
     for (size_t i = 0; i < n; ++i)
     {
index 6b91786b645cba5a2b9fd45aab96aa003a6c4a17..2b8a9368cbd4772dbe60fc9d9f2eeb3a9e336b69 100644 (file)
@@ -438,7 +438,8 @@ static void jsonChildFunc(struct jsonWalk* data)
         {
         case TR_VARIANT_TYPE_DICT:
             {
-                int const i = pstate->childIndex++;
+                int const i = pstate->childIndex;
+                ++pstate->childIndex;
 
                 if (i % 2 == 0)
                 {
@@ -460,7 +461,8 @@ static void jsonChildFunc(struct jsonWalk* data)
 
         case TR_VARIANT_TYPE_LIST:
             {
-                bool const isLast = ++pstate->childIndex == pstate->childCount;
+                ++pstate->childIndex;
+                bool const isLast = pstate->childIndex == pstate->childCount;
 
                 if (!isLast)
                 {
index 8d2dec332c5eab150aae1b0c55c654da44f015f1..b4616f7c9a99aff1bae1b7e2a92ffd89d197d571 100644 (file)
@@ -250,7 +250,8 @@ tr_variant* tr_variantDictFind(tr_variant* dict, tr_quark const key)
 
 static bool tr_variantDictFindType(tr_variant* dict, tr_quark const key, int type, tr_variant** setme)
 {
-    return tr_variantIsType(*setme = tr_variantDictFind(dict, key), type);
+    *setme = tr_variantDictFind(dict, key);
+    return tr_variantIsType(*setme, type);
 }
 
 size_t tr_variantListSize(tr_variant const* list)
@@ -278,7 +279,8 @@ bool tr_variantListRemove(tr_variant* list, size_t i)
     {
         removed = true;
         tr_variantFree(&list->val.l.vals[i]);
-        tr_removeElementFromArray(list->val.l.vals, i, sizeof(tr_variant), list->val.l.count--);
+        tr_removeElementFromArray(list->val.l.vals, i, sizeof(tr_variant), list->val.l.count);
+        --list->val.l.count;
     }
 
     return removed;
@@ -823,7 +825,9 @@ void tr_variantWalk(tr_variant const* v, struct VariantWalkFuncs const* walkFunc
         }
         else if (tr_variantIsContainer(node->v) && node->childIndex < node->v->val.l.count)
         {
-            int const index = node->childIndex++;
+            int const index = node->childIndex;
+            ++node->childIndex;
+
             v = node->v->val.l.vals + index;
 
             if (tr_variantIsDict(node->v))
@@ -958,7 +962,7 @@ static void tr_variantListCopy(tr_variant* target, tr_variant const* src)
     int i = 0;
     tr_variant const* val;
 
-    while ((val = tr_variantListChild((tr_variant*)src, i++)) != NULL)
+    while ((val = tr_variantListChild((tr_variant*)src, i)) != NULL)
     {
         if (tr_variantIsBool(val))
         {
@@ -997,6 +1001,8 @@ static void tr_variantListCopy(tr_variant* target, tr_variant const* src)
         {
             tr_logAddError("tr_variantListCopy skipping item");
         }
+
+        ++i;
     }
 }
 
index d1ce997b9cf7b3459e9143b80f171e66bb58afc6..bbf9073169bd1057c7f65f3f30a44e05edb87b9c 100644 (file)
@@ -178,8 +178,9 @@ static CURL* createEasy(tr_session* s, struct tr_web* web, struct tr_web_task* t
 {
     bool is_default_value;
     tr_address const* addr;
-    CURL* e = task->curl_easy = curl_easy_init();
+    CURL* e = curl_easy_init();
 
+    task->curl_easy = e;
     task->timeout_secs = getTimeoutFromURL(task);
 
     curl_easy_setopt(e, CURLOPT_AUTOREFERER, 1L);
index dc771f5f2cfc36ab16209fa3862411fa66c1f82f..5b2b2353c32d7a4d70f0ecd84d8a545574cc586a 100644 (file)
@@ -204,23 +204,27 @@ static bool replaceURL(tr_variant* metainfo, char const* in, char const* out)
         tr_variant* tier;
         int tierCount = 0;
 
-        while ((tier = tr_variantListChild(announce_list, tierCount++)) != NULL)
+        while ((tier = tr_variantListChild(announce_list, tierCount)) != NULL)
         {
             tr_variant* node;
             int nodeCount = 0;
 
-            while ((node = tr_variantListChild(tier, nodeCount++)) != NULL)
+            while ((node = tr_variantListChild(tier, nodeCount)) != NULL)
             {
                 if (tr_variantGetStr(node, &str, NULL) && strstr(str, in) != NULL)
                 {
                     char* newstr = replaceSubstr(str, in, out);
-                    printf("\tReplaced in \"announce-list\" tier %d: \"%s\" --> \"%s\"\n", tierCount, str, newstr);
+                    printf("\tReplaced in \"announce-list\" tier %d: \"%s\" --> \"%s\"\n", tierCount + 1, str, newstr);
                     tr_variantFree(node);
                     tr_variantInitStr(node, newstr, TR_BAD_SIZE);
                     tr_free(newstr);
                     changed = true;
                 }
+
+                ++nodeCount;
             }
+
+            ++tierCount;
         }
     }
 
@@ -232,19 +236,23 @@ static bool announce_list_has_url(tr_variant* announce_list, char const* url)
     tr_variant* tier;
     int tierCount = 0;
 
-    while ((tier = tr_variantListChild(announce_list, tierCount++)) != NULL)
+    while ((tier = tr_variantListChild(announce_list, tierCount)) != NULL)
     {
         tr_variant* node;
         char const* str;
         int nodeCount = 0;
 
-        while ((node = tr_variantListChild(tier, nodeCount++)) != NULL)
+        while ((node = tr_variantListChild(tier, nodeCount)) != NULL)
         {
             if (tr_variantGetStr(node, &str, NULL) && strcmp(str, url) == 0)
             {
                 return true;
             }
+
+            ++nodeCount;
         }
+
+        ++tierCount;
     }
 
     return false;
index 5aa2262287ef04d9087bb5f8c7fcd6fbc2d6bef5..d70a7c58bf4cc40b27ad31abc1486a7a16ceb652 100644 (file)
@@ -283,7 +283,7 @@ static void doScrape(tr_info const* inf)
                         tr_quark key;
                         tr_variant* val;
 
-                        while (tr_variantDictChild(files, i++, &key, &val))
+                        while (tr_variantDictChild(files, i, &key, &val))
                         {
                             if (memcmp(inf->hash, tr_quark_get_string(key, NULL), SHA_DIGEST_LENGTH) == 0)
                             {
@@ -302,6 +302,8 @@ static void doScrape(tr_info const* inf)
                                 printf("%d seeders, %d leechers\n", (int)seeders, (int)leechers);
                                 matched = true;
                             }
+
+                            ++i;
                         }
                     }