iconv/wctomb: fix memory corruption related to CURRENT_UTF8 #511
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi,
I found an issue with wasi-libc's
iconv()
implementation: This reproducer shows thaticonv()
performs an out-of-bounds write toicv_out[-1]
:The underlying issue here is an interaction between
iconv()
,wctomb()
, and wasi-libc's inconsistent implementation of theCURRENT_UTF8
macro.iconv
calls out towctomb
here and where it doesn't properly handle the error case:The call to
wctomb
is intended to never fail, butwctomb
's behavior depends on the current locale.iconv
always sets this locale to UTF8:wctomb
and others access the current locale viaCURRENT_UTF8
. The implementation in wasi-libc does not work properly though.CURRENT_UTF8
doesn't reflect updates toCURRENT_LOCALE
:This PR fixes that issue, though it would be nice to refactor
iconv
to not rely on a global "CURRENT_LOCALE" variable..Thanks!
NOTE: While most of the affected code for this issue is from upstream
musl
, the brokenCURRENT_UTF8
macro logic is only present inwasi-libc
. The reproducer above works fine on native musl, glibc and emscripten-based WASM builds.