Fix some issues revealed by coverity

This commit is contained in:
Mike Gelfand 2015-05-09 08:37:55 +00:00
parent df98031294
commit 2321bc3fad
14 changed files with 49 additions and 25 deletions

View File

@ -172,7 +172,8 @@ gtr_pref_flag_get (const tr_quark key)
{
bool boolVal;
tr_variantDictFindBool (getPrefs (), key, &boolVal);
if (!tr_variantDictFindBool (getPrefs (), key, &boolVal))
boolVal = false;
return boolVal != 0;
}

View File

@ -970,11 +970,7 @@ new_time_combo (GObject * core, const tr_quark key)
{
char buf[128];
GtkTreeIter iter;
struct tm tm;
tm.tm_hour = i / 60;
tm.tm_min = i % 60;
tm.tm_sec = 0;
strftime (buf, sizeof (buf), "%H:%M", &tm);
g_snprintf (buf, sizeof (buf), "%02d:%02d", i / 60, i % 60);
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter, 0, i, 1, buf, -1);
}

View File

@ -431,20 +431,25 @@ gtr_combo_box_new_enum (const char * text_1, ...)
GtkWidget * w;
GtkCellRenderer * r;
GtkListStore * store;
va_list vl;
const char * text;
va_start (vl, text_1);
store = gtk_list_store_new (2, G_TYPE_INT, G_TYPE_STRING);
text = text_1;
if (text != NULL) do
if (text != NULL)
{
const int val = va_arg (vl, int);
gtk_list_store_insert_with_values (store, NULL, INT_MAX, 0, val, 1, text, -1);
text = va_arg (vl, const char *);
va_list vl;
va_start (vl, text_1);
do
{
const int val = va_arg (vl, int);
gtk_list_store_insert_with_values (store, NULL, INT_MAX, 0, val, 1, text, -1);
text = va_arg (vl, const char *);
}
while (text != NULL);
va_end (vl);
}
while (text != NULL);
w = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
r = gtk_cell_renderer_text_new ();

View File

@ -1145,7 +1145,7 @@ on_announce_done (const tr_announce_response * response,
/* if the tracker included scrape fields in its announce response,
then a separate scrape isn't needed */
if ((scrape_fields >= 3) || (!tracker->scrape && (scrape_fields >= 1)))
if (scrape_fields >= 3 || (scrape_fields >= 1 && tracker->scrape != NULL))
{
tr_logAddTorDbg (tier->tor, "Announce response contained scrape info; "
"rescheduling next scrape to %d seconds from now.",

View File

@ -877,7 +877,7 @@ testForEndgame (const tr_swarm * s)
{
/* we consider ourselves to be in endgame if the number of bytes
we've got requested is >= the number of bytes left to download */
return (s->requestCount * s->tor->blockSize)
return ((uint64_t) s->requestCount * s->tor->blockSize)
>= tr_torrentGetLeftUntilDone (s->tor);
}
@ -1994,6 +1994,9 @@ myHandshakeDoneCB (tr_handshake * handshake,
ensureAtomExists (s, addr, port, 0, -1, TR_PEER_FROM_INCOMING);
atom = getExistingAtom (s, addr);
assert (atom != NULL);
atom->time = tr_time ();
atom->piece_data_time = 0;
atom->lastConnectionAt = tr_time ();

View File

@ -1951,7 +1951,7 @@ fillOutputBuffer (tr_peerMsgs * msgs, time_t now)
bool ok = false;
data = tr_torrentGetMetadataPiece (msgs->torrent, piece, &dataLen);
if ((dataLen > 0) && (data != NULL))
if (data != NULL)
{
tr_variant tmp;
struct evbuffer * payload;

View File

@ -500,7 +500,7 @@ handle_web_client (struct evhttp_request * req,
char * filename = tr_strdup_printf ("%s%s%s",
webClientDir,
TR_PATH_DELIMITER_STR,
subpath && *subpath ? subpath : "index.html");
*subpath != '\0' ? subpath : "index.html");
serve_file (req, server, filename);
tr_free (filename);
}
@ -613,10 +613,17 @@ handle_request (struct evhttp_request * req, void * arg)
{
size_t plen;
char * p = tr_base64_decode_str (auth + 6, &plen);
if (p && plen && ((pass = strchr (p, ':'))))
if (p != NULL)
{
user = p;
*pass++ = '\0';
if (plen > 0 && (pass = strchr (p, ':')) != NULL)
{
user = p;
*pass++ = '\0';
}
else
{
tr_free (p);
}
}
}

View File

@ -192,6 +192,8 @@ tr_torrentGetMetadataPiece (tr_torrent * tor, int piece, int * len)
}
}
assert (ret == NULL || *len > 0);
return ret;
}

View File

@ -2093,11 +2093,15 @@ onSigCHLD (int i UNUSED)
static void
torrentCallScript (const tr_torrent * tor, const char * script)
{
char timeStr[128];
char timeStr[128], * newlinePos;
const time_t now = tr_time ();
tr_strlcpy (timeStr, ctime (&now), sizeof (timeStr));
*strchr (timeStr,'\n') = '\0';
/* ctime () includes '\n', but it's better to be safe */
newlinePos = strchr (timeStr, '\n');
if (newlinePos != NULL)
*newlinePos = '\0';
if (script && *script)
{

View File

@ -284,6 +284,9 @@ tr_eventClose (tr_session * session)
{
assert (tr_isSession (session));
if (session->events == NULL)
return;
session->events->die = true;
tr_logAddDeep (__FILE__, __LINE__, NULL, "closing trevent pipe");
tr_netCloseSocket (session->events->fds[1]);

View File

@ -282,7 +282,7 @@ tr_upnpPulse (tr_upnp * handle,
int err_udp = -1;
errno = 0;
if (!handle->urls.controlURL || !handle->data.first.servicetype)
if (!handle->urls.controlURL)
handle->isMapped = 0;
else
{

View File

@ -112,6 +112,9 @@ class PeerItem: public QTreeWidgetItem
const PeerItem * i = dynamic_cast<const PeerItem*> (&other);
QTreeWidget * tw (treeWidget ());
const int column = tw ? tw->sortColumn () : 0;
assert (i != nullptr);
switch (column)
{
case COL_UP: return peer.rateToPeer < i->peer.rateToPeer;

View File

@ -89,8 +89,7 @@ FreespaceLabel::onSessionExecuted (int64_t tag, const QString& result, tr_varian
// update the label
int64_t bytes = -1;
tr_variantDictFindInt (arguments, TR_KEY_size_bytes, &bytes);
if (bytes >= 0)
if (tr_variantDictFindInt (arguments, TR_KEY_size_bytes, &bytes) && bytes >= 0)
setText (tr("%1 free").arg(Formatter::sizeToString (bytes)));
else
setText (QString ());

View File

@ -132,6 +132,7 @@ TorrentFilter::lessThan (const QModelIndex& left, const QModelIndex& right) cons
val = a->compareSeedRatio (*b);
if (!val)
val = -compare (a->queuePosition(), b->queuePosition());
// fall through
case SortMode::SORT_BY_RATIO:
if (!val)