1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2025-02-21 21:57:01 +00:00

fix: Coverity warnings (#842)

* Silence coverity CHECKED_RETURN on added.f load

The existing code behaved alright since added.f is optional.
However, by testing for success we can both silence the warning
and prevent a useless initialization of NULL/0 to added_f and
added_f_length.

* Silence coverity CHECKED_RETURN on added6.f load

ipv6 variant of previous commit.

* Silence coverity CHECKED_RETURN writing benc strs

saveStringFunc() gets the target string by calling tr_variantGetStr().
It previously didn't check to see if this function succeeded because
saveStringFunc() isn't reached without the type already being known.
However, checking the return value costs nothing and makes Coverity happy.

* Silence coverity CHECKED_RETURN on ut metadata

Like earlier few Coverity commits in this PR, we're handling optional
values by declaring stack locals set to the default (e.g. -1) and then
trying to read the variant.

Unlike the earlier commits, there is a two-part step to thise read:
checking for the metadata, then checking for the individual fields.
The earlier fixes' aproach -- e.g. initializing to -1 only if the reads
failed -- would involve new nested conditionals. I find the new complexity
to outweigh the benefit of removing the dead store, so in this case I'm
casting the return value to `(void)` to tell Coverity to shush.

* Silence coverity CHECKED_RETURN on scrape

Check the return value of tr_variantGetInt() when showing
seeder and leecher counts in transmission-show.

* Silence CHECKED_RETURN on rpc recently-active

When building a list of removed torrent IDs from variants, confirm that
we can read the IDs from the variants before adding them to the list.
I don't _think_ this would have failed before, but Coverity's right that
it's reasonable to add a safeguard here.

* fix: better fix to serializing benc strings

The approach in 33e2ece7e5 was
a little problematic: GetString() shouldn't fail here; but if
it somehow did, we still want to encode a zero-length benc string here.

* chore: make uncrustify happy
This commit is contained in:
Charles Kerr 2019-02-18 22:38:24 +00:00 committed by GitHub
parent 3ba0b9df15
commit 309d97c578
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 18 deletions

View file

@ -1086,9 +1086,9 @@ static void parseUtMetadata(tr_peerMsgs* msgs, uint32_t msglen, struct evbuffer*
if (tr_variantFromBencFull(&dict, tmp, msglen, NULL, &benc_end) == 0)
{
tr_variantDictFindInt(&dict, TR_KEY_msg_type, &msg_type);
tr_variantDictFindInt(&dict, TR_KEY_piece, &piece);
tr_variantDictFindInt(&dict, TR_KEY_total_size, &total_size);
(void)tr_variantDictFindInt(&dict, TR_KEY_msg_type, &msg_type);
(void)tr_variantDictFindInt(&dict, TR_KEY_piece, &piece);
(void)tr_variantDictFindInt(&dict, TR_KEY_total_size, &total_size);
tr_variantFree(&dict);
}
@ -1159,10 +1159,15 @@ static void parseUtPex(tr_peerMsgs* msgs, uint32_t msglen, struct evbuffer* inbu
{
tr_pex* pex;
size_t n;
size_t added_f_len = 0;
uint8_t const* added_f = NULL;
size_t added_f_len;
uint8_t const* added_f;
if (!tr_variantDictFindRaw(&val, TR_KEY_added_f, &added_f, &added_f_len))
{
added_f_len = 0;
added_f = NULL;
}
tr_variantDictFindRaw(&val, TR_KEY_added_f, &added_f, &added_f_len);
pex = tr_peerMgrCompactToPex(added, added_len, added_f, added_f_len, &n);
n = MIN(n, MAX_PEX_PEER_COUNT);
@ -1186,10 +1191,15 @@ static void parseUtPex(tr_peerMsgs* msgs, uint32_t msglen, struct evbuffer* inbu
{
tr_pex* pex;
size_t n;
size_t added_f_len = 0;
uint8_t const* added_f = NULL;
size_t added_f_len;
uint8_t const* added_f;
if (!tr_variantDictFindRaw(&val, TR_KEY_added6_f, &added_f, &added_f_len))
{
added_f_len = 0;
added_f = NULL;
}
tr_variantDictFindRaw(&val, TR_KEY_added6_f, &added_f, &added_f_len);
pex = tr_peerMgrCompact6ToPex(added, added_len, added_f, added_f_len, &n);
n = MIN(n, MAX_PEX_PEER_COUNT);

View file

@ -912,12 +912,14 @@ static char const* torrentGet(tr_session* session, tr_variant* args_in, tr_varia
while ((d = tr_variantListChild(&session->removedTorrents, n++)) != NULL)
{
int64_t intVal;
int64_t date;
int64_t id;
if (tr_variantDictFindInt(d, TR_KEY_date, &intVal) && intVal >= now - interval)
if (tr_variantDictFindInt(d, TR_KEY_date, &date) &&
date >= now - interval &&
tr_variantDictFindInt(d, TR_KEY_id, &id))
{
tr_variantDictFindInt(d, TR_KEY_id, &intVal);
tr_variantListAddInt(removed_out, intVal);
tr_variantListAddInt(removed_out, id);
}
}
}

View file

@ -351,7 +351,12 @@ static void saveStringFunc(tr_variant const* v, void* evbuf)
{
size_t len;
char const* str;
tr_variantGetStr(v, &str, &len);
if (!tr_variantGetStr(v, &str, &len))
{
len = 0;
str = NULL;
}
evbuffer_add_printf(evbuf, "%zu:", len);
evbuffer_add(evbuf, str, len);
}

View file

@ -287,10 +287,18 @@ static void doScrape(tr_info const* inf)
{
if (memcmp(inf->hash, tr_quark_get_string(key, NULL), SHA_DIGEST_LENGTH) == 0)
{
int64_t seeders = -1;
int64_t leechers = -1;
tr_variantDictFindInt(val, TR_KEY_complete, &seeders);
tr_variantDictFindInt(val, TR_KEY_incomplete, &leechers);
int64_t seeders;
if (!tr_variantDictFindInt(val, TR_KEY_complete, &seeders))
{
seeders = -1;
}
int64_t leechers;
if (!tr_variantDictFindInt(val, TR_KEY_incomplete, &leechers))
{
leechers = -1;
}
printf("%d seeders, %d leechers\n", (int)seeders, (int)leechers);
matched = true;
}