[Libguestfs] [PATCH libnbd 7/9] generator: On entry to API functions, check Flags and OFlags parameters.

Richard W.M. Jones
2019-08-10T13:02:46Z

Generate checks that no unknown (at the time of compilation) flags are
passed to Flags or OFlags parameters.
---
 generator/generator | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/generator/generator b/generator/generator
index 96d1148..a6aea26 100755
--- a/generator/generator
+++ b/generator/generator
@@ -3689,6 +3689,19 @@ let generate_lib_api_c ()      );

     (* Check parameters are valid. *)
+    let print_flags_check n { flag_prefix; flags } +      let value = match
errcode with
+        | Some value -> value
+        | None -> assert false in
+      let mask = List.fold_left (lor) 0 (List.map snd flags) in
+      pr "  if ((%s & ~%d) != 0) {\n" n mask;
+      pr "    set_error (EINVAL, \"%%s: invalid value for flag:
%%d\",\n";
+      pr "               \"%s\", %s);\n" n n;
+      pr "    ret = %s;\n" value;
+      pr "    goto out;\n";
+      pr "  }\n";
+      need_out_label := true
+    in
     List.iter (
       function
       | Enum (n, { enum_prefix; enums }) ->
@@ -3708,6 +3721,8 @@ let generate_lib_api_c ()           pr "    goto
out;\n";
          pr "  }\n";
          need_out_label := true
+      | Flags (n, flags) ->
+         print_flags_check n flags
       | String n ->
          let value = match errcode with
            | Some value -> value
@@ -3720,6 +3735,11 @@ let generate_lib_api_c ()           need_out_label :=
true
       | _ -> ()
     ) args;
+    List.iter (
+      function
+      | OFlags (n, flags) ->
+         print_flags_check n flags
+    ) optargs;

     (* Make the call. *)
     pr "  ret = nbd_unlocked_%s " name;
--
2.22.0