From a4789fcac57d9a8f14b78a52349ff1adebc823ee Mon Sep 17 00:00:00 2001 From: Christopher Lam Date: Sun, 10 Feb 2019 22:04:03 +0800 Subject: [PATCH] bugfix: fix crasher when loading saved-report with unknown choices. If a saved-report with e.g. relative date, multichoice option is unknown, the report would crash, and the Report-Options would segfault. This commit fixes both: report-date defaults to 'today', multichoice-options defaults to default-value. Following this commit, if a report loads a saved-report or .gcm from a future version, a gnc:warn will be emitted and the report will not crash; it will use relative-date today. Multichoice will remain the default value. Report Options will not segfault. The user will be notified via a gnc:gui-warn dialog --- libgnucash/app-utils/date-utilities.scm | 10 +++++-- libgnucash/app-utils/options.scm | 38 ++++++++++++++++--------- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/libgnucash/app-utils/date-utilities.scm b/libgnucash/app-utils/date-utilities.scm index 1d6606189e..e918c0177d 100644 --- a/libgnucash/app-utils/date-utilities.scm +++ b/libgnucash/app-utils/date-utilities.scm @@ -430,8 +430,14 @@ (define (gnc:get-absolute-from-relative-date date-symbol) (let ((rel-date-data (hash-ref gnc:relative-date-hash date-symbol))) (if rel-date-data - ((gnc:reldate-get-fn rel-date-data)) - (gnc:error "Tried to look up an undefined date symbol")))) + ((gnc:reldate-get-fn rel-date-data)) + (let* ((msg (_ "Tried to look up an undefined date symbol \ +'~a'. This report was probably saved by a later version of GnuCash. \ +Defaulting to today.")) + (conmsg (format #f msg date-symbol)) + (uimsg (format #f (_ msg) date-symbol))) + (gnc:gui-warn conmsg uimsg) + (current-time))))) (define (gnc:get-relative-date-strings date-symbol) (let ((rel-date-info (hash-ref gnc:relative-date-hash date-symbol))) diff --git a/libgnucash/app-utils/options.scm b/libgnucash/app-utils/options.scm index e8a676b43d..c87dbf165a 100644 --- a/libgnucash/app-utils/options.scm +++ b/libgnucash/app-utils/options.scm @@ -17,6 +17,18 @@ ;; 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 ;; Boston, MA 02110-1301, USA gnu@gnu.org (use-modules (ice-9 regex)) +(use-modules (gnucash gettext)) + +(define (rpterror-earlier type newoption fallback) + ;; Translators: the 3 ~a below refer to (1) option type (2) unknown + ;; new option name, (3) fallback option name. The order is + ;; important, and must not be changed. + (let* ((template (N_ "This report was saved using a later version of \ +GnuCash. One of the newer ~a options '~a' is not available, fallback to \ +the option '~a'.")) + (console-msg (format #f template type newoption fallback)) + (ui-msg (format #f (_ template) type newoption fallback))) + (gnc:gui-warn console-msg ui-msg))) (define (gnc:make-option ;; The category of this option @@ -580,11 +592,11 @@ (if (pair? (cdr date)) (cons (car date) (cadr date)) date)) - (define (list-lookup list item) - (cond - ((null? list) #f) - ((eq? item (car list)) 0) - (else (+ 1 (list-lookup (cdr list) item))))) + (define (list-lookup full-list item) + (or (list-index (lambda (i) (eq? i item)) full-list) + (begin + (rpterror-earlier "date" item (car full-list)) + 0))) (let* ((value (default-getter)) (value->string (lambda () (string-append "'" (gnc:value->string value))))) @@ -862,11 +874,11 @@ validator (cons #f acct-type-list) #f #f #f))) -(define (gnc:multichoice-list-lookup list item ) - (cond - ((null? list) #f) - ((eq? item (vector-ref (car list) 0)) 0) - (else (+ 1 (gnc:multichoice-list-lookup (cdr list) item))))) +(define (gnc:multichoice-list-lookup full-lst item) + (or (list-index (lambda (i) (eq? (vector-ref i 0) item)) full-lst) + (begin + (rpterror-earlier "multichoice" item (car full-lst)) + 0))) ;; multichoice options use the option-data as a list of vectors. ;; Each vector contains a permissible value (scheme symbol), a @@ -930,7 +942,7 @@ (set! value x) (if (procedure? setter-function-called-cb) (setter-function-called-cb x))) - (gnc:error "Illegal Multichoice option set"))) + (rpterror-earlier "multichoice" x default-value))) (lambda () default-value) (gnc:restore-form-generator value->string) (lambda (b p) (qof-book-set-option b (symbol->string value) p)) @@ -1016,7 +1028,7 @@ (set! value x) (if (procedure? setter-function-called-cb) (setter-function-called-cb x))) - (gnc:error "Illegal Radiobutton option set"))) + (rpterror-earlier "radiobutton" x default-value))) (lambda () default-value) (gnc:restore-form-generator value->string) (lambda (b p) (qof-book-set-option b (symbol->string value) p)) @@ -1078,7 +1090,7 @@ (lambda (x) (if (list-legal x) (set! value x) - (gnc:error "Illegal list option set"))) + (rpterror-earlier "list" x default-value))) (lambda () default-value) (gnc:restore-form-generator value->string) (lambda (b p)