Skip to content

Commit ba0fdb6

Browse files
committed
Improve error messaging when invalid --object-bits is parsed
It is easier to debug your invocation of a command line tool if the specific invalid argument and the reason why it is invalid is given.
1 parent b0bc4a6 commit ba0fdb6

File tree

4 files changed

+12
-5
lines changed

4 files changed

+12
-5
lines changed

regression/cbmc/object-bits-parsing/non_numeric.desc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
CORE
22
test.c
33
--object-bits foobar
4+
Value of "foobar" given for object-bits is not a valid unsigned integer.
45
object-bits must be positive and less than the pointer width
56
^EXIT=1$
67
^SIGNAL=0$

regression/cbmc/object-bits-parsing/too_large.desc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
CORE
22
test.c
33
--object-bits 65
4+
Value of "65" given for object-bits is out of range.
45
object-bits must be positive and less than the pointer width
56
^EXIT=1$
67
^SIGNAL=0$

regression/cbmc/object-bits-parsing/zero.desc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
CORE
22
test.c
33
--object-bits 0
4+
Value of "0" given for object-bits is out of range.
45
object-bits must be positive and less than the pointer width
56
^EXIT=1$
67
^SIGNAL=0$

src/util/config.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -776,14 +776,18 @@ configt::bv_encodingt parse_object_bits_encoding(
776776
const std::string &argument,
777777
const std::size_t pointer_width)
778778
{
779-
const auto object_bits = string2optional<unsigned int>(argument);
780-
if(!object_bits || *object_bits <= 0 || *object_bits >= pointer_width)
781-
{
779+
const auto throw_for_reason = [&](const std::string &reason) {
782780
throw invalid_command_line_argument_exceptiont(
783-
"object-bits must be positive and less than the pointer width (" +
781+
"Value of \"" + argument + "\" given for object-bits is " + reason +
782+
". object-bits must be positive and less than the pointer width (" +
784783
std::to_string(pointer_width) + ") ",
785784
"--object_bits");
786-
}
785+
};
786+
const auto object_bits = string2optional<unsigned int>(argument);
787+
if(!object_bits)
788+
throw_for_reason("not a valid unsigned integer");
789+
if(*object_bits <= 0 || *object_bits >= pointer_width)
790+
throw_for_reason("out of range");
787791

788792
configt::bv_encodingt bv_encoding;
789793
bv_encoding.object_bits = *object_bits;

0 commit comments

Comments
 (0)