Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: On Windows narrow returns empty strings for items #172

Closed
1 task done
vm2mv opened this issue Oct 10, 2023 · 3 comments
Closed
1 task done

[Bug]: On Windows narrow returns empty strings for items #172

vm2mv opened this issue Oct 10, 2023 · 3 comments
Assignees
Labels

Comments

@vm2mv
Copy link

vm2mv commented Oct 10, 2023

bit7z version

4.0.x

Compilation options

BIT7Z_AUTO_FORMAT, BIT7Z_AUTO_PREFIX_LONG_PATHS, BIT7Z_BUILD_TESTS, BIT7Z_REGEX_MATCHING, BIT7Z_STATIC_RUNTIME

7-zip version

v23.01

7-zip shared library used

7z.dll / 7z.so

Compilers

MSVC

Compiler versions

MSVC 2022

Architecture

x86_64, x86

Operating system

Windows

Operating system versions

Windows 10 build 1607 (without embeded ICU)

Bug description

On Windows narrow returns empty strings for items (without BIT7Z_USE_NATIVE_STRING).

#ifdef _WIN32
#ifdef BIT7Z_USE_SYSTEM_CODEPAGE
#define CODEPAGE CP_ACP
#define CODEPAGE_FLAGS 0
#else
#define CODEPAGE CP_UTF8
#define CODEPAGE_FLAGS WC_NO_BEST_FIT_CHARS
#endif
#else
...
#if !defined( _WIN32 ) || !defined( BIT7Z_USE_NATIVE_STRING )
auto narrow( const wchar_t* wideString, size_t size ) -> std::string {
    if ( wideString == nullptr || size == 0 ) {
        return "";
    }
#ifdef _WIN32
    const int narrowStringSize = WideCharToMultiByte( CODEPAGE,
                                                      CODEPAGE_FLAGS,
                                                      wideString,
                                                      static_cast< int >( size ),
                                                      nullptr,
                                                      0,
                                                      nullptr,
                                                      nullptr );
    if ( narrowStringSize == 0 ) {
        return "";
    }
...

WideCharToMultiByte returns zero
GLE = 1004 (ERROR_INVALID_FLAGS)

See:
https://learn.microsoft.com/en-us/windows/win32/api/stringapiset/nf-stringapiset-widechartomultibyte

For the code page 65001 (UTF-8) or the code page 54936 (GB18030, Windows Vista and later), dwFlags must be set to either 0 or WC_ERR_INVALID_CHARS. Otherwise, the function fails with ERROR_INVALID_FLAGS.

WC_NO_BEST_FIT_CHARS is not compatible with CP_UTF8

Steps to reproduce

No response

Expected behavior

No response

Relevant compilation output

No response

Code of Conduct

@vm2mv vm2mv added the 🐞 bug label Oct 10, 2023
@vm2mv vm2mv changed the title [Bug]: [Bug]: On Windows narrow returns empty strings for items Oct 10, 2023
@rikyoz
Copy link
Owner

rikyoz commented Oct 10, 2023

Hi!

WC_NO_BEST_FIT_CHARS is not compatible with CP_UTF8

You're right; I must have missed that part in the documentation!

Strangely, this problem has never happened to me in any of my (numerous) tests, and it still does not happen now...

Anyway, I'm working on a fix right now.

Thank you for reporting the issue! 🙏

rikyoz added a commit that referenced this issue Oct 11, 2023
@rikyoz
Copy link
Owner

rikyoz commented Oct 12, 2023

Fixed in v4.0.3.

I could not test Windows 10 build 1607 specifically, but I managed to reproduce the problem, even though only on Windows 7.

In my tests, more recent Windows 10 versions, as well as Windows 11, have no problems when passing both CP_UTF8 and WC_NO_BEST_FIT_CHARS to WideCharToMultiByte. That's probably why the issue went unnoticed before your report!

Maybe Microsoft changed the behavior of WideCharToMultiByte at some point, and they forgot to update the documentation accordingly, or it is simply a bug in the more recent versions of Windows.

Anyway, it's better to stick to the documentation, and I fixed the code accordingly.
I apologize for the delay in releasing the fix; testing took some time.
Thank you again!

@rikyoz rikyoz closed this as completed Oct 12, 2023
@vm2mv
Copy link
Author

vm2mv commented Oct 12, 2023

Thank you for the wonderful library and quick fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants