[gimp] pdb: return NULL for string arrays with zero elements



commit 02d119275362b9f9e3834f3a770427ef41b12239
Author: Michael Natterer <mitch gimp org>
Date:   Mon Sep 14 01:12:57 2015 +0200

    pdb: return NULL for string arrays with zero elements
    
    In e61b1f76 things were changed to return string arrays that can be
    freed with g_strfreev(), but that returned a one-element array
    containing a NULL pointer for zero-length arrays. Fix the code
    to return NULL again for zero-length arrays. See bug #751584.

 libgimp/gimp_pdb.c             |   10 ++++++----
 libgimp/gimpbrushes_pdb.c      |   10 ++++++----
 libgimp/gimpbuffer_pdb.c       |   10 ++++++----
 libgimp/gimpcontext_pdb.c      |   10 ++++++----
 libgimp/gimpdynamics_pdb.c     |   10 ++++++----
 libgimp/gimpfonts_pdb.c        |   10 ++++++----
 libgimp/gimpgradients_pdb.c    |   10 ++++++----
 libgimp/gimpimage_pdb.c        |   10 ++++++----
 libgimp/gimpitem_pdb.c         |   10 ++++++----
 libgimp/gimppalettes_pdb.c     |   10 ++++++----
 libgimp/gimppaths_pdb.c        |   10 ++++++----
 libgimp/gimppatterns_pdb.c     |   10 ++++++----
 libgimp/gimpproceduraldb_pdb.c |   10 ++++++----
 tools/pdbgen/lib.pl            |   31 ++++++++++++++++++++-----------
 14 files changed, 98 insertions(+), 63 deletions(-)
---
diff --git a/libgimp/gimp_pdb.c b/libgimp/gimp_pdb.c
index 533b78b..5553cad 100644
--- a/libgimp/gimp_pdb.c
+++ b/libgimp/gimp_pdb.c
@@ -218,10 +218,12 @@ gimp_get_parasite_list (gint *num_parasites)
   if (return_vals[0].data.d_status == GIMP_PDB_SUCCESS)
     {
       *num_parasites = return_vals[1].data.d_int32;
-      parasites = g_new (gchar *, *num_parasites + 1);
-      for (i = 0; i < *num_parasites; i++)
-        parasites[i] = g_strdup (return_vals[2].data.d_stringarray[i]);
-      parasites[i] = NULL;
+      if (*num_parasites > 0)
+        {
+          parasites = g_new0 (gchar *, *num_parasites + 1);
+          for (i = 0; i < *num_parasites; i++)
+            parasites[i] = g_strdup (return_vals[2].data.d_stringarray[i]);
+        }
     }
 
   gimp_destroy_params (return_vals, nreturn_vals);
