Skip to content

Commit dd4dac9

Browse files
committed
Use parse_object_bits_encoding in parse_object_bits_encoding
The previous use of `unsafe_string2unsigned` handled non numeric input based on the behaviour of 0 being returned by `unsafe_string2unsigned`, which is then caught by the subsequent range check. It was well defined behaviour of `std::strtoul`. However this was only discoverable, based on looking at the implementation of `unsafe_string2unsigned` and reading the documentation of `std::strtoul` or by using a debugger. The explict check in the refactored version makes the handling of this case clear from reading `parse_object_bits_encoding` only, without reference to other code or documentation.
1 parent 5eee678 commit dd4dac9

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

src/util/config.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -776,8 +776,8 @@ configt::bv_encodingt parse_object_bits_encoding(
776776
const std::string &argument,
777777
const std::size_t pointer_width)
778778
{
779-
const unsigned int object_bits = unsafe_string2unsigned(argument);
780-
if(object_bits <= 0 || object_bits >= pointer_width)
779+
const auto object_bits = string2optional<unsigned int>(argument);
780+
if(!object_bits || *object_bits <= 0 || *object_bits >= pointer_width)
781781
{
782782
throw invalid_command_line_argument_exceptiont(
783783
"object-bits must be positive and less than the pointer width (" +
@@ -786,7 +786,7 @@ configt::bv_encodingt parse_object_bits_encoding(
786786
}
787787

788788
configt::bv_encodingt bv_encoding;
789-
bv_encoding.object_bits = object_bits;
789+
bv_encoding.object_bits = *object_bits;
790790
bv_encoding.is_object_bits_default = false;
791791
return bv_encoding;
792792
}

0 commit comments

Comments
 (0)