Pali Rohár <[email protected]> wrote:

> For the "[PATCH 3/3] crt: improve _wassert() emulation" I would propose
> small improvement (changes attached). It implements decision based on
> _set_error_mode() (as it was already written in the comment)

Can we do this as a follow-up? As I mentioned in previous email, I like the 
idea and wanted to suggest it.

> use alloca
> instead of _alloca (alloca is GNU name and mingw-w64 crt already uses
> the non-underscore variant) and reduce calculation of alloca buffers.

I think we should keep using MS function names internally.

> Currently it first tries to estimate buffer size based on wcsnlen and
> MB_LEN_MAX. Then it calculates the real buffer size correctly via
> WideCharToMultiByte() and third time it is using WideCharToMultiByte()
> for the final conversion. So the first step via wcsnlen and MB_LEN_MAX
> is not needed, the real size can be calculated directly.

I think you misunderstood the purpose of `wcsnlen` trick.

So, basically, we limit `_alloca` allocation size to `BUFSIZ` for `_Message` 
arg and to `FILENAME_MAX` for `_File` arg. The `wcsnlen` trick is used to 
allocate smaller buffer when we can ensure that length of converted string will 
never exceed `BUFSIZ` or `FILENAME_MAX`. It is an attempt to reduce possibility 
of stack overflow. While doing so, I overlooked a stupid off-by-one error...

> Also memset is not needed.

I'm pretty sure it is needed. `WideCharToMultiByte` writes NUL to output buffer 
only if it was converted.

Consider string "テクスト" whose length is 12 code units (+NUL) in UTF-8 or 8 code 
units (+NUL) in code page 932. Now, if you call

    WideCharToMultiByte (CP_UTF8, 0, L"テクスト", -1, buf, 10, NULL, NULL)

It will write "テクス" to `buf` and not NUL-terminate it. Call to `memset` ensures 
that string will always be NUL-terminated.

- Kirill Makurin

_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to