/pidgin/main: 1b75f8a4129c: Fix some clang static analysis warnings

Tomasz Wasilczyk twasilczyk at pidgin.im
Fri Feb 7 18:57:38 EST 2014


Changeset: 1b75f8a4129cb00b1b560216c6be1eeb869b6ad7
Author:	 Tomasz Wasilczyk <twasilczyk at pidgin.im>
Date:	 2014-02-08 00:57 +0100
Branch:	 default
URL: https://hg.pidgin.im/pidgin/main/rev/1b75f8a4129c

Description:

Fix some clang static analysis warnings

diffstat:

 libpurple/ciphers/pbkdf2cipher.c   |   3 ++-
 libpurple/dnssrv.c                 |  10 ++++++++++
 libpurple/idle.c                   |   3 +--
 libpurple/log.c                    |   8 +++++++-
 libpurple/plugins/log_reader.c     |  16 ++++++++++------
 libpurple/plugins/ssl/ssl-gnutls.c |   7 +++++--
 libpurple/util.c                   |   8 ++++++--
 libpurple/xfer.c                   |   2 ++
 8 files changed, 43 insertions(+), 14 deletions(-)

diffs (209 lines):

diff --git a/libpurple/ciphers/pbkdf2cipher.c b/libpurple/ciphers/pbkdf2cipher.c
--- a/libpurple/ciphers/pbkdf2cipher.c
+++ b/libpurple/ciphers/pbkdf2cipher.c
@@ -208,7 +208,8 @@ purple_pbkdf2_cipher_digest(PurpleCipher
 	halfkey_pad = priv->out_len - (halfkey_count - 1) * halfkey_len;
 
 	salt_ext = g_new(guchar, salt_ext_len);
-	memcpy(salt_ext, priv->salt, priv->salt_len);
+	if (priv->salt_len > 0)
+		memcpy(salt_ext, priv->salt, priv->salt_len);
 
 	for (halfkey_no = 1; halfkey_no <= halfkey_count; halfkey_no++) {
 		memset(halfkey, 0, halfkey_len);
diff --git a/libpurple/dnssrv.c b/libpurple/dnssrv.c
--- a/libpurple/dnssrv.c
+++ b/libpurple/dnssrv.c
@@ -137,6 +137,8 @@ select_random_response(GList *list, Purp
 	size_t runningtotal;
 	int r;
 
+	g_return_val_if_fail(list != NULL, NULL);
+
 	runningtotal = 0;
 	cur = list;
 
@@ -157,6 +159,8 @@ select_random_response(GList *list, Purp
 	r = runningtotal ? g_random_int_range(1, runningtotal + 1) : 0;
 	cur = list;
 	while (r > ((PurpleSrvResponseContainer *)cur->data)->sum) {
+		if (G_UNLIKELY(!cur->next))
+			break;
 		cur = cur->next;
 	}
 
@@ -180,6 +184,8 @@ srv_reorder(GList *list, int num)
 		/* Nothing to sort */
 		return;
 
+	g_return_if_fail(list != NULL);
+
 	/* First build a list of container structs */
 	for (i = 0, cur = list; i < num; i++, cur = cur->next) {
 		container = g_new(PurpleSrvResponseContainer, 1);
@@ -198,6 +204,7 @@ srv_reorder(GList *list, int num)
 		cur->data = container->response;
 		g_free(container);
 		cur = cur->next;
+		g_return_if_fail(cur);
 	}
 }
 
@@ -225,6 +232,9 @@ purple_srv_sort(GList *list)
 	count = 1;
 	while (cur) {
 		PurpleSrvResponse *next_response;
+
+		g_return_val_if_fail(cur->data, list);
+
 		pref = ((PurpleSrvResponse *)cur->data)->pref;
 		next_response = cur->next ? cur->next->data : NULL;
 		if (!next_response || next_response->pref != pref) {
diff --git a/libpurple/idle.c b/libpurple/idle.c
--- a/libpurple/idle.c
+++ b/libpurple/idle.c
@@ -114,7 +114,7 @@ static gint time_until_next_idle_event;
 static void
 check_idleness(void)
 {
-	time_t time_idle;
+	time_t time_idle = 0;
 	gboolean auto_away;
 	const gchar *idle_reporting;
 	gboolean report_idle = TRUE;
@@ -142,7 +142,6 @@ check_idleness(void)
 	else
 	{
 		/* Don't report idle time */
-		time_idle = 0;
 		report_idle = FALSE;
 
 		/* If we're not reporting idle, we can still do auto-away.
diff --git a/libpurple/log.c b/libpurple/log.c
--- a/libpurple/log.c
+++ b/libpurple/log.c
@@ -292,6 +292,10 @@ gint purple_log_get_activity_score(Purpl
 
 				while (logs) {
 					PurpleLog *log = (PurpleLog*)(logs->data);
+					if (!log) {
+						g_warn_if_reached();
+						continue;
+					}
 					/* Activity score counts bytes in the log, exponentially
 					   decayed with a half-life of 14 days. */
 					score_double += purple_log_get_size(log) *
@@ -453,6 +457,8 @@ PurpleLogLogger *purple_log_logger_new(c
 
 void purple_log_logger_free(PurpleLogLogger *logger)
 {
+	if (!logger)
+		return;
 	g_free(logger->name);
 	g_free(logger->id);
 	g_free(logger);
@@ -1579,7 +1585,7 @@ static gsize txt_logger_write(PurpleLog 
 		data = log->logger_data;
 
 		/* if we can't write to the file, give up before we hurt ourselves */
-		if(!data->file)
+		if(!data || !data->file)
 			return 0;
 
 		if (log->type == PURPLE_LOG_SYSTEM)
diff --git a/libpurple/plugins/log_reader.c b/libpurple/plugins/log_reader.c
--- a/libpurple/plugins/log_reader.c
+++ b/libpurple/plugins/log_reader.c
@@ -922,7 +922,7 @@ static char * msn_logger_read (PurpleLog
 		PurpleXmlNode *to;
 		enum name_guesses name_guessed = NAME_GUESS_UNKNOWN;
 		const char *their_name;
-		struct tm *tm;
+		struct tm *tm = NULL;
 		char *timestamp;
 		char *tmp;
 		const char *style;
@@ -1101,12 +1101,16 @@ static char * msn_logger_read (PurpleLog
 			text = g_string_append(text, ";\">");
 		}
 
-		msn_logger_parse_timestamp(message, &tm);
-
-		timestamp = g_strdup_printf("<font size=\"2\">(%02u:%02u:%02u)</font> ",
+		if (msn_logger_parse_timestamp(message, &tm)) {
+			timestamp = g_strdup_printf(
+				"<font size=\"2\">(%02u:%02u:%02u)</font> ",
 				tm->tm_hour, tm->tm_min, tm->tm_sec);
-		text = g_string_append(text, timestamp);
-		g_free(timestamp);
+			text = g_string_append(text, timestamp);
+			g_free(timestamp);
+		} else {
+			text = g_string_append(text,
+				"<font size=\"2\">(00:00:00)</font> ");
+		}
 
 		if (from_name) {
 			text = g_string_append(text, "<b>");
diff --git a/libpurple/plugins/ssl/ssl-gnutls.c b/libpurple/plugins/ssl/ssl-gnutls.c
--- a/libpurple/plugins/ssl/ssl-gnutls.c
+++ b/libpurple/plugins/ssl/ssl-gnutls.c
@@ -412,8 +412,10 @@ ssl_gnutls_connect(PurpleSslConnection *
 	gnutls_set_default_priority(gnutls_data->session);
 #endif
 
-	gnutls_server_name_set(gnutls_data->session, GNUTLS_NAME_DNS, gsc->host,
-		strlen(gsc->host));
+	if (gsc->host) {
+		gnutls_server_name_set(gnutls_data->session, GNUTLS_NAME_DNS,
+			gsc->host, strlen(gsc->host));
+	}
 
 	gnutls_credentials_set(gnutls_data->session, GNUTLS_CRD_CERTIFICATE,
 		xcred);
@@ -1192,6 +1194,7 @@ x509_display_string(PurpleCertificate *c
 
 	/* Pull out the SHA1 checksum */
 	sha_bin = x509_sha1sum(crt);
+	g_return_val_if_fail(sha_bin != NULL, NULL);
 	sha_asc = purple_base16_encode_chunked(sha_bin->data, sha_bin->len);
 
 	/* Get the cert Common Name */
diff --git a/libpurple/util.c b/libpurple/util.c
--- a/libpurple/util.c
+++ b/libpurple/util.c
@@ -3731,7 +3731,7 @@ purple_str_seconds_to_string(guint secs)
 	hrs  = secs / (60 * 60);
 	secs = secs % (60 * 60);
 	mins = secs / 60;
-	secs = secs % 60;
+	/* secs = secs % 60; */
 
 	if (days > 0)
 	{
@@ -3870,8 +3870,12 @@ void purple_got_protocol_handler_uri(con
 		while (*tmp || *pairstart) {
 			if (*tmp == delimiter || !(*tmp)) {
 				/* If there is no explicit value */
-				if (keyend == NULL)
+				if (keyend == NULL) {
 					keyend = tmp;
+				}
+				/* without these brackets, clang won't
+				 * recognize tmp as a non-NULL
+				 */
 
 				if (keyend && keyend != pairstart) {
 					char *p;
diff --git a/libpurple/xfer.c b/libpurple/xfer.c
--- a/libpurple/xfer.c
+++ b/libpurple/xfer.c
@@ -613,6 +613,8 @@ purple_xfer_request_accepted(PurpleXfer 
 		purple_xfer_set_status(xfer, PURPLE_XFER_STATUS_ACCEPTED);
 		priv->ops.init(xfer);
 		return;
+	} else {
+		g_return_if_fail(filename != NULL);
 	}
 
 	buddy = purple_blist_find_buddy(account, priv->who);



More information about the Commits mailing list