Skip to content

Commit

Permalink
args: give an error on invalid number parsing
Browse files Browse the repository at this point in the history
Signed-off-by: Cliff Frey <[email protected]>
  • Loading branch information
Cliff Frey committed Feb 20, 2013
1 parent 92461a5 commit 2b714ac
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
2 changes: 2 additions & 0 deletions elements/test/confparsetest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ ConfParseTest::initialize(ErrorHandler *errh)
CHECK(IntArg().parse("-0", i32) == true && i32 == 0);
CHECK(IntArg().parse("-5", i32) == true && i32 == -5);
CHECK(u32 == 97);
CHECK_ERR(IntArg().parse("aoeu", u32, args) == false && u32 == 97, "invalid number");
CHECK(IntArg().parse("0", u32) == true && u32 == 0);
CHECK(IntArg().parse("-0", u32) == false);
CHECK(IntArg().parse("4294967294", u32) == true && u32 == 4294967294U);
Expand Down Expand Up @@ -174,6 +175,7 @@ ConfParseTest::initialize(ErrorHandler *errh)
CHECK_ERR(BoundedIntArg(0U, 100U).parse("-1", i32, args) == false && i32 == 10, "out of range, bound 0");
CHECK_ERR(BoundedIntArg(0U, 100U).parse("-1", i32, args) == false && i32 == 10, "out of range, bound 0");
CHECK_ERR(BoundedIntArg(0U, ~0U).parse("-1", i32, args) == false && i32 == 10, "out of range, bound 0");
CHECK_ERR(BoundedIntArg(-1, 9).parse("aoeu", i32, args) == false && i32 == 10, "invalid number");

bool b; (void) b;
CHECK(FixedPointArg(1).parse("0.5", i32) == true && i32 == 1);
Expand Down
5 changes: 3 additions & 2 deletions include/click/args.hh
Original file line number Diff line number Diff line change
Expand Up @@ -1051,15 +1051,16 @@ class IntArg : public NumArg { public:

template<typename V>
bool parse_saturating(const String &str, V &result, const ArgContext &args = blank_args) {
(void) args;
constexpr bool is_signed = integer_traits<V>::is_signed;
constexpr int nlimb = int((sizeof(V) + sizeof(limb_type) - 1) / sizeof(limb_type));
limb_type x[nlimb];
if (parse(str.begin(), str.end(), is_signed, int(sizeof(V)), x, nlimb)
!= str.end())
status = status_inval;
if (status && status != status_range)
if (status && status != status_range) {
args.error("invalid number");
return false;
}
typedef typename make_unsigned<V>::type unsigned_v_type;
extract_integer(x, reinterpret_cast<unsigned_v_type &>(result));
return true;
Expand Down

0 comments on commit 2b714ac

Please sign in to comment.