When running the "client -i" command using ClientApi::Run, upon rare circumstances (which seem somewhat deterministic yet seemingly random as to why it happens), occasionally, in the process of running this code, an exception is thrown whereby P4 claims that, on the last line of the clientspec, an unexpected character was found and could not be parsed. We verified that this clientspec was indeed correct (in fact, it was built by P4 itself), and we also verified that (in some of the circumstances) the supposedly bad character wasn't even in the string!
The cause for this is found within the function P4String::StringToStrBuf, which converts the managed string to a native string. That function does the conversion by calling Encoding::GetBytes(string) to retrieve a byte array. However, Encoding::GetBytes(string) IS NOT guaranteed to be null-terminated. In fact, no null terminator will appear in that string. This works a large percentage of the time because C# often zeroes out swaths of allocated memory for safety's sake, but in the cases exhibiting this bug, there is at least one character not part of the managed string making it into the native string.
The proper fix for this bug is to change the line:
buffer->Set((const char*)ptr);
to:
buffer->Set((const char*)ptr, b->Length);
in order to force specify the length of the string, rather than rely upon strlen to find a null-character which may or may not be there.