All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Edwin Török" <edwin.torok@cloud.com>
To: xen-devel@lists.xenproject.org
Cc: "Edwin Török" <edwin.torok@cloud.com>,
	"Christian Lindig" <christian.lindig@citrix.com>,
	"David Scott" <dave@recoil.org>,
	"Anthony PERARD" <anthony.perard@citrix.com>
Subject: [PATCH v1] tools/ocaml: fix warnings
Date: Wed, 27 Mar 2024 16:30:21 +0000	[thread overview]
Message-ID: <bc752eab0aafd7981634e4f5be7a53f49e1f3cef.1711556875.git.edwin.torok@cloud.com> (raw)

Do not rely on the string values of the `Failure` exception,
 but use the `_opt` functions instead.

Signed-off-by: Edwin Török <edwin.torok@cloud.com>
---
 tools/ocaml/xenstored/config.ml | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)

diff --git a/tools/ocaml/xenstored/config.ml b/tools/ocaml/xenstored/config.ml
index 95ef745a54..e0df236f73 100644
--- a/tools/ocaml/xenstored/config.ml
+++ b/tools/ocaml/xenstored/config.ml
@@ -83,25 +83,27 @@ let validate cf expected other =
   let err = ref [] in
   let append x = err := x :: !err in
   List.iter (fun (k, v) ->
+      let parse ~err_msg parser v f =
+        match parser v with
+        | None -> append (k, err_msg)
+        | Some r -> f r
+      in
       try
         if not (List.mem_assoc k expected) then
           other k v
         else let ty = List.assoc k expected in
           match ty with
           | Unit f       -> f ()
-          | Bool f       -> f (bool_of_string v)
+          | Bool f       -> parse ~err_msg:"expect bool arg" bool_of_string_opt v f
           | String f     -> f v
-          | Int f        -> f (int_of_string v)
-          | Float f      -> f (float_of_string v)
-          | Set_bool r   -> r := (bool_of_string v)
+          | Int f        -> parse ~err_msg:"expect int arg" int_of_string_opt v f
+          | Float f      -> parse ~err_msg:"expect float arg" float_of_string_opt v f
+          | Set_bool r   -> parse ~err_msg:"expect bool arg" bool_of_string_opt v (fun x -> r := x)
           | Set_string r -> r := v
-          | Set_int r    -> r := int_of_string v
-          | Set_float r  -> r := (float_of_string v)
+          | Set_int r    -> parse ~err_msg:"expect int arg" int_of_string_opt v (fun x -> r:= x)
+          | Set_float r  -> parse ~err_msg:"expect float arg" float_of_string_opt v (fun x -> r := x)
       with
       | Not_found                 -> append (k, "unknown key")
-      | Failure "int_of_string"   -> append (k, "expect int arg")
-      | Failure "bool_of_string"  -> append (k, "expect bool arg")
-      | Failure "float_of_string" -> append (k, "expect float arg")
       | exn                       -> append (k, Printexc.to_string exn)
     ) cf;
   if !err != [] then raise (Error !err)
-- 
2.44.0



             reply	other threads:[~2024-03-27 16:30 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-27 16:30 Edwin Török [this message]
2024-03-27 16:33 ` [PATCH v1] tools/ocaml: fix warnings Andrew Cooper
2024-04-25 10:12 ` Christian Lindig

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bc752eab0aafd7981634e4f5be7a53f49e1f3cef.1711556875.git.edwin.torok@cloud.com \
    --to=edwin.torok@cloud.com \
    --cc=anthony.perard@citrix.com \
    --cc=christian.lindig@citrix.com \
    --cc=dave@recoil.org \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.