[Libguestfs] [libnbd PATCH 1/2] debug: Change trace of RStaticString returns

Eric Blake eblake at redhat.com
Sat Sep 3 18:10:57 UTC 2022


We have very few APIs that return RStaticString.  At present, only two
can fail (get_protocol, connection_state); and when they succeed,
their string is short enough to not need sanitization.  Thus we can
simplify them, which in turn will make it easier for the next patch to
convert the remaining two RStaticString functions that cannot fail
(get_package_name, get_version) to also trace their results.

This is the entire change to the generated api.c:

| --- lib/api.c.bak       2022-09-03 12:38:46.213177887 -0500
| +++ lib/api.c   2022-09-03 12:44:41.571034888 -0500
| @@ -2839,10 +2839,7 @@ nbd_get_protocol (struct nbd_handle *h)
|      if (ret == NULL)
|        debug (h, "leave: error=\"%s\"", nbd_get_error ());
|      else {
| -      char *ret_printable =
| -          nbd_internal_printable_string (ret);
| -      debug (h, "leave: ret=" "%s", ret_printable ? ret_printable : "");
| -      free (ret_printable);
| +      debug (h, "leave: ret=" "%s", ret);
|      }
|    }
|
| @@ -5048,10 +5045,7 @@ nbd_connection_state (struct nbd_handle
|      if (ret == NULL)
|        debug (h, "leave: error=\"%s\"", nbd_get_error ());
|      else {
| -      char *ret_printable =
| -          nbd_internal_printable_string (ret);
| -      debug (h, "leave: ret=" "%s", ret_printable ? ret_printable : "");
| -      free (ret_printable);
| +      debug (h, "leave: ret=" "%s", ret);
|      }
|    }
---
 generator/C.ml | 22 ++++++++--------------
 1 file changed, 8 insertions(+), 14 deletions(-)

diff --git a/generator/C.ml b/generator/C.ml
index 9c67efaa..38f8d946 100644
--- a/generator/C.ml
+++ b/generator/C.ml
@@ -715,39 +715,33 @@ let
   (* Print the trace when we leave a call with debugging enabled. *)
   and print_trace_leave ret =
     pr "  if_debug (h) {\n";
-    let errcode = errcode_of_ret ret in
-    (match errcode with
+    (match errcode_of_ret ret with
      | Some r ->
         pr "    if (ret == %s)\n" r;
-     | None ->
-        pr "    if (0)\n";
+     | None -> assert false
     );
     pr "      debug (h, \"leave: error=\\\"%%s\\\"\", nbd_get_error ());\n";
     pr "    else {\n";
     (match ret with
-     | RStaticString | RString ->
+     | RString ->
         pr "      char *ret_printable =\n";
         pr "          nbd_internal_printable_string (ret);\n"
-     | RBool | RErr | RFd | RInt
-     | RInt64 | RCookie | RSizeT
-     | RUInt | RUIntPtr | REnum _ | RFlags _ -> ()
+     | _ -> ()
     );
     pr "      debug (h, \"leave: ret=\" ";
     (match ret with
      | RBool | RErr | RFd | RInt | REnum _ -> pr "\"%%d\", ret"
      | RInt64 | RCookie -> pr "\"%%\" PRIi64 \"\", ret"
      | RSizeT -> pr "\"%%zd\", ret"
-     | RStaticString | RString ->
-        pr "\"%%s\", ret_printable ? ret_printable : \"\""
+     | RString -> pr "\"%%s\", ret_printable ? ret_printable : \"\""
+     | RStaticString -> pr "\"%%s\", ret"
      | RUInt | RFlags _ -> pr "\"%%u\", ret"
      | RUIntPtr -> pr "\"%%\" PRIuPTR, ret"
     );
     pr ");\n";
     (match ret with
-     | RStaticString | RString -> pr "      free (ret_printable);\n"
-     | RBool | RErr | RFd | RInt
-     | RInt64 | RCookie | RSizeT
-     | RUInt | REnum _ | RFlags _ | RUIntPtr -> ()
+     | RString -> pr "      free (ret_printable);\n"
+     | _ -> ()
     );
     pr "    }\n";
     pr "  }\n"
-- 
2.37.2



More information about the Libguestfs mailing list