# HG changeset patch # User Andreas Köhler # Date 1219929200 -7200 # Node ID 76a465d818931f83721617fabf5dc023cee3d59c # Parent c9bcbd4beb3167654c9ad061247914f555517c67 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 diff -r c9bcbd4beb31 -r 76a465d81893 loudmouth/lm-connection.c --- 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"); diff -r c9bcbd4beb31 -r 76a465d81893 loudmouth/lm-sha.c --- 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; } diff -r c9bcbd4beb31 -r 76a465d81893 loudmouth/lm-sha.h --- 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 -const gchar * lm_sha_hash (const gchar *str); +gchar * lm_sha_hash (const gchar *str); #endif /* __LM_SHA_H__ */