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

msvc_sink throws on invalid utf-8 #3241

Open
captainurist opened this issue Nov 3, 2024 · 6 comments
Open

msvc_sink throws on invalid utf-8 #3241

captainurist opened this issue Nov 3, 2024 · 6 comments

Comments

@captainurist
Copy link

This is what msvc_sink is doing:

    #if defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
        wmemory_buf_t wformatted;
        details::os::utf8_to_wstrbuf(string_view_t(formatted.data(), formatted.size()), wformatted);
        OutputDebugStringW(wformatted.data());
    #else
        OutputDebugStringA(formatted.data());
    #endif

Then in utf8_to_wstrbuf we have:

    int result_size =
        ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.data(), str_size, NULL, 0);

MB_ERR_INVALID_CHARS causes the function to fail on invalid characters, and then utf8_to_wstrbuf throws.

We've run into this when trying to output game data that wasn't utf-8 encoded, had to roll out our own implementation of msvc_sink (OpenEnroth/OpenEnroth#1825).

My feel here is that logging should work at best effort basis and should not throw unless absolutely necessary (as exception can lead to std::terminate). Log calls can be buried somewhere deep in error handling code, and the usual expectation there is that logging calls don't throw.

In this particular case writing out text that's a bit garbled is perfectly OK.

@gabime
Copy link
Owner

gabime commented Nov 3, 2024

Loggers never throw. The sinks might throw but loggers catch everything, so I am not sure what is the problem

@captainurist
Copy link
Author

captainurist commented Nov 3, 2024

Ah OK. Then please disregard my comments on safety.

What do you think about just logging the text that's a bit garbled in this case instead of throwing?

@gabime
Copy link
Owner

gabime commented Nov 3, 2024

I don't think it's a good idea. Might lead to undefined behavior.

@captainurist
Copy link
Author

There is no UB involved, MultiByteToWideChar w/o MB_ERR_INVALID_CHARS will just replace invalid code points with U+FFFD (�), or drop them if we're talking older Windows versions.

The change is safe, the only concern there is about the difference in behavior. The options are:

  1. Throw, potentially causing other loggers in the enclosing dist_sink not to run, and write an error to stderr.
  2. Write out text with �s to msvc debug console.

My opinion is that the latter option is strictly better. What am I missing?

@gabime
Copy link
Owner

gabime commented Nov 3, 2024

I agree. PR is welcome. Could you also add a simple test for this ?

@captainurist
Copy link
Author

Sure, will do a PR with a test. Thanks!

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

No branches or pull requests

2 participants