]> Cypherpunks repositories - gostls13.git/commit
os: make readConsole handle its input and output correctly
authorAlex Brainman <alex.brainman@gmail.com>
Wed, 21 Sep 2016 01:19:36 +0000 (11:19 +1000)
committerAlex Brainman <alex.brainman@gmail.com>
Thu, 13 Oct 2016 06:16:53 +0000 (06:16 +0000)
commit1af769da8260b277bd5aa92b5074b3400b1f8d9d
tree389b579d08267ac2e5989ffc9f33942e58a31cb2
parent0a0f4bc181835882717d2f4be75fced4061d4572
os: make readConsole handle its input and output correctly

This CL introduces first test for readConsole. And new test
discovered couple of problems with readConsole.

Console characters consist of multiple bytes each, but byte blocks
returned by syscall.ReadFile have no character boundaries. Some
multi-byte characters might start at the end of one block, and end
at the start of next block. readConsole feeds these blocks to
syscall.MultiByteToWideChar to convert them into utf16, but if some
multi-byte characters have no ending or starting bytes, the
syscall.MultiByteToWideChar might get confused. Current version of
syscall.MultiByteToWideChar call will make
syscall.MultiByteToWideChar ignore all these not complete
multi-byte characters.

The CL solves this issue by changing processing from "randomly
sized block of bytes at a time" to "one multi-byte character at a
time". New readConsole code calls syscall.ReadFile to get 1 byte
first. Then it feeds this byte to syscall.MultiByteToWideChar.
The new syscall.MultiByteToWideChar call uses MB_ERR_INVALID_CHARS
flag to make syscall.MultiByteToWideChar return error if input is
not complete character. If syscall.MultiByteToWideChar returns
correspondent error, we read another byte and pass 2 byte buffer
into syscall.MultiByteToWideChar, and so on until success.

Old readConsole code would also sometimes return no data if user
buffer was smaller then uint16 size, which would confuse callers
that supply 1 byte buffer. This CL fixes that problem too.

Fixes #17097

Change-Id: I88136cdf6a7bf3aed5fbb9ad2c759b6c0304ce30
Reviewed-on: https://go-review.googlesource.com/29493
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
src/internal/syscall/windows/syscall_windows.go
src/os/export_windows_test.go [new file with mode: 0644]
src/os/file_windows.go
src/os/os_windows_test.go