Skip to content

WriteFile UTF-8 console path uses ACP conversion and misreports partial writes #13

@739C1AE2

Description

@739C1AE2

In support/thunk/kernel32/u/WriteFile.cc, the console code path converts the input buffer using:

if (!w_buffer.from_a(static_cast<const char *>(lpBuffer),
nNumberOfBytesToWrite)) {

which decodes the input using CP_ACP.

However, when handling a partial write, the code converts the wide string back using:

if (!u_written.from_w(w_buffer.c_str())) {

and u_str::from_w() encodes using UTF-8.

This appears inconsistent: the input buffer is decoded using ACP (from_a()), while the byte count is calculated using UTF-8 (u_str::from_w()).

Additionally, in the partial-write path:

d::u_str u_written;
if (!u_written.from_w(w_buffer.c_str())) {
SetLastError(ERROR_OUTOFMEMORY);
return FALSE;
}
*lpNumberOfBytesWritten = u_written.size();

converts the entire buffer instead of only the first w_written characters reported by WriteConsoleW(), so lpNumberOfBytesWritten may not reflect the actual number of bytes written.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions