juce-framework / JUCE

JUCE is an open-source cross-platform C++ application framework for desktop and mobile applications, including VST, VST3, AU, AUv3, LV2 and AAX audio plug-ins.
https://juce.com
Other
6.54k stars 1.73k forks source link

[bugfix] Fixed to properly convert multibyte text to juce::String. #1393

Closed COx2 closed 3 months ago

COx2 commented 3 months ago

Summary

This fixes an issue where the implementation of the juce::String::fromUTF8 function incorrectly handles multi-byte character strings.

Details

The current implementation may incorrectly treat parts of multi-byte character strings as invalid characters. For example, consider the following code:

// Equivalent to --> auto url = juce::URL(juce::File("C:/path/to/日本語"));
auto url = juce::URL(juce::File(juce::CharPointer_UTF8("C:/path/to/\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e")));

// Return is --> file:///C%3A/path/to/%E6%97%A5%E6%9C%AC%E8%AA%9E
juce::Logger::outputDebugString(url.toString(true));

// Output is --> C:\path\to\日本語9E9E9E
juce::Logger::outputDebugString(url.getLocalFile().getFullPathName());

As you can see, the multi-byte characters in the file path are not properly handled, leading to incorrect encoding and data corruption. With this fix, multi-byte character strings are properly interpreted, and the entire string is correctly decoded.

After fix

With this fix applied, the execution result of the above code will change, and the conversion process from multi-byte character strings (including Japanese characters) to juce::String will work correctly.

// Equivalent to --> auto url = juce::URL(juce::File("C:/path/to/日本語"));
auto url = juce::URL(juce::File(juce::CharPointer_UTF8("C:/path/to/\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e")));

// Return is --> file:///C%3A/path/to/%E6%97%A5%E6%9C%AC%E8%AA%9E
juce::Logger::outputDebugString(url.toString(true));

// Output is --> C:\path\to\日本語
juce::Logger::outputDebugString(url.getLocalFile().getFullPathName());

Related Files

reuk commented 3 months ago

Thanks for reporting this issue, the suggested fix has been merged here: https://github.com/juce-framework/JUCE/commit/131b838c65ca51a6b7239e3de10c0b540a832af1

COx2 commented 3 months ago

Thanks!