Make lm_sha_hash thread safe. Fixes LM-64
authorAndreas Köhler <andi5.py@gmx.net>
Thu, 28 Aug 2008 15:13:20 +0200
changeset 496 76a465d81893
parent 494 c9bcbd4beb31
child 498 7b2f03410e76
Make lm_sha_hash thread safe. Fixes LM-64 Compute the checksum in a stack-local variable and the result is printed into a newly allocated string of 41 bytes which is returned afterwards. Patch from Andreas Köhler. committer: Mikael Hallendal <micke@imendio.com>
loudmouth/lm-connection.c
loudmouth/lm-sha.c
loudmouth/lm-sha.h
--- a/loudmouth/lm-connection.c	Thu Aug 28 14:40:25 2008 +0200
+++ b/loudmouth/lm-connection.c	Thu Aug 28 15:13:20 2008 +0200
@@ -582,14 +582,15 @@
 	}
 
 	if (auth_type & AUTH_TYPE_DIGEST) {
-		gchar       *str;
-		const gchar *digest;
+		gchar *str;
+                gchar *digest;
 
 		lm_verbose ("Using digest\n");
 		str = g_strconcat (connection->stream_id, password, NULL);
 		digest = lm_sha_hash (str);
 		g_free (str);
 		lm_message_node_add_child (q_node, "digest", digest);
+                g_free (digest);
 	} 
 	else if (auth_type & AUTH_TYPE_PLAIN) {
 		lm_verbose ("Using plaintext auth\n");
--- a/loudmouth/lm-sha.c	Thu Aug 28 14:40:25 2008 +0200
+++ b/loudmouth/lm-sha.c	Thu Aug 28 15:13:20 2008 +0200
@@ -597,29 +597,35 @@
   }
 }
 
-#ifdef G_OS_WIN32
-#define snprintf _snprintf
-#endif
-
-const gchar *
+/**
+ * lm_sha_hash:
+ * @str: the input string
+ *
+ * Computes the SHA1 checksum of @str and prints it in text mode.
+ *
+ * Return value: A newly allocated string.
+ **/
+gchar *
 lm_sha_hash (const gchar *str)
 {
-        static gchar  ret_val[41];
-        SHA1Context   ctx;
-        guint8         hash[SHA1_HASH_SIZE];
-        gchar        *ch;
-        guint          i;
-        
-        SHA1Init (&ctx);
-        SHA1Update (&ctx, str, strlen (str));
-        SHA1Final (&ctx, hash);
+	gchar        *ret_val;
+	SHA1Context   ctx;
+	guint8        hash[SHA1_HASH_SIZE];
+	gchar        *ch;
+	guint         i;
+
+	ret_val = g_malloc (SHA1_HASH_SIZE*2 + 1);
 
-        ch = ret_val;
+	SHA1Init (&ctx);
+	SHA1Update (&ctx, str, strlen (str));
+	SHA1Final (&ctx, hash);
+
+	ch = ret_val;
 
-        for (i = 0; i < SHA1_HASH_SIZE; ++i) {
-                snprintf (ch, 3, "%02x", hash[i]);
-                ch += 2;
-        }
+	for (i = 0; i < SHA1_HASH_SIZE; ++i) {
+		g_snprintf (ch, 3, "%02x", hash[i]);
+		ch += 2;
+	}
 
-        return (const gchar *) ret_val;
+	return ret_val;
 }
--- a/loudmouth/lm-sha.h	Thu Aug 28 14:40:25 2008 +0200
+++ b/loudmouth/lm-sha.h	Thu Aug 28 15:13:20 2008 +0200
@@ -23,6 +23,6 @@
 
 #include <glib.h>
 
-const gchar *     lm_sha_hash    (const gchar *str);
+gchar *     lm_sha_hash    (const gchar *str);
 
 #endif /* __LM_SHA_H__ */