-
Notifications
You must be signed in to change notification settings - Fork 839
Fix tolower()/toupper() implementation to not lead to undefined behavior #4539
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
Conversation
ret[i] = static_cast<char>(::tolower(ret[i])); | ||
for (char &ch : ret) | ||
ch = | ||
(ch >= 'A' && ch <= 'Z') ? static_cast<char>(ch + ('a' - 'A')) : ch; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will likely fail with EBCDIC but probably that's ok :-)
But more seriously, I wonder why the cast to unsigned char is bad. It allows ctype(3) implementations that use lookup tables and are thus fast, in general. (I get it that this isn't used much and that its performance isn't important.)
I don't object, though, and will test this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that use lookup tables and are thus fast,
I'm quite skeptical that lookup based code would be faster than branch testing. Modern processors are much better at doing branch prediction than fetching bytes from slow memory. Anyway this implementation or the alternate can only deal with ASCII, and that's fine given its intended purpose.
With this PR on top of 9.6.2, proj has one test failure (vs 2), and qgis builds. The remaining test issue is
which looks to be in Adding a cast to unsigned char gets me a clean pass.
|
Interesting comment in the "notes" in https://en.cppreference.com/w/cpp/string/byte/isgraph |
isgraph() use now fixed |
Fixes #4537