summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDaniil Baturin <daniil@baturin.org>2022-05-10 15:04:28 +0300
committerDaniil Baturin <daniil@baturin.org>2022-05-19 16:19:34 +0300
commit218be1f46204f21a9e22c1d98a73ded019bcbed1 (patch)
tree9ae9b382f5256b52cee36272d30b182ed88798e1 /src
parentf0d5e978fa8de3f4c0a01989def0f83892104d00 (diff)
downloadvyos-utils-218be1f46204f21a9e22c1d98a73ded019bcbed1.tar.gz
vyos-utils-218be1f46204f21a9e22c1d98a73ded019bcbed1.zip
T4421: only allow numbers that look like an integer part with an optional fractional part
Diffstat (limited to 'src')
-rw-r--r--src/numeric.ml5
1 files changed, 5 insertions, 0 deletions
diff --git a/src/numeric.ml b/src/numeric.ml
index ab59d6c..b296cec 100644
--- a/src/numeric.ml
+++ b/src/numeric.ml
@@ -35,7 +35,12 @@ let check_positive opts n =
if opts.positive && (n <= 0.0) then
failwith "Number should be positive"
+let looks_like_number value =
+ try let _ = Pcre.exec ~pat:"^(\\-?)[0-9]+(\\.[0-9]+)?$" value in true
+ with Not_found -> false
+
let number_of_string opts s =
+ if not (looks_like_number s) then Printf.ksprintf failwith "'%s' is not a valid number" s else
let n = float_of_string_opt s in
match n with
| Some n ->