Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
alexzhornyak opened this issue Mar 18, 2021 · 0 comments

Comments

@alexzhornyak
Copy link
Contributor

alexzhornyak commented Mar 18, 2021

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 added a commit to alexzhornyak/uscxml that referenced this issue Mar 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant