Author: ivan
Date: Mon Nov 2 15:10:06 2026
New Revision: 1939958
Log:
Fix a bug in the svn_utf__win32_utf16_to_utf8()/svn_utf__win32_utf16_to_utf8()
functions when they are called with empty SRC and non-empty PREFIX.
This bug didn't cause real problems in the current code. The only usage when
PREFIX is not empty was svn_io__utf8_to_unicode_longpath(), but PREFIX is not
empty for SRC longer than 248 characters.
* subversion/libsvn_subr/utf.c
(svn_utf__win32_utf8_to_utf16, svn_utf__win32_utf16_to_utf8): Add special
handling when SRC length is zero.
* subversion/tests/libsvn_subr/utf-test.c
(test_utf8_to_utf16, test_utf16_to_utf8): New tests.
(test_funcs): Add test_utf8_to_utf16 and test_utf16_to_utf8.
Modified:
subversion/trunk/subversion/libsvn_subr/utf.c
subversion/trunk/subversion/tests/libsvn_subr/utf-test.c
Modified: subversion/trunk/subversion/libsvn_subr/utf.c
==============================================================================
--- subversion/trunk/subversion/libsvn_subr/utf.c Mon Nov 2 14:31:56
2026 (r1939957)
+++ subversion/trunk/subversion/libsvn_subr/utf.c Mon Nov 2 15:10:06
2026 (r1939958)
@@ -1338,19 +1338,31 @@ svn_utf__win32_utf8_to_utf16(const WCHAR
return SVN_NO_ERROR;
}
- wide_count = MultiByteToWideChar(CP_UTF8, 0, src, utf8_count, NULL, 0);
- if (wide_count == 0)
- return svn_error_wrap_apr(apr_get_os_error(),
- _("Conversion to UTF-16 failed"));
-
- wide_str = apr_palloc(result_pool,
- (prefix_len + wide_count + 1) * sizeof(*wide_str));
- if (prefix_len)
- memcpy(wide_str, prefix, prefix_len * sizeof(*wide_str));
- if (0 == MultiByteToWideChar(CP_UTF8, 0, src, utf8_count,
- wide_str + prefix_len, wide_count))
- return svn_error_wrap_apr(apr_get_os_error(),
- _("Conversion to UTF-16 failed"));
+ if (utf8_count == 0)
+ {
+ /* MultiByteToWideChar() cannot convert zero length string. */
+ wide_count = 0;
+ wide_str = apr_palloc(result_pool,
+ (prefix_len + wide_count + 1) * sizeof(*wide_str));
+ if (prefix_len)
+ memcpy(wide_str, prefix, prefix_len * sizeof(*wide_str));
+ }
+ else
+ {
+ wide_count = MultiByteToWideChar(CP_UTF8, 0, src, utf8_count, NULL, 0);
+ if (wide_count == 0)
+ return svn_error_wrap_apr(apr_get_os_error(),
+ _("Conversion to UTF-16 failed"));
+
+ wide_str = apr_palloc(result_pool,
+ (prefix_len + wide_count + 1) * sizeof(*wide_str));
+ if (prefix_len)
+ memcpy(wide_str, prefix, prefix_len * sizeof(*wide_str));
+ if (0 == MultiByteToWideChar(CP_UTF8, 0, src, utf8_count,
+ wide_str + prefix_len, wide_count))
+ return svn_error_wrap_apr(apr_get_os_error(),
+ _("Conversion to UTF-16 failed"));
+ }
wide_str[prefix_len + wide_count] = 0;
*result = wide_str;
@@ -1375,21 +1387,34 @@ svn_utf__win32_utf16_to_utf8(const char
return SVN_NO_ERROR;
}
- utf8_count = WideCharToMultiByte(CP_UTF8, 0, src, wide_count,
- NULL, 0, NULL, FALSE);
- if (utf8_count == 0)
- return svn_error_wrap_apr(apr_get_os_error(),
- _("Conversion from UTF-16 failed"));
+ if (wide_count == 0)
+ {
+ /* WideCharToMultiByte() cannot convert zero length string. */
+ utf8_count = 0;
+ utf8_str = apr_palloc(result_pool,
+ (prefix_len + utf8_count + 1) * sizeof(*utf8_str));
- utf8_str = apr_palloc(result_pool,
- (prefix_len + utf8_count + 1) * sizeof(*utf8_str));
- if (prefix_len)
- memcpy(utf8_str, prefix, prefix_len * sizeof(*utf8_str));
- if (0 == WideCharToMultiByte(CP_UTF8, 0, src, wide_count,
- utf8_str + prefix_len, utf8_count,
- NULL, FALSE))
- return svn_error_wrap_apr(apr_get_os_error(),
- _("Conversion from UTF-16 failed"));
+ if (prefix_len)
+ memcpy(utf8_str, prefix, prefix_len * sizeof(*utf8_str));
+ }
+ else
+ {
+ utf8_count = WideCharToMultiByte(CP_UTF8, 0, src, wide_count, NULL, 0,
+ NULL, FALSE);
+ if (utf8_count == 0)
+ return svn_error_wrap_apr(apr_get_os_error(),
+ _("Conversion from UTF-16 failed"));
+
+ utf8_str = apr_palloc(result_pool,
+ (prefix_len + utf8_count + 1) * sizeof(*utf8_str));
+ if (prefix_len)
+ memcpy(utf8_str, prefix, prefix_len * sizeof(*utf8_str));
+ if (0 == WideCharToMultiByte(CP_UTF8, 0, src, wide_count,
+ utf8_str + prefix_len, utf8_count, NULL,
+ FALSE))
+ return svn_error_wrap_apr(apr_get_os_error(),
+ _("Conversion from UTF-16 failed"));
+ }
utf8_str[prefix_len + utf8_count] = 0;
*result = utf8_str;
Modified: subversion/trunk/subversion/tests/libsvn_subr/utf-test.c
==============================================================================
--- subversion/trunk/subversion/tests/libsvn_subr/utf-test.c Mon Nov 2
14:31:56 2026 (r1939957)
+++ subversion/trunk/subversion/tests/libsvn_subr/utf-test.c Mon Nov 2
15:10:06 2026 (r1939958)
@@ -1339,7 +1339,47 @@ test_utf8_align(apr_pool_t *pool)
return SVN_NO_ERROR;
}
+#ifdef WIN32
+static svn_error_t *
+test_utf8_to_utf16(apr_pool_t *pool)
+{
+ const WCHAR *actual;
+
+ SVN_ERR(svn_utf__win32_utf8_to_utf16(&actual, "", NULL, pool));
+ SVN_TEST_ASSERT(wcscmp(L"", actual) == 0);
+ SVN_ERR(svn_utf__win32_utf8_to_utf16(&actual, "simple", NULL, pool));
+ SVN_TEST_ASSERT(wcscmp(L"simple", actual) == 0);
+
+ SVN_ERR(svn_utf__win32_utf8_to_utf16(&actual, "", L"prefix-", pool));
+ SVN_TEST_ASSERT(wcscmp(L"prefix-", actual) == 0);
+
+ SVN_ERR(svn_utf__win32_utf8_to_utf16(&actual, "simple", L"prefix-", pool));
+ SVN_TEST_ASSERT(wcscmp(L"prefix-simple", actual) == 0);
+
+ return SVN_NO_ERROR;
+}
+
+static svn_error_t *
+test_utf16_to_utf8(apr_pool_t *pool)
+{
+ const char *actual;
+
+ SVN_ERR(svn_utf__win32_utf16_to_utf8(&actual, L"", NULL, pool));
+ SVN_TEST_STRING_ASSERT(actual, "");
+
+ SVN_ERR(svn_utf__win32_utf16_to_utf8(&actual, L"simple", NULL, pool));
+ SVN_TEST_STRING_ASSERT(actual, "simple");
+
+ SVN_ERR(svn_utf__win32_utf16_to_utf8(&actual, L"", "prefix-", pool));
+ SVN_TEST_STRING_ASSERT(actual, "prefix-");
+
+ SVN_ERR(svn_utf__win32_utf16_to_utf8(&actual, L"simple", "prefix-", pool));
+ SVN_TEST_STRING_ASSERT(actual, "prefix-simple");
+
+ return SVN_NO_ERROR;
+}
+#endif
/* The test table. */
@@ -1381,6 +1421,12 @@ static struct svn_test_descriptor_t test
"test grapheme-aware left trim"),
SVN_TEST_PASS2(test_utf8_align,
"test utf8 alignment"),
+#ifdef WIN32
+ SVN_TEST_PASS2(test_utf8_to_utf16,
+ "test svn_utf__win32_utf8_to_utf16"),
+ SVN_TEST_PASS2(test_utf16_to_utf8,
+ "test svn_utf__win32_utf16_to_utf8"),
+#endif
SVN_TEST_NULL
};