summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorRichard Jones <rjones@redhat.com>2010-05-19 12:27:00 +0100
committerRichard Jones <rjones@redhat.com>2010-05-20 10:30:12 +0100
commit30c091f49dafab4ca9c8b6640d19fc0450b15971 (patch)
treec4bba201e9cff32aeb533041bbb8ee3d2b49dd7b /src
parente715451fae0ba738973af98a4e506b6c5564626a (diff)
downloadlibguestfs-30c091f49dafab4ca9c8b6640d19fc0450b15971.tar.gz
libguestfs-30c091f49dafab4ca9c8b6640d19fc0450b15971.tar.xz
libguestfs-30c091f49dafab4ca9c8b6640d19fc0450b15971.zip
generator: Check parameters are not NULL (RHBZ#501893).
This adds additional tests to check that several types of parameter including String are not NULL when passed to the C functions. Previously this would cause a segfault inside libguestfs. With this change, you get an error message / exception. Of the possible pointer parameters, only OptString is now permitted to be NULL. This change does not affect the Perl bindings. This is because Perl XS code was already adding similar checks if you passed undef into a parameter expecting a string.
Diffstat (limited to 'src')
-rwxr-xr-xsrc/generator.ml57
1 files changed, 47 insertions, 10 deletions
diff --git a/src/generator.ml b/src/generator.ml
index f976d815..eb326980 100755
--- a/src/generator.ml
+++ b/src/generator.ml
@@ -5835,6 +5835,50 @@ check_state (guestfs_h *g, const char *caller)
";
+ let error_code_of = function
+ | RErr | RInt _ | RInt64 _ | RBool _ -> "-1"
+ | RConstString _ | RConstOptString _
+ | RString _ | RStringList _
+ | RStruct _ | RStructList _
+ | RHashtable _ | RBufferOut _ -> "NULL"
+ in
+
+ (* Generate code to check String-like parameters are not passed in
+ * as NULL (returning an error if they are).
+ *)
+ let check_null_strings shortname style =
+ let pr_newline = ref false in
+ List.iter (
+ function
+ (* parameters which should not be NULL *)
+ | String n
+ | Device n
+ | Pathname n
+ | Dev_or_Path n
+ | FileIn n
+ | FileOut n
+ | BufferIn n
+ | StringList n
+ | DeviceList n ->
+ pr " if (%s == NULL) {\n" n;
+ pr " error (g, \"%%s: %%s: parameter cannot be NULL\",\n";
+ pr " \"%s\", \"%s\");\n" shortname n;
+ pr " return %s;\n" (error_code_of (fst style));
+ pr " }\n";
+ pr_newline := true
+
+ (* can be NULL *)
+ | OptString _
+
+ (* not applicable *)
+ | Bool _
+ | Int _
+ | Int64 _ -> ()
+ ) (snd style);
+
+ if !pr_newline then pr "\n";
+ in
+
(* Generate code to generate guestfish call traces. *)
let trace_call shortname style =
pr " if (guestfs__get_trace (g)) {\n";
@@ -5892,6 +5936,7 @@ check_state (guestfs_h *g, const char *caller)
generate_prototype ~extern:false ~semicolon:false ~newline:true
~handle:"g" name style;
pr "{\n";
+ check_null_strings shortname style;
trace_call shortname style;
pr " return guestfs__%s " shortname;
generate_c_call_args ~handle:"g" style;
@@ -5904,21 +5949,12 @@ check_state (guestfs_h *g, const char *caller)
List.iter (
fun (shortname, style, _, _, _, _, _) ->
let name = "guestfs_" ^ shortname in
+ let error_code = error_code_of (fst style) in
(* Generate the action stub. *)
generate_prototype ~extern:false ~semicolon:false ~newline:true
~handle:"g" name style;
- let error_code =
- match fst style with
- | RErr | RInt _ | RInt64 _ | RBool _ -> "-1"
- | RConstString _ | RConstOptString _ ->
- failwithf "RConstString|RConstOptString cannot be used by daemon functions"
- | RString _ | RStringList _
- | RStruct _ | RStructList _
- | RHashtable _ | RBufferOut _ ->
- "NULL" in
-
pr "{\n";
(match snd style with
@@ -5943,6 +5979,7 @@ check_state (guestfs_h *g, const char *caller)
pr " int serial;\n";
pr " int r;\n";
pr "\n";
+ check_null_strings shortname style;
trace_call shortname style;
pr " if (check_state (g, \"%s\") == -1) return %s;\n"
shortname error_code;