Skip to content

Commit 3abe10a

Browse files
committed
Throw if asLong() gets a partially-numeric string
1 parent 2831ca6 commit 3abe10a

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

docopt_value.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,14 @@ namespace docopt {
259259
{
260260
// Attempt to convert a string to a long
261261
if (kind == Kind::String) {
262-
// Doesn't guard against trailing characters,
263-
// but doing so (if desired) would be trivial by checking pos.
262+
const std::string& str = variant.strValue;
264263
std::size_t pos;
265-
return stol(variant.strValue, &pos); // Throws if it can't convert
264+
const long ret = stol(str, &pos); // Throws if it can't convert
265+
if (pos != str.length()) {
266+
// The string ended in non-digits.
267+
throw std::runtime_error( str + " contains non-numeric characters.");
268+
}
269+
return ret;
266270
}
267271
throwIfNotKind(Kind::Long);
268272
return variant.longValue;

0 commit comments

Comments
 (0)