diff --git a/libgimp/gimpbrushes_pdb.c b/libgimp/gimpbrushes_pdb.c
index c3714dd..8f4c8e5 100644
--- a/libgimp/gimpbrushes_pdb.c
+++ b/libgimp/gimpbrushes_pdb.c
@@ -97,10 +97,12 @@ gimp_brushes_get_list (const gchar *filter,
   if (return_vals[0].data.d_status == GIMP_PDB_SUCCESS)
     {
       *num_brushes = return_vals[1].data.d_int32;
-      brush_list = g_new (gchar *, *num_brushes + 1);
-      for (i = 0; i < *num_brushes; i++)
-        brush_list[i] = g_strdup (return_vals[2].data.d_stringarray[i]);
-      brush_list[i] = NULL;
+      if (*num_brushes > 0)
+        {
+          brush_list = g_new0 (gchar *, *num_brushes + 1);
+          for (i = 0; i < *num_brushes; i++)
+            brush_list[i] = g_strdup (return_vals[2].data.d_stringarray[i]);
+        }
     }
 
   gimp_destroy_params (return_vals, nreturn_vals);
diff --git a/libgimp/gimpbuffer_pdb.c b/libgimp/gimpbuffer_pdb.c
index e2b0bc3..4a17f6e 100644
--- a/libgimp/gimpbuffer_pdb.c
+++ b/libgimp/gimpbuffer_pdb.c
@@ -68,10 +68,12 @@ gimp_buffers_get_list (const gchar *filter,
   if (return_vals[0].data.d_status == GIMP_PDB_SUCCESS)
     {
       *num_buffers = return_vals[1].data.d_int32;
-      buffer_list = g_new (gchar *, *num_buffers + 1);
-      for (i = 0; i < *num_buffers; i++)
-        buffer_list[i] = g_strdup (return_vals[2].data.d_stringarray[i]);
-      buffer_list[i] = NULL;
+      if (*num_buffers > 0)
+        {
+          buffer_list = g_new0 (gchar *, *num_buffers + 1);
+          for (i = 0; i < *num_buffers; i++)
+            buffer_list[i] = g_strdup (return_vals[2].data.d_stringarray[i]);
+        }
     }
 
   gimp_destroy_params (return_vals, nreturn_vals);
diff --git a/libgimp/gimpcontext_pdb.c b/libgimp/gimpcontext_pdb.c
index e72df3f..4254701 100644
--- a/libgimp/gimpcontext_pdb.c
+++ b/libgimp/gimpcontext_pdb.c
@@ -165,10 +165,12 @@ gimp_context_list_paint_methods (gint    *num_paint_methods,
   if (success)
     {
       *num_paint_methods = return_vals[1].data.d_int32;
-      *paint_methods = g_new (gchar *, *num_paint_methods + 1);
-      for (i = 0; i < *num_paint_methods; i++)
-        (*paint_methods)[i] = g_strdup (return_vals[2].data.d_stringarray[i]);
-      (*paint_methods)[i] = NULL;
+      if (*num_paint_methods > 0)
+        {
+          *paint_methods = g_new0 (gchar *, *num_paint_methods + 1);
+          for (i = 0; i < *num_paint_methods; i++)
+            (*paint_methods)[i] = g_strdup (return_vals[2].data.d_stringarray[i]);
+        }
     }
 
   gimp_destroy_params (return_vals, nreturn_vals);
diff --git a/libgimp/gimpdynamics_pdb.c b/libgimp/gimpdynamics_pdb.c
index 7d6ebbd..ab42990 100644
--- a/libgimp/gimpdynamics_pdb.c
+++ b/libgimp/gimpdynamics_pdb.c
@@ -99,10 +99,12 @@ gimp_dynamics_get_list (const gchar *filter,
   if (return_vals[0].data.d_status == GIMP_PDB_SUCCESS)
     {
       *num_dynamics = return_vals[1].data.d_int32;
-      dynamics_list = g_new (gchar *, *num_dynamics + 1);
-      for (i = 0; i < *num_dynamics; i++)
-        dynamics_list[i] = g_strdup (return_vals[2].data.d_stringarray[i]);
-      dynamics_list[i] = NULL;
+      if (*num_dynamics > 0)
+        {
+          dynamics_list = g_new0 (gchar *, *num_dynamics + 1);
+          for (i = 0; i < *num_dynamics; i++)
+            dynamics_list[i] = g_strdup (return_vals[2].data.d_stringarray[i]);
+        }
     }
 
   gimp_destroy_params (return_vals, nreturn_vals);
diff --git a/libgimp/gimpfonts_pdb.c b/libgimp/gimpfonts_pdb.c
index 398179b..81e7a82 100644
--- a/libgimp/gimpfonts_pdb.c
+++ b/libgimp/gimpfonts_pdb.c
@@ -94,10 +94,12 @@ gimp_fonts_get_list (const gchar *filter,
   if (return_vals[0].data.d_status == GIMP_PDB_SUCCESS)
     {
       *num_fonts = return_vals[1].data.d_int32;
-      font_list = g_new (gchar *, *num_fonts + 1);
-      for (i = 0; i < *num_fonts; i++)
-        font_list[i] = g_strdup (return_vals[2].data.d_stringarray[i]);
-      font_list[i] = NULL;
+      if (*num_fonts > 0)
+        {
+          font_list = g_new0 (gchar *, *num_fonts + 1);
+          for (i = 0; i < *num_fonts; i++)
+            font_list[i] = g_strdup (return_vals[2].data.d_stringarray[i]);
+        }
     }
 
   gimp_destroy_params (return_vals, nreturn_vals);
diff --git a/libgimp/gimpgradients_pdb.c b/libgimp/gimpgradients_pdb.c
index ec8e18e..96a02f0 100644
--- a/libgimp/gimpgradients_pdb.c
+++ b/libgimp/gimpgradients_pdb.c
@@ -97,10 +97,12 @@ gimp_gradients_get_list (const gchar *filter,
   if (return_vals[0].data.d_status == GIMP_PDB_SUCCESS)
     {
       *num_gradients = return_vals[1].data.d_int32;
-      gradient_list = g_new (gchar *, *num_gradients + 1);
-      for (i = 0; i < *num_gradients; i++)
-        gradient_list[i] = g_strdup (return_vals[2].data.d_stringarray[i]);
-      gradient_list[i] = NULL;
+      if (*num_gradients > 0)
+        {
+          gradient_list = g_new0 (gchar *, *num_gradients + 1);
+          for (i = 0; i < *num_gradients; i++)
+            gradient_list[i] = g_strdup (return_vals[2].data.d_stringarray[i]);
+        }
     }
 
   gimp_destroy_params (return_vals, nreturn_vals);
diff --git a/libgimp/gimpimage_pdb.c b/libgimp/gimpimage_pdb.c
index 3bcfb5a..17c3c32 100644
--- a/libgimp/gimpimage_pdb.c
+++ b/libgimp/gimpimage_pdb.c
@@ -2923,10 +2923,12 @@ gimp_image_get_parasite_list (gint32  image_ID,
   if (return_vals[0].data.d_status == GIMP_PDB_SUCCESS)
     {
       *num_parasites = return_vals[1].data.d_int32;
-      parasites = g_new (gchar *, *num_parasites + 1);
-      for (i = 0; i < *num_parasites; i++)
-        parasites[i] = g_strdup (return_vals[2].data.d_stringarray[i]);
-      parasites[i] = NULL;
+      if (*num_parasites > 0)
+        {
+          parasites = g_new0 (gchar *, *num_parasites + 1);
+          for (i = 0; i < *num_parasites; i++)
+            parasites[i] = g_strdup (return_vals[2].data.d_stringarray[i]);
+        }
     }
 
   gimp_destroy_params (return_vals, nreturn_vals);
diff --git a/libgimp/gimpitem_pdb.c b/libgimp/gimpitem_pdb.c
index a08425a..1e4adf9 100644
--- a/libgimp/gimpitem_pdb.c
+++ b/libgimp/gimpitem_pdb.c
@@ -1006,10 +1006,12 @@ gimp_item_get_parasite_list (gint32  item_ID,
   if (return_vals[0].data.d_status == GIMP_PDB_SUCCESS)
     {
       *num_parasites = return_vals[1].data.d_int32;
-      parasites = g_new (gchar *, *num_parasites + 1);
-      for (i = 0; i < *num_parasites; i++)
-        parasites[i] = g_strdup (return_vals[2].data.d_stringarray[i]);
-      parasites[i] = NULL;
+      if (*num_parasites > 0)
+        {
+          parasites = g_new0 (gchar *, *num_parasites + 1);
+          for (i = 0; i < *num_parasites; i++)
+            parasites[i] = g_strdup (return_vals[2].data.d_stringarray[i]);
+        }
     }
 
   gimp_destroy_params (return_vals, nreturn_vals);
diff --git a/libgimp/gimppalettes_pdb.c b/libgimp/gimppalettes_pdb.c
index 393cef9..82b6336 100644
--- a/libgimp/gimppalettes_pdb.c
+++ b/libgimp/gimppalettes_pdb.c
@@ -95,10 +95,12 @@ gimp_palettes_get_list (const gchar *filter,
   if (return_vals[0].data.d_status == GIMP_PDB_SUCCESS)
     {
       *num_palettes = return_vals[1].data.d_int32;
-      palette_list = g_new (gchar *, *num_palettes + 1);
-      for (i = 0; i < *num_palettes; i++)
-        palette_list[i] = g_strdup (return_vals[2].data.d_stringarray[i]);
-      palette_list[i] = NULL;
+      if (*num_palettes > 0)
+        {
+          palette_list = g_new0 (gchar *, *num_palettes + 1);
+          for (i = 0; i < *num_palettes; i++)
+            palette_list[i] = g_strdup (return_vals[2].data.d_stringarray[i]);
+        }
     }
 
   gimp_destroy_params (return_vals, nreturn_vals);
diff --git a/libgimp/gimppaths_pdb.c b/libgimp/gimppaths_pdb.c
index fc44d0e..439558c 100644
--- a/libgimp/gimppaths_pdb.c
+++ b/libgimp/gimppaths_pdb.c
@@ -65,10 +65,12 @@ gimp_path_list (gint32  image_ID,
   if (return_vals[0].data.d_status == GIMP_PDB_SUCCESS)
     {
       *num_paths = return_vals[1].data.d_int32;
-      path_list = g_new (gchar *, *num_paths + 1);
-      for (i = 0; i < *num_paths; i++)
-        path_list[i] = g_strdup (return_vals[2].data.d_stringarray[i]);
-      path_list[i] = NULL;
+      if (*num_paths > 0)
+        {
+          path_list = g_new0 (gchar *, *num_paths + 1);
+          for (i = 0; i < *num_paths; i++)
+            path_list[i] = g_strdup (return_vals[2].data.d_stringarray[i]);
+        }
     }
 
   gimp_destroy_params (return_vals, nreturn_vals);
diff --git a/libgimp/gimppatterns_pdb.c b/libgimp/gimppatterns_pdb.c
index 39f5aa0..daa79c7 100644
--- a/libgimp/gimppatterns_pdb.c
+++ b/libgimp/gimppatterns_pdb.c
@@ -97,10 +97,12 @@ gimp_patterns_get_list (const gchar *filter,
   if (return_vals[0].data.d_status == GIMP_PDB_SUCCESS)
     {
       *num_patterns = return_vals[1].data.d_int32;
-      pattern_list = g_new (gchar *, *num_patterns + 1);
-      for (i = 0; i < *num_patterns; i++)
-        pattern_list[i] = g_strdup (return_vals[2].data.d_stringarray[i]);
-      pattern_list[i] = NULL;
+      if (*num_patterns > 0)
+        {
+          pattern_list = g_new0 (gchar *, *num_patterns + 1);
+          for (i = 0; i < *num_patterns; i++)
+            pattern_list[i] = g_strdup (return_vals[2].data.d_stringarray[i]);
+        }
     }
 
   gimp_destroy_params (return_vals, nreturn_vals);
diff --git a/libgimp/gimpproceduraldb_pdb.c b/libgimp/gimpproceduraldb_pdb.c
index 6cbbfb2..dd5c0af 100644
--- a/libgimp/gimpproceduraldb_pdb.c
+++ b/libgimp/gimpproceduraldb_pdb.c
@@ -162,10 +162,12 @@ gimp_procedural_db_query (const gchar   *name,
   if (success)
     {
       *num_matches = return_vals[1].data.d_int32;
-      *procedure_names = g_new (gchar *, *num_matches + 1);
-      for (i = 0; i < *num_matches; i++)
-        (*procedure_names)[i] = g_strdup (return_vals[2].data.d_stringarray[i]);
-      (*procedure_names)[i] = NULL;
+      if (*num_matches > 0)
+        {
+          *procedure_names = g_new0 (gchar *, *num_matches + 1);
+          for (i = 0; i < *num_matches; i++)
+            (*procedure_names)[i] = g_strdup (return_vals[2].data.d_stringarray[i]);
+        }
     }
 
   gimp_destroy_params (return_vals, nreturn_vals);
diff --git a/tools/pdbgen/lib.pl b/tools/pdbgen/lib.pl
index c548ab6..34830f4 100644
--- a/tools/pdbgen/lib.pl
+++ b/tools/pdbgen/lib.pl
@@ -273,10 +273,9 @@ CODE
                my $arg = $arg_types{$type};
                my $var;
            
-               my $ch = ""; my $cf = ""; my $numvarplus = "";
-               if ($type =~ /stringarray/) {
-                   $numvarplus = ' + 1';
-               }
+               my $ch = "";
+               my $cf = "";
+
                if ($type =~ /^string(array)?/) {
                    $ch = 'g_strdup (';
                    $cf = ')';
@@ -321,9 +320,23 @@ CODE
                    my $numvar = '*' . $_->{array}->{name};
                    $numvar = "num_$_->{name}" if exists $_->{array}->{no_lib};
 
-                   $return_marshal .= <<NEW . (($ch || $cf) ? <<CP1 : <<CP2);
+                   $return_marshal .= <<NUMVAR;
       $numvar = return_vals[$numpos].data.d_$numtype;
-      $var = g_new ($datatype, $numvar$numvarplus);
+NUMVAR
+
+                    if ($type =~ /stringarray/) {
+                        $return_marshal .= <<CP;
+      if ($numvar > 0)
+        {
+          $var = g_new0 ($datatype, $numvar + 1);
+          for (i = 0; i < $numvar; i++)
+            $dh$_->{name}$df\[i] = ${ch}return_vals[$argc].data.d_$type\[i]${cf};
+        }
+CP
+                   }
+                    else {
+                        $return_marshal .= <<NEW . (($ch || $cf) ? <<CP1 : <<CP2);
+      $var = g_new ($datatype, $numvar);
 NEW
       for (i = 0; i < $numvar; i++)
         $dh$_->{name}$df\[i] = ${ch}return_vals[$argc].data.d_$type\[i]${cf};
@@ -332,11 +345,7 @@ CP1
               return_vals[$argc].data.d_$type,
               $numvar * sizeof ($datatype));
 CP2
-                    if ($type =~ /stringarray/) {
-                       $return_marshal .= <<FINISH
-      $dh$_->{name}$df\[i] = NULL;
-FINISH
-                   }
+                    }
                    $out->{headers} = "#include <string.h>\n" unless ($ch || $cf);
                 }
                else {


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