[Libguestfs] [PATCH v2 4/4] java: do not try to malloc 0 elements in get_all_event_callbacks

Pino Toscano ptoscano at redhat.com
Fri Mar 3 17:13:19 UTC 2017


In case there are no event handlers registered with the handle,
get_all_event_callbacks will count 0 elements, trying to malloc a buffer
of that size.  POSIX says that this can result in either a null pointer,
or an unusable pointer.

Short-circuit get_all_event_callbacks to allocate nothing when there are
no events, making sure to use its results only when there were events.
---
 java/handle.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/java/handle.c b/java/handle.c
index f225361..0993f33 100644
--- a/java/handle.c
+++ b/java/handle.c
@@ -85,7 +85,7 @@ Java_com_redhat_et_libguestfs_GuestFS__1close
   (JNIEnv *env, jobject obj, jlong jg)
 {
   guestfs_h *g = (guestfs_h *) (long) jg;
-  size_t len, i;
+  size_t len;
   struct callback_data **data;
 
   /* There is a nasty, difficult to solve case here where the
@@ -96,11 +96,14 @@ Java_com_redhat_et_libguestfs_GuestFS__1close
 
   guestfs_close (g);
 
-  for (i = 0; i < len; ++i) {
-    (*env)->DeleteGlobalRef (env, data[i]->callback);
-    free (data[i]);
+  if (len > 0) {
+    size_t i;
+    for (i = 0; i < len; ++i) {
+      (*env)->DeleteGlobalRef (env, data[i]->callback);
+      free (data[i]);
+    }
+    free (data);
   }
-  free (data);
 }
 
 /* See EventCallback interface. */
@@ -274,6 +277,10 @@ get_all_event_callbacks (JNIEnv *env, guestfs_h *g, size_t *len_rtn)
     data = guestfs_next_private (g, &key);
   }
 
+  /* No events, so no need to allocate anything. */
+  if (*len_rtn == 0)
+    return NULL;
+
   /* Copy them into the return array. */
   r = malloc (sizeof (struct callback_data *) * (*len_rtn));
   if (r == NULL) {
-- 
2.9.3




More information about the Libguestfs mailing list