gnucash maint: un_escape: More terse, more correct.

John Ralls jralls at code.gnucash.org
Thu Dec 20 01:36:27 EST 2018


Updated	 via  https://github.com/Gnucash/gnucash/commit/127c658f (commit)
	from  https://github.com/Gnucash/gnucash/commit/f2976420 (commit)



commit 127c658f05afd4bdddfdd0c04d02aa7fd9264c76
Author: John Ralls <jralls at ceridwen.us>
Date:   Wed Dec 19 22:35:18 2018 -0800

    un_escape: More terse, more correct.
    
    Doesn't run past the end of the input string even if the last
    character is a quote.

diff --git a/gnucash/import-export/bi-import/dialog-bi-import.c b/gnucash/import-export/bi-import/dialog-bi-import.c
index 6aabcc3..894eeec 100644
--- a/gnucash/import-export/bi-import/dialog-bi-import.c
+++ b/gnucash/import-export/bi-import/dialog-bi-import.c
@@ -877,25 +877,21 @@ gnc_bi_import_create_bis (GtkListStore * store, QofBook * book,
  * @return char* Modified string.
 */
 static char*
+static char*
 un_escape(char *str)
 {
     gchar quote = '"';
     gchar *newStr = NULL, *tmpstr = str;
-    int n = 0;
+    int n = strlen (str), i;
+    newStr = g_malloc (n + 1);
+    memset (newStr, 0, n + 1);
 
-    newStr = g_malloc(strlen(str)+1); // This must be freed in the calling code.
-    while(*tmpstr != '\0')
+    for (i = 0; *tmpstr != '\0'; ++i, ++tmpstr)
     {
-        if(*tmpstr == quote)
-            // We always want the character after a quote.
-            newStr[n] = *(++tmpstr);
-        else
-            newStr[n] = *tmpstr;
-        ++tmpstr;
-        ++n;
+        newStr[i] = *tmpstr == quote ? *(++tmpstr) : *(tmpstr);
+        if (*tmpstr == '\0')
+            break;
     }
-
     g_free (str);
-    newStr[n] = '\0'; //ending the character array
     return newStr;
 }



Summary of changes:
 gnucash/import-export/bi-import/dialog-bi-import.c | 20 ++++++++------------
 1 file changed, 8 insertions(+), 12 deletions(-)



More information about the gnucash-changes mailing list