[Libguestfs] [PATCH libnbd 5/9] generator: On entry to API functions, check Enum parameters.

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

In the generated wrapper code this adds checks for all Enum
parameters.  Since only nbd_set_tls uses an Enum parameter, the only
extra code generated by this change is:

  int
  nbd_set_tls (struct nbd_handle *h, int tls) {
    // ...
    switch (tls) {
    case LIBNBD_TLS_DISABLE:
    case LIBNBD_TLS_ALLOW:
    case LIBNBD_TLS_REQUIRE:
      break;
    default:
      set_error (EINVAL, "%s: invalid value for parameter: %d",
                 "tls", tls);
      ret = -1;
      goto out;
    }

This doesn't change the C API, but previously this parameter was not
checked.  So programs using this API which previously happened to work
would now get an error.
---
 generator/generator | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/generator/generator b/generator/generator
index 73b8b79..2b37cea 100755
--- a/generator/generator
+++ b/generator/generator
@@ -3686,6 +3686,23 @@ let generate_lib_api_c ()      (* Check parameters are
valid. *)
     List.iter (
       function
+      | Enum (n, { enum_prefix; enums }) ->
+         let value = match errcode with
+           | Some value -> value
+           | None -> assert false in
+         pr "  switch (%s) {\n" n;
+         List.iter (
+           fun (enum, _) ->
+             pr "  case LIBNBD_%s_%s:\n" enum_prefix enum
+         ) enums;
+         pr "    break;\n";
+         pr "  default:\n";
+         pr "    set_error (EINVAL, \"%%s: invalid value for
parameter: %%d\",\n";
+         pr "               \"%s\", %s);\n" n n;
+         pr "    ret = %s;\n" value;
+         pr "    goto out;\n";
+         pr "  }\n";
+         need_out_label := true
       | String n ->
          let value = match errcode with
            | Some value -> value
--
2.22.0