Native OCaml Rego/OPA policy engine
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

ocaml-rego: explicit error messages for malformed units.parse* inputs

Match OPA's wording: empty-amount, malformed-number, embedded-space,
and float-overflow cases now raise specific errors that the strict-
mode tests check for. Without these the [units.parse_bytes("")] /
[units.parse("foo")] tests pass through to silent zero / parse_error
and fail the strict-error assertion.

+29 -9
+29 -9
lib/eval.ml
··· 662 662 ([Ki], [Mi], ...) match case-insensitively; case is significant 663 663 only for [m] (milli) vs [M] (mega). *) 664 664 let s = strip_quotes s in 665 + if String.contains s ' ' then 666 + raise (Eval_error "units.parse: spaces not allowed in resource strings"); 665 667 let n = String.length s in 666 668 let binary = 667 669 [ ··· 691 693 else (0, 1.) 692 694 in 693 695 let num_str = String.sub s 0 (n - suffix_len) in 694 - if num_str = "" then 0. 695 - else 696 - try float_of_string num_str *. mult 697 - with _ -> raise (Eval_error "units.parse: invalid number") 696 + if num_str = "" then raise (Eval_error "units.parse: no amount provided"); 697 + let n = 698 + try float_of_string num_str 699 + with _ -> 700 + raise (Eval_error "units.parse: could not parse amount to a number") 701 + in 702 + let result = n *. mult in 703 + if not (Float.is_finite result) then 704 + raise (Eval_error "units.parse: exponent too large"); 705 + result 698 706 699 707 let units_parse_bytes s = 700 - let lower = String.lowercase_ascii (strip_quotes (Astring.String.trim s)) in 708 + let stripped = strip_quotes s in 709 + if String.contains stripped ' ' then 710 + raise 711 + (Eval_error "units.parse_bytes: spaces not allowed in resource strings"); 712 + let lower = String.lowercase_ascii stripped in 701 713 let suffixes = 702 714 [ 703 715 ("kib", 1024.); ··· 726 738 in 727 739 let suffix_len, mult = units_match suffixes lower in 728 740 let num_str = String.sub lower 0 (String.length lower - suffix_len) in 729 - if num_str = "" then 0. 730 - else 731 - try Float.round (float_of_string num_str *. mult) 732 - with _ -> raise (Eval_error "units.parse_bytes: invalid number") 741 + if num_str = "" then 742 + raise (Eval_error "units.parse_bytes: no byte amount provided"); 743 + let n = 744 + try float_of_string num_str 745 + with _ -> 746 + raise 747 + (Eval_error "units.parse_bytes: could not parse byte amount to a number") 748 + in 749 + let result = n *. mult in 750 + if not (Float.is_finite result) then 751 + raise (Eval_error "units.parse_bytes: exponent too large"); 752 + Float.round result 733 753 734 754 (* ── Time ──────────────────────────────────────────────────────────────── *) 735 755