uscxml icon indicating copy to clipboard operation
uscxml copied to clipboard

[MSVC 2015] All Ctype functions like 'isspace' should be changed to be safe

Open alexzhornyak opened this issue 4 years ago • 0 comments

Example of the problem

ISWHITESPACE function in String.cpp cause Debug Assertion if non-latin symbol is used

<scxml datamodel="ecmascript" initial="StateShape3" name="ScxmlShape1" version="1.0" xmlns="http://www.w3.org/2005/07/scxml">
	<state id="StateShape3" initial="тест">
		<transition event="Quit" target="FinalShape1"/>
		<state id="StateShape2">
			<transition cond="! (_event.data==&quot;привет&quot;)" event="b" target="тест"/>
		</state>
		<state id="тест">
			<transition cond="_event.data==&quot;привет&quot;" event="b" target="StateShape2"/>
		</state>
	</state>
	<final id="FinalShape1"/>
</scxml>

The next state machine source code cause Debug Assertion Expression: c>=-1 && c<=255 because cyrillic symbols has negative char values

The problem is in macro ISWHITESPACE from String.cpp

#define ISWHITESPACE(char) (isspace(char))

image

Solution

  1. Wrap every ctype function with macro like
#define ISWHITESPACE(symbol) (isspace(static_cast<unsigned char>(symbol)))
  1. Use intermediate safe function like
template<int (&F)(int)>
int safe_ctype(unsigned char c) { return F(c); }

//...
char c = CHAR_MIN;
bool b = safe_ctype<isupper>(c);  // No UB.

alexzhornyak avatar Mar 18 '21 09:03 alexzhornyak