From 6137a266fd5f8d5953f7fa4baeee339d27f0caee Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be>
Date: Sat, 27 Aug 2011 19:24:48 +0200
Subject: Mark JIDs with empty domains as invalid when StringPrep cache is
 disabled.

We used to only do this correctly in the default compilation setting
where the cache is enabled.

Resolves: #965

diff --git a/Swiften/JID/JID.cpp b/Swiften/JID/JID.cpp
index 925c763..f121120 100644
--- a/Swiften/JID/JID.cpp
+++ b/Swiften/JID/JID.cpp
@@ -92,10 +92,6 @@ void JID::nameprepAndSetComponents(const std::string& node, const std::string& d
 			r.first->second = StringPrep::getPrepared(domain, StringPrep::XMPPNodePrep);
 		}
 		domain_ = r.first->second;
-		if (domain_.empty()) {
-			valid_ = false;
-			return;
-		}
 
 		r = resourcePrepCache.insert(std::make_pair(resource, std::string()));
 		if (r.second) {
@@ -103,6 +99,11 @@ void JID::nameprepAndSetComponents(const std::string& node, const std::string& d
 		}
 		resource_ = r.first->second;
 #endif
+
+		if (domain_.empty()) {
+			valid_ = false;
+			return;
+		}
 	}
 	catch (const std::exception&) {
 		valid_ = false;
diff --git a/Swiften/JID/UnitTest/JIDTest.cpp b/Swiften/JID/UnitTest/JIDTest.cpp
index f406635..e083918 100644
--- a/Swiften/JID/UnitTest/JIDTest.cpp
+++ b/Swiften/JID/UnitTest/JIDTest.cpp
@@ -25,6 +25,7 @@ class JIDTest : public CppUnit::TestFixture
 		CPPUNIT_TEST(testConstructorWithString_EmptyNode);
 		CPPUNIT_TEST(testConstructorWithString_IllegalResource);
 		CPPUNIT_TEST(testConstructorWithStrings);
+		CPPUNIT_TEST(testConstructorWithStrings_EmptyDomain);
 		CPPUNIT_TEST(testIsBare);
 		CPPUNIT_TEST(testIsBare_NotBare);
 		CPPUNIT_TEST(testToBare);
@@ -139,6 +140,12 @@ class JIDTest : public CppUnit::TestFixture
 			CPPUNIT_ASSERT_EQUAL(std::string("baz"), testling.getResource());
 		}
 
+		void testConstructorWithStrings_EmptyDomain() {
+			JID testling("foo", "", "baz");
+
+			CPPUNIT_ASSERT(!testling.isValid());
+		}
+
 		void testIsBare() {
 			CPPUNIT_ASSERT(JID("foo@bar").isBare());
 		}
-- 
cgit v0.10.2-6-g49f6