boostorg / lexical_cast

General literal text conversions, such as an int represented as a string, or vice versa
https://boost.org/libs/lexical_cast
34 stars 58 forks source link

Rule breaking bool cast #29

Closed rezaebrh closed 5 years ago

rezaebrh commented 5 years ago

In case of forcing boolvalue to be something other than 0or 1 , lexical_cast won't throw any error. But the result is out of bool domain. This is where error might happen in case of re converting the converted value back to bool. For more detail check boost::lexical_cast wrong output

In our project, we use a function to convert and convert back some bool value to std::string and vise versa. When we are casting the value back to bool, we face bad_lexical_casterror. since the project is huge, we can't find out the rule breaking part but we hope some changes in boost will be helping us.

apolukhin commented 5 years ago

Compilers optimize assuming that bool has either 0 or 1 value. Any attempt to deal with broken bool is doomed https://godbolt.org/z/NqdmeB (optimized away on clang in all cases, on GCC in majority of cases).

Use UB sanitizer to find the corrupted bools in your project. Nothing could be done in lexical_cast