[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[Libguestfs] [PATCH] Don't include control characters in BufferIn trace output



Certain API calls, pwrite-device is an example, take a BufferIn as an argument
which may contain arbitrary binary data. If trace output is on, we will attempt
to print this to the terminal. As well as not working properly due to embedded
NUL characters, this can cause problems on the output terminal, is unlikely to
be useful, and can't be entered into guestfish in any case.

This patch scans BufferIn arguments if trace output is on. If it finds any
control characters it will replace the entire string with '<data>' in the trace
output.
---
 generator/generator_c.ml |   23 +++++++++++++++++++++--
 1 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/generator/generator_c.ml b/generator/generator_c.ml
index 0a9060b..b82f6c6 100644
--- a/generator/generator_c.ml
+++ b/generator/generator_c.ml
@@ -573,7 +573,7 @@ check_state (guestfs_h *g, const char *caller)
 
     let needs_i =
       List.exists (function
-                   | StringList _ | DeviceList _ -> true
+                   | StringList _ | DeviceList _ | BufferIn _ -> true
                    | _ -> false) args in
     if needs_i then (
       pr "    size_t i;\n";
@@ -591,10 +591,29 @@ check_state (guestfs_h *g, const char *caller)
       | Dev_or_Path n
       | FileIn n
       | FileOut n
-      | BufferIn n
       | Key n ->
           (* guestfish doesn't support string escaping, so neither do we *)
           pr "    fprintf (stderr, \" \\\"%%s\\\"\", %s);\n" n
+      | BufferIn n ->           (* BufferIn may contain non-printable data *)
+          pr "    int %s_clean = 1;\n" n;
+          pr "    for (i = 0; i < %s_size; i++) {\n" n;
+          (* We reject all control characters except tab, line feed and
+             character return *)
+          pr "      if (%s[i] < 32) {\n" n;
+          pr "        if (%s[i] != 9 && %s[i] != 10 && %s[i] != 13) {\n" n n n;
+          pr "          %s_clean = 0;\n" n;
+          pr "          break;\n";
+          pr "        }\n";
+          pr "      } else if (%s[i] == 127) {\n" n;
+          pr "        %s_clean = 0;\n" n;
+          pr "        break;\n";
+          pr "      }\n";
+          pr "    }\n";
+          pr "    if (%s_clean) {\n" n;
+          pr "      fprintf (stderr, \" \\\"%%s\\\"\", %s);\n" n;
+          pr "    } else {\n";
+          pr "      fprintf (stderr, \" <data>\");\n";
+          pr "    }\n";
       | OptString n ->			(* string option *)
           pr "    if (%s) fprintf (stderr, \" \\\"%%s\\\"\", %s);\n" n n;
           pr "    else fprintf (stderr, \" null\");\n"

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]