snowcone-ltd / libmatoya

Cross-platform application development.
MIT License
578 stars 50 forks source link

matoya unicode not threadsafe #136

Closed bmcnett closed 5 months ago

bmcnett commented 5 months ago

matoya uses strtok_r because it is known that strtok is not even reentrant, not to mention threadsafe.

unfortunately it calls wcstombs and mbstowcs inside of MTY_MultiToWide and MTY_WideToMulti though these functions are also documented to be non-reentrant.

a non-reentrant function is not safe to call from multiple threads simultaneously, even on a machine that has only one core.

this PR fixes this bug, but shines light on the fact that MTY_MultiToWide and MTY_WideToMulti are not cross-platform.

that is to say, that on Windows they convert utf8<=>utf16, which is useful. on all other platforms, they convert utf8<=>utf32, which has no purpose, and that is why they are never used except on Windows

but, they give off the appearance of being useful cross-platform functions. though practically speaking, every use of MTY_MultiToWide and MTY_WideToMulti could be replaced by the corresponding Windows APIs, with no loss of generality or safety.

since they are never called on non-Windows platforms, because utf8<=>utf32 is not a useful thing to do.