[Libguestfs] [PATCH 3/6] java: use cleanup handlers for structs (lists) as return values

Pino Toscano ptoscano at redhat.com
Mon Mar 6 14:42:48 UTC 2017


Filling some of their fields may cause the flow to skip to throw the
"out of memory" exception, and return immediately.  To avoid leaking the
struct, or struct list, from the C implementation, use a cleanup handler
so there is no need to manually clean it up.
---
 generator/java.ml | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/generator/java.ml b/generator/java.ml
index ea35aa2..0bbb559 100644
--- a/generator/java.ml
+++ b/generator/java.ml
@@ -696,13 +696,15 @@ throw_out_of_memory (JNIEnv *env, const char *msg)
            pr "  jobject jr;\n";
            pr "  jclass cl;\n";
            pr "  jfieldID fl;\n";
-           pr "  struct guestfs_%s *r;\n" typ
+           pr "  CLEANUP_FREE_%s struct guestfs_%s *r = NULL;\n"
+             (String.uppercase_ascii typ) typ
        | RStructList (_, typ) ->
            pr "  jobjectArray jr;\n";
            pr "  jclass cl;\n";
            pr "  jfieldID fl;\n";
            pr "  jobject jfl;\n";
-           pr "  struct guestfs_%s_list *r;\n" typ
+           pr "  CLEANUP_FREE_%s_LIST struct guestfs_%s_list *r = NULL;\n"
+             (String.uppercase_ascii typ) typ
        | RBufferOut _ ->
            pr "  jstring jr;\n";
            pr "  char *r;\n";
@@ -1008,7 +1010,6 @@ and generate_java_struct_return typ jtyp cols =
         pr "  fl = (*env)->GetFieldID (env, cl, \"%s\", \"C\");\n" name;
         pr "  (*env)->SetCharField (env, jr, fl, r->%s);\n" name;
   ) cols;
-  pr "  guestfs_free_%s (r);\n" typ;
   pr "  return jr;\n"
 
 and generate_java_struct_list_return typ jtyp cols =
@@ -1069,7 +1070,6 @@ and generate_java_struct_list_return typ jtyp cols =
   pr "    (*env)->SetObjectArrayElement (env, jr, i, jfl);\n";
   pr "  }\n";
   pr "\n";
-  pr "  guestfs_free_%s_list (r);\n" typ;
   pr "  return jr;\n"
 
 and generate_java_makefile_inc () =
-- 
2.9.3




More information about the Libguestfs mailing list