From 18ee68ad1240b84739b603ee03e53b552f904e8c Mon Sep 17 00:00:00 2001 From: Pino Toscano Date: Mon, 6 Mar 2017 13:25:48 +0100 Subject: [PATCH] java: use cleanup handlers for structs (lists) as return values 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 ea35aa237..0bbb55981 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 () =