Nikolay Sivov (@nsivov) commented about dlls/kernelbase/process.c:
- if (ret) WideCharToMultiByte( CP_ACP, 0, dstW, ret, dst, count, NULL, NULL ); - } - else ret = ExpandEnvironmentStringsW( us_src.Buffer, NULL, 0 );
+ /* We always need to call ExpandEnvironmentStringsW, since we need the result to calculate the needed buffer size */ + count_neededW = ExpandEnvironmentStringsW( us_src.Buffer, NULL, 0 ); + if (!(dstW = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, count_neededW * sizeof(WCHAR)))) goto cleanup; + count_neededW = ExpandEnvironmentStringsW( us_src.Buffer, dstW, count_neededW); + + /* Calculate needed buffer */ + count_neededA = WideCharToMultiByte( CP_ACP, 0, dstW, count_neededW, NULL, 0, NULL, NULL ); + if (!count_neededA) goto cleanup; + + /* If provided buffer is enough, do actual conversion */ + if (count >= count_neededA) + count_neededA = WideCharToMultiByte( CP_ACP, 0, dstW, count_neededW, dst, count, NULL, NULL ); Do we have a test that shows that this condition is necessary?
It makes sense to add tests first, then remove todos to make it obvious. -- https://gitlab.winehq.org/wine/wine/-/merge_requests/4045#note_53767