summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniil Baturin <daniil@baturin.org>2022-05-10 15:04:28 +0300
committerDaniil Baturin <daniil@baturin.org>2022-05-10 15:04:28 +0300
commit4aa302a05fad95f3d13eb2ff20bbfee88c32e7ff (patch)
treeb73f26786d52f25aee8e2334f45a3538bd8337c3
parent5bbda46493d0c11c8a90e50a68c9788a7488345e (diff)
downloadvyos-utils-4aa302a05fad95f3d13eb2ff20bbfee88c32e7ff.tar.gz
vyos-utils-4aa302a05fad95f3d13eb2ff20bbfee88c32e7ff.zip
T4421: only allow numbers that look like an integer part with an optional fractional part
-rwxr-xr-xdebian/rules2
-rw-r--r--src/numeric.ml5
2 files changed, 6 insertions, 1 deletions
diff --git a/debian/rules b/debian/rules
index 4d3db35..c6e8920 100755
--- a/debian/rules
+++ b/debian/rules
@@ -8,7 +8,7 @@ DIR := debian/tmp
override_dh_auto_build:
eval `opam env`
mkdir -p _build
- ocamlfind ocamlopt -o _build/numeric src/numeric.ml
+ ocamlfind ocamlopt -o _build/numeric -package pcre -linkpkg src/numeric.ml
ocamlfind ocamlopt -o _build/validate-value -package pcre,unix,containers -linkpkg src/validate_value.ml
override_dh_auto_install:
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 ->