From f0b7ffcea9b9983555b0555ea5815a193d1e129c Mon Sep 17 00:00:00 2001
From: Edwin Mons <edwin.mons@isode.com>
Date: Wed, 24 Oct 2018 16:53:32 +0200
Subject: Honour JID part length in ICUConverter

ICUConverter allowed jidparts greater than 1023 bytes during
stringprep.

The SConscript of StorageTest did not work when libidn had been
disabled, which prevented test=unit from completing.

Test-Information:

Unit tests pass on Debian 9 in a build with ICU and ASan enabled.

Change-Id: I791a09457c6b852e61ce1281871e0f3ca0ce645c

diff --git a/Swiften/IDN/ICUConverter.cpp b/Swiften/IDN/ICUConverter.cpp
index d6b0827..37ce708 100644
--- a/Swiften/IDN/ICUConverter.cpp
+++ b/Swiften/IDN/ICUConverter.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012-2016 Isode Limited.
+ * Copyright (c) 2012-2018 Isode Limited.
  * All rights reserved.
  * See the COPYING file for more information.
  */
@@ -19,6 +19,8 @@ using namespace Swift;
 using boost::numeric_cast;
 
 namespace {
+    static constexpr auto maxStringPrepLength = 1023;
+
     typedef std::vector<UChar, SafeAllocator<UChar> > ICUString;
 
     const char* toConstCharArray(const std::string& input) {
@@ -93,15 +95,8 @@ namespace {
         ICUString icuInput = convertToICUString(s);
         ICUString icuResult;
         UParseError parseError;
-        icuResult.resize(icuInput.size());
+        icuResult.resize(maxStringPrepLength);
         int32_t icuResultLength = usprep_prepare(icuProfile.get(), vecptr(icuInput), numeric_cast<int32_t>(icuInput.size()), vecptr(icuResult), numeric_cast<int32_t>(icuResult.size()), USPREP_ALLOW_UNASSIGNED, &parseError, &status);
-        icuResult.resize(numeric_cast<size_t>(icuResultLength));
-        if (status == U_BUFFER_OVERFLOW_ERROR) {
-            status = U_ZERO_ERROR;
-            icuResult.resize(numeric_cast<size_t>(icuResultLength));
-            icuResultLength = usprep_prepare(icuProfile.get(), vecptr(icuInput), numeric_cast<int32_t>(icuInput.size()), vecptr(icuResult), numeric_cast<int32_t>(icuResult.size()), USPREP_ALLOW_UNASSIGNED, &parseError, &status);
-            icuResult.resize(numeric_cast<size_t>(icuResultLength));
-        }
         if (U_FAILURE(status)) {
             return std::vector<char, SafeAllocator<char> >();
         }
diff --git a/Swiften/QA/StorageTest/SConscript b/Swiften/QA/StorageTest/SConscript
index b8360ec..46107a0 100644
--- a/Swiften/QA/StorageTest/SConscript
+++ b/Swiften/QA/StorageTest/SConscript
@@ -10,7 +10,8 @@ if env["TEST"] :
     myenv.MergeFlags(myenv["CPPUNIT_FLAGS"])
     myenv.MergeFlags(myenv["GOOGLETEST_FLAGS"])
     myenv.MergeFlags(myenv["BOOST_FLAGS"])
-    myenv.MergeFlags(myenv["LIBIDN_FLAGS"])
+    myenv.MergeFlags(myenv.get("LIBIDN_FLAGS", {}))
+    myenv.MergeFlags(myenv.get("ICU_FLAGS", {}))
     myenv.MergeFlags(myenv.get("EXPAT_FLAGS", {}))
     myenv.MergeFlags(myenv.get("LIBXML_FLAGS", {}))
     myenv.MergeFlags(myenv["PLATFORM_FLAGS"])
-- 
cgit v0.10.2-6-g49f6