r23073 - gnucash/trunk/src - Bug #703305: Fix crash on entering a non-valid date.

Christian Stimming cstim at code.gnucash.org
Sun Jun 30 16:18:37 EDT 2013


Author: cstim
Date: 2013-06-30 16:18:37 -0400 (Sun, 30 Jun 2013)
New Revision: 23073
Trac: http://svn.gnucash.org/trac/changeset/23073

Modified:
   gnucash/trunk/src/gnome-utils/gnc-date-edit.c
   gnucash/trunk/src/plugins/bi_import/dialog-bi-import.c
Log:
Bug #703305: Fix crash on entering a non-valid date.

For now, the date that couldn't get parsed is replaced by today. Improvements
to this behaviour are welcome, though :-)

Modified: gnucash/trunk/src/gnome-utils/gnc-date-edit.c
===================================================================
--- gnucash/trunk/src/gnome-utils/gnc-date-edit.c	2013-06-29 05:57:52 UTC (rev 23072)
+++ gnucash/trunk/src/gnome-utils/gnc-date-edit.c	2013-06-30 20:18:37 UTC (rev 23073)
@@ -262,14 +262,20 @@
 {
     GtkWidget *toplevel;
     struct tm mtm;
+    gboolean date_was_valid;
 
     g_return_if_fail (GNC_IS_DATE_EDIT (gde));
 
     ENTER("gde %p", gde);
 
     /* This code is pretty much just copied from gtk_date_edit_get_date */
-    qof_scan_date (gtk_entry_get_text (GTK_ENTRY (gde->date_entry)),
+    date_was_valid = qof_scan_date (gtk_entry_get_text (GTK_ENTRY (gde->date_entry)),
                    &mtm.tm_mday, &mtm.tm_mon, &mtm.tm_year);
+    if (!date_was_valid)
+    {
+        /* No valid date. Hacky workaround: Instead of crashing we randomly choose today's date. */
+        gnc_tm_get_today_start(&mtm);
+    }
 
     mtm.tm_mon--;
 
@@ -949,14 +955,24 @@
     struct tm tm = {0};
     char *str;
     gchar *flags = NULL;
+    gboolean date_was_valid;
 
     /* Assert, because we're just hosed if it's NULL */
     g_assert(gde != NULL);
     g_assert(GNC_IS_DATE_EDIT(gde));
 
-    qof_scan_date (gtk_entry_get_text (GTK_ENTRY (gde->date_entry)),
+    date_was_valid = qof_scan_date (gtk_entry_get_text (GTK_ENTRY (gde->date_entry)),
                    &tm.tm_mday, &tm.tm_mon, &tm.tm_year);
 
+    if (!date_was_valid)
+    {
+        /* Hm... no valid date. What should we do not? As a hacky workaround we
+        revert to today's date. Alternatively we can return some value that
+        signals that we don't get a valid date, but all callers of this
+        function will have to check this. Alas, I'm too lazy to do this here. */
+        gnc_tm_get_today_start(&tm);
+    }
+
     tm.tm_mon--;
 
     tm.tm_year -= 1900;

Modified: gnucash/trunk/src/plugins/bi_import/dialog-bi-import.c
===================================================================
--- gnucash/trunk/src/plugins/bi_import/dialog-bi-import.c	2013-06-29 05:57:52 UTC (rev 23072)
+++ gnucash/trunk/src/plugins/bi_import/dialog-bi-import.c	2013-06-30 20:18:37 UTC (rev 23073)
@@ -574,6 +574,7 @@
             gncInvoiceSetCurrency (invoice, gncOwnerGetCurrency (owner));	// Set the invoice currency based on the owner
             if (strlen (date_opened) != 0)	// If a date is specified in CSV
             {
+                // FIXME: Must check for the return value of qof_scan_date!
                 qof_scan_date (date_opened, &day, &month, &year);
                 gncInvoiceSetDateOpened (invoice,
                                          gnc_dmy2timespec (day, month, year));
@@ -657,6 +658,7 @@
 
         // add entry to invoice/bill
         entry = gncEntryCreate (book);
+        // FIXME: Must check for the return value of qof_scan_date!
         qof_scan_date (date, &day, &month, &year);
         {
             GDate *date = g_date_new_dmy(day, month, year);
@@ -720,6 +722,7 @@
                     // autopost this invoice
                     Timespec d1, d2;
                     d1 = gnc_dmy2timespec (day, month, year);
+                    // FIXME: Must check for the return value of qof_scan_date!
                     qof_scan_date (due_date, &day, &month, &year);	// obtains the due date, or leaves it at date_posted
                     d2 = gnc_dmy2timespec (day, month, year);
                     acc = gnc_account_lookup_for_register



More information about the gnucash-changes mailing list