在 2025-10-18 14:21, Yan-Jie Wang 写道:
Isn't this juststr = (char*) ::operator new[](::std::nothrow, mbSize);I think it should be str = (char*) ::operator new[](mbSize, ::std::nothrow);
Errr, I meant `str = new(::std::nothrow) char[mbSize];`.Microsoft doc [1] seems to suggest the returned string be freed with `delete[]`, so it should be allocated with `new`.
[1] https://learn.microsoft.com/en-us/cpp/cpp/convertbstrtostring?view=msvc-170
However, this will require to include an additional header, "new". If this is OK, I can submit a revised patch.
I think I have to check MSVC for some details. I tried this one on x86:
#include <comutil.h>
#include <string.h>
#include <wchar.h>
#include <stdio.h>
#include <new>
int
main(void)
try {
constexpr unsigned int len = 0x30000000;
::BSTR b1 = new wchar_t[len + 1];
::wmemset(b1, L'a', len);
b1[len] = 0;
::printf(" b1 = %p\n", b1);
char* s1 = ::_com_util::ConvertBSTRToString(b1);
::printf(" s1 = %p\n", s1);
::printf("strlen(s1) = %zd (expecting %d)\n", ::strlen(s1), len);
delete[] b1;
delete[] s1;
}
catch(::std::exception& e) {
::printf("exception = %s\n", e.what());
}
This caused `ConvertBSTRToString()` to fail, but it didn't return a null
pointer:
E:\lh_mouse\Desktop>cl /nologo /W4 /O2 /EHs test.cc /link vccomsup.lib
test.cc
E:\lh_mouse\Desktop>test.exe
b1 = 02FED020
exception = bad allocation
Maybe we can just do `str = new char[mbSize];`. The exception will propagate to
the caller.
--
Best regards,
LIU Hao
OpenPGP_signature.asc
Description: OpenPGP digital signature
_______________________________________________ Mingw-w64-public mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/mingw-w64-public
