[SERVER-50917] Unsafe calls to <cctype> functions Created: 14/Sep/20  Updated: 29/Oct/23  Resolved: 13/Oct/20

Status: Closed
Project: Core Server
Component/s: Internal Code
Affects Version/s: None
Fix Version/s: 4.9.0

Type: Improvement Priority: Minor - P4
Reporter: Billy Donahue Assignee: Billy Donahue
Resolution: Fixed Votes: 0
Labels: servicearch-wfbf-day
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to SERVER-71747 Move mongo ctype lint to clang tidy Closed
Backwards Compatibility: Fully Compatible
Sprint: Service arch 2020-10-19
Participants:

 Description   

All the <cctype> functions ( isxdigit, isalnum, etc) take an int parameter, not a char. An inspection of our codebase shows that we are passing char to them all the time, and this is incorrect and potentially UB. A negative char will be sign-extended to int, which will be outside the range [0,255]. These functions use table lookups, so this will become an access outside the lookup table bounds.

https://en.cppreference.com/w/cpp/header/cctype

Typical warning on the cppreference.com docs for such functions.
https://en.cppreference.com/w/cpp/string/byte/isalpha

Like all other functions from <cctype>, the behavior of std::isalpha is undefined if the argument's value is neither representable as unsigned char nor equal to EOF. To use these functions safely with plain chars (or signed chars), the argument should first be converted to unsigned char:

Another subtle problem with these functions is that all except isdigit and isxdigit are locale-dependent. This is rarely anticipated by callers, who are expecting "C" locale ASCII behavior. We might be better off writing wrappers for these 12 functions and lint-warning against #include <cctype> or #include <ctype.h>. The wrappers can take char, and be locale independent.



 Comments   
Comment by Githook User [ 13/Oct/20 ]

Author:

{'name': 'Billy Donahue', 'email': 'billy.donahue@mongodb.com', 'username': 'BillyDonahue'}

Message: SERVER-50917 util/ctype.h to replace <cctype> & <ctype.h> funcs
Branch: master
https://github.com/mongodb/mongo/commit/ff37b70553dbfd0b2aaeabb8a29c4d492507a9d3

Comment by Githook User [ 13/Oct/20 ]

Author:

{'name': 'Billy Donahue', 'email': 'billy.donahue@mongodb.com', 'username': 'BillyDonahue'}

Message: SERVER-50917 use util/ctype.h functions
Branch: master
https://github.com/10gen/mongo-enterprise-modules/commit/4e6ca290e7e261d4b831707735309b9dd0bb14bf

Comment by Billy Donahue [ 13/Oct/20 ]

enterprise CR http://mongodbcr.appspot.com/706010001

Comment by Billy Donahue [ 10/Oct/20 ]

CR https://mongodbcr.appspot.com/678890004/

Generated at Thu Feb 08 05:23:59 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.