From 91a22d48c334dc89061c725ea027994f021f78bf Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Wed, 25 Jun 2014 17:43:25 +0000 Subject: [PATCH] fix bugs in convert_to/from_native() on windows --- ChangeLog | 1 + src/escape_string.cpp | 2 ++ test/test_string.cpp | 8 ++++++++ 3 files changed, 11 insertions(+) diff --git a/ChangeLog b/ChangeLog index 372e23c7d..6b4834614 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,6 @@ 1.0 release + * fix bugs in convert_to/from_native() on windows * fix support for web servers not supporting keepalive * support storing save_path in resume data * don't use full allocation on network drives (on windows) diff --git a/src/escape_string.cpp b/src/escape_string.cpp index 6a706d466..766afba6b 100644 --- a/src/escape_string.cpp +++ b/src/escape_string.cpp @@ -592,6 +592,7 @@ namespace libtorrent ret.resize(ws.size() * 4 + 1); std::size_t size = WideCharToMultiByte(CP_ACP, 0, ws.c_str(), -1, &ret[0], ret.size(), NULL, NULL); if (size == std::size_t(-1)) return s; + if (size != 0 && ret[size - 1] == '\0') --size; ret.resize(size); return ret; } @@ -602,6 +603,7 @@ namespace libtorrent ws.resize(s.size() + 1); std::size_t size = MultiByteToWideChar(CP_ACP, 0, s.c_str(), -1, &ws[0], ws.size()); if (size == std::size_t(-1)) return s; + if (size != 0 && ret[size - 1] == '\0') --size; ws.resize(size); std::string ret; libtorrent::wchar_utf8(ws, ret); diff --git a/test/test_string.cpp b/test/test_string.cpp index 81231fed2..12df3aae6 100644 --- a/test/test_string.cpp +++ b/test/test_string.cpp @@ -233,6 +233,14 @@ int test_main() ptr = string_tokenize(next, ' ', &next); TEST_EQUAL(ptr, NULL); + TEST_EQUAL(std::string("foobar"), convert_from_native(convert_to_native("foobar"))); + TEST_EQUAL(std::string("foobar") + , convert_from_native(convert_to_native("foo")) + + convert_from_native(convert_to_native("bar"))); + + TEST_EQUAL(convert_to_native("foobar") + , convert_to_native("foo") + convert_to_native("bar")); + return 0; }