[gnumeric] Dif: avoid using g_alloca.



commit 13fbc080861d178a009e772225bd521044769969
Author: Morten Welinder <terra gnome org>
Date:   Fri Dec 1 19:03:12 2017 -0500

    Dif: avoid using g_alloca.
    
    g_alloca allocations can be convenient, but using stack space propertional
    to the file size is not good.

 NEWS                  |    3 +++
 plugins/dif/ChangeLog |    4 ++++
 plugins/dif/dif.c     |   43 ++++++++++++++++++++++++++-----------------
 3 files changed, 33 insertions(+), 17 deletions(-)
---
diff --git a/NEWS b/NEWS
index 65c7b38..2545795 100644
--- a/NEWS
+++ b/NEWS
@@ -1,5 +1,8 @@
 Gnumeric 1.12.38
 
+Morten:
+       * Avoid large stack use here and there.
+
 --------------------------------------------------------------------------
 Gnumeric 1.12.37
 
diff --git a/plugins/dif/ChangeLog b/plugins/dif/ChangeLog
index 272185b..cc1e220 100644
--- a/plugins/dif/ChangeLog
+++ b/plugins/dif/ChangeLog
@@ -1,3 +1,7 @@
+2017-12-01  Morten Welinder  <terra gnome org>
+
+       * dif.c (dif_parse_header): Use heap, not stack, allocation.
+
 2017-12-01  Morten Welinder <terra gnome org>
 
        * Release 1.12.37
diff --git a/plugins/dif/dif.c b/plugins/dif/dif.c
index c264f13..3284c49 100644
--- a/plugins/dif/dif.c
+++ b/plugins/dif/dif.c
@@ -105,24 +105,28 @@ static gboolean
 dif_parse_header (DifInputContext *ctxt)
 {
        while (1) {
-               gchar *topic, *num_line, *str_line;
+               gchar *topic = NULL, *num_line = NULL, *str_line = NULL;
                size_t str_line_len;
+               int res = -1;
 
-               if (!dif_get_line (ctxt))
-                       return FALSE;
-               topic = g_alloca (strlen (ctxt->line) + 1);
-               strcpy (topic, ctxt->line);
+               if (!dif_get_line (ctxt)) {
+                       res = FALSE;
+                       goto out;
+               }
+               topic = g_strdup (ctxt->line);
 
-               if (!dif_get_line (ctxt))
-                       return FALSE;
-               num_line = g_alloca (strlen (ctxt->line) + 1);
-               strcpy (num_line, ctxt->line);
+               if (!dif_get_line (ctxt)) {
+                       res = FALSE;
+                       goto out;
+               }
+               num_line = g_strdup (ctxt->line);
 
-               if (!dif_get_line (ctxt))
-                       return FALSE;
-               str_line_len = strlen (ctxt->line);
-               str_line = g_alloca (str_line_len + 1);
-               strcpy (str_line, ctxt->line);
+               if (!dif_get_line (ctxt)) {
+                       res = FALSE;
+                       goto out;
+               }
+               str_line = g_strdup (ctxt->line);
+               str_line_len = strlen (str_line);
 
                if (strcmp (topic, "TABLE") == 0) {
                        gchar *name;
@@ -137,7 +141,7 @@ dif_parse_header (DifInputContext *ctxt)
                                /* FIXME - rename the sheet */
                        }
                } else if (strcmp (topic, "DATA") == 0) {
-                       break;
+                       res = TRUE;
                }
 
                /*
@@ -145,9 +149,14 @@ dif_parse_header (DifInputContext *ctxt)
                 * SIZE, LABEL, UNITS, TUPLES, VECTORS, COMMENT, MINORSTART,
                 * TRUELENGTH, PERIODICITY, DISPLAYUNITS
                 */
-       }
 
-       return TRUE;
+       out:
+               g_free (topic);
+               g_free (num_line);
+               g_free (str_line);
+               if (res >= 0)
+                       return res;
+       }
 }
 
 /*


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