After some searching I found invalid use of pin_ptr
in zoom_sdk_c_sharp_wrap/zoom_sdk_dotnet_wrap_util.h
.
Function ZOOM_SDK_DOTNET_WRAP::PlatformString2WChar
uses PtrToStringChars
and pins the resulting pointer on the managed string data. So far so good.
But pin_ptr
pins only as long as the variable is in scope. So as soon as PlatformString2WChar
returns, which is done directly after pinning, the pointer is no longer pinned. But the pointer is still used as parameter for SDKAuth
which is called afterwards. And it is not pinned anymore. So any garbage collection can lead to overwriting of parts of the string. And we get error 11. The fact that we only see the problem in SDKAuth is that the token string is strongly verified and it is larger than usual strings like user names or passwords.
from pin_ptr (C++/CLI) | Microsoft Docs
An object is pinned only while a pin_ptr points to it. The object is no longer pinned when its pinning pointer goes out of scope, or is set to nullptr. After the pin_ptr goes out of scope, the object that was pinned can be moved in the heap by the garbage collector. Any native pointers that still point to the object will not be updated, and de-referencing one of them could raise an unrecoverable exception.
Original code in C# Wrapper:
static const wchar_t* PlatformString2WChar(String^ str)
{
if (nullptr == str)
{
return NULL;
}
pin_ptr<const wchar_t> wch = PtrToStringChars(str);
return wch;
}
Bug fixed version:
#include <msclr\marshal.h>
#include <msclr\marshal_cppstd.h>
static std::wstring PlatformString2WCharBugFix(String^ str)
{
if (nullptr == str)
{
return std::wstring();
}
return msclr::interop::marshal_as<std::wstring>(str);
}
But as you see you need to change the signature of the function in order to avoid manual memory deallocation. This way there are no memory leaks and no references of unpinned managed pointers. But you need to adjust all code parts where this function is called:
PlatformString2WChar(someStr)
to something like:
PlatformString2WCharBugFix(someStr).c_str()
Should be enough as long as the string is not used after someStr
goes out of scope.
@roland.oldenburg Please check if this fixes the problem on the machine that is heavily affected by this problem.
@Michael_Condon Please forward to developer.