diff options
| author | Mattias EngdegÄrd | 2019-07-10 19:24:58 +0200 |
|---|---|---|
| committer | Mattias EngdegÄrd | 2019-09-23 15:06:53 +0200 |
| commit | bd0f173199c112b2b146e727f80e973e7fc12143 (patch) | |
| tree | 36e80c5d5b1bba4ee98e6a5ed085fdbe7de3bfa1 | |
| parent | 73e1727c405214086bb3a0647c91855e1b0853c2 (diff) | |
| download | emacs-bd0f173199c112b2b146e727f80e973e7fc12143.tar.gz emacs-bd0f173199c112b2b146e727f80e973e7fc12143.zip | |
Fix trig simplification crash (bug#33052)
* lisp/calc/calc-alg.el (calcFunc-sec, calcFunc-csc, calcFunc-cot):
Check that `math-known-sin' and `math-known-tan' succeeded before
using their value in arithmetic.
* test/lisp/calc/calc-tests.el (calc-test-trig): Add regression tests.
Backport from master.
| -rw-r--r-- | lisp/calc/calc-alg.el | 18 | ||||
| -rw-r--r-- | test/lisp/calc/calc-tests.el | 30 |
2 files changed, 42 insertions, 6 deletions
diff --git a/lisp/calc/calc-alg.el b/lisp/calc/calc-alg.el index 8e3476d191e..b41749ca775 100644 --- a/lisp/calc/calc-alg.el +++ b/lisp/calc/calc-alg.el | |||
| @@ -847,11 +847,13 @@ | |||
| 847 | (and (eq calc-angle-mode 'rad) | 847 | (and (eq calc-angle-mode 'rad) |
| 848 | (let ((n (math-linear-in (nth 1 math-simplify-expr) '(var pi var-pi)))) | 848 | (let ((n (math-linear-in (nth 1 math-simplify-expr) '(var pi var-pi)))) |
| 849 | (and n | 849 | (and n |
| 850 | (math-div 1 (math-known-sin (car n) (nth 1 n) 120 300))))) | 850 | (let ((s (math-known-sin (car n) (nth 1 n) 120 300))) |
| 851 | (and s (math-div 1 s)))))) | ||
| 851 | (and (eq calc-angle-mode 'deg) | 852 | (and (eq calc-angle-mode 'deg) |
| 852 | (let ((n (math-integer-plus (nth 1 math-simplify-expr)))) | 853 | (let ((n (math-integer-plus (nth 1 math-simplify-expr)))) |
| 853 | (and n | 854 | (and n |
| 854 | (math-div 1 (math-known-sin (car n) (nth 1 n) '(frac 2 3) 300))))) | 855 | (let ((s (math-known-sin (car n) (nth 1 n) '(frac 2 3) 300))) |
| 856 | (and s (math-div 1 s)))))) | ||
| 855 | (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arcsin) | 857 | (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arcsin) |
| 856 | (math-div | 858 | (math-div |
| 857 | 1 | 859 | 1 |
| @@ -872,11 +874,13 @@ | |||
| 872 | (and (eq calc-angle-mode 'rad) | 874 | (and (eq calc-angle-mode 'rad) |
| 873 | (let ((n (math-linear-in (nth 1 math-simplify-expr) '(var pi var-pi)))) | 875 | (let ((n (math-linear-in (nth 1 math-simplify-expr) '(var pi var-pi)))) |
| 874 | (and n | 876 | (and n |
| 875 | (math-div 1 (math-known-sin (car n) (nth 1 n) 120 0))))) | 877 | (let ((s (math-known-sin (car n) (nth 1 n) 120 0))) |
| 878 | (and s (math-div 1 s)))))) | ||
| 876 | (and (eq calc-angle-mode 'deg) | 879 | (and (eq calc-angle-mode 'deg) |
| 877 | (let ((n (math-integer-plus (nth 1 math-simplify-expr)))) | 880 | (let ((n (math-integer-plus (nth 1 math-simplify-expr)))) |
| 878 | (and n | 881 | (and n |
| 879 | (math-div 1 (math-known-sin (car n) (nth 1 n) '(frac 2 3) 0))))) | 882 | (let ((s (math-known-sin (car n) (nth 1 n) '(frac 2 3) 0))) |
| 883 | (and s (math-div 1 s)))))) | ||
| 880 | (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arcsin) | 884 | (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arcsin) |
| 881 | (math-div 1 (nth 1 (nth 1 math-simplify-expr)))) | 885 | (math-div 1 (nth 1 (nth 1 math-simplify-expr)))) |
| 882 | (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arccos) | 886 | (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arccos) |
| @@ -977,11 +981,13 @@ | |||
| 977 | (and (eq calc-angle-mode 'rad) | 981 | (and (eq calc-angle-mode 'rad) |
| 978 | (let ((n (math-linear-in (nth 1 math-simplify-expr) '(var pi var-pi)))) | 982 | (let ((n (math-linear-in (nth 1 math-simplify-expr) '(var pi var-pi)))) |
| 979 | (and n | 983 | (and n |
| 980 | (math-div 1 (math-known-tan (car n) (nth 1 n) 120))))) | 984 | (let ((tn (math-known-tan (car n) (nth 1 n) 120))) |
| 985 | (and tn (math-div 1 tn)))))) | ||
| 981 | (and (eq calc-angle-mode 'deg) | 986 | (and (eq calc-angle-mode 'deg) |
| 982 | (let ((n (math-integer-plus (nth 1 math-simplify-expr)))) | 987 | (let ((n (math-integer-plus (nth 1 math-simplify-expr)))) |
| 983 | (and n | 988 | (and n |
| 984 | (math-div 1 (math-known-tan (car n) (nth 1 n) '(frac 2 3)))))) | 989 | (let ((tn (math-known-tan (car n) (nth 1 n) '(frac 2 3)))) |
| 990 | (and tn (math-div 1 tn)))))) | ||
| 985 | (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arcsin) | 991 | (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arcsin) |
| 986 | (math-div (list 'calcFunc-sqrt | 992 | (math-div (list 'calcFunc-sqrt |
| 987 | (math-sub 1 (math-sqr (nth 1 (nth 1 math-simplify-expr))))) | 993 | (math-sub 1 (math-sqr (nth 1 (nth 1 math-simplify-expr))))) |
diff --git a/test/lisp/calc/calc-tests.el b/test/lisp/calc/calc-tests.el index 3f0b65aeeef..1c89857a185 100644 --- a/test/lisp/calc/calc-tests.el +++ b/test/lisp/calc/calc-tests.el | |||
| @@ -138,6 +138,36 @@ An existing calc stack is reused, otherwise a new one is created." | |||
| 138 | (nth 1 (calcFunc-cos 1))) | 138 | (nth 1 (calcFunc-cos 1))) |
| 139 | 0 4)))))) | 139 | 0 4)))))) |
| 140 | 140 | ||
| 141 | (ert-deftest calc-test-trig () | ||
| 142 | "Trigonometric simplification; bug#33052." | ||
| 143 | (let ((calc-angle-mode 'rad)) | ||
| 144 | (let ((calc-symbolic-mode t)) | ||
| 145 | (should (equal (math-simplify '(calcFunc-sin (/ (var pi var-pi) 4))) | ||
| 146 | '(/ (calcFunc-sqrt 2) 2))) | ||
| 147 | (should (equal (math-simplify '(calcFunc-cos (/ (var pi var-pi) 4))) | ||
| 148 | '(/ (calcFunc-sqrt 2) 2))) | ||
| 149 | (should (equal (math-simplify '(calcFunc-sec (/ (var pi var-pi) 4))) | ||
| 150 | '(calcFunc-sqrt 2))) | ||
| 151 | (should (equal (math-simplify '(calcFunc-csc (/ (var pi var-pi) 4))) | ||
| 152 | '(calcFunc-sqrt 2))) | ||
| 153 | (should (equal (math-simplify '(calcFunc-tan (/ (var pi var-pi) 3))) | ||
| 154 | '(calcFunc-sqrt 3))) | ||
| 155 | (should (equal (math-simplify '(calcFunc-cot (/ (var pi var-pi) 3))) | ||
| 156 | '(/ (calcFunc-sqrt 3) 3)))) | ||
| 157 | (let ((calc-symbolic-mode nil)) | ||
| 158 | (should (equal (math-simplify '(calcFunc-sin (/ (var pi var-pi) 4))) | ||
| 159 | '(calcFunc-sin (/ (var pi var-pi) 4)))) | ||
| 160 | (should (equal (math-simplify '(calcFunc-cos (/ (var pi var-pi) 4))) | ||
| 161 | '(calcFunc-cos (/ (var pi var-pi) 4)))) | ||
| 162 | (should (equal (math-simplify '(calcFunc-sec (/ (var pi var-pi) 4))) | ||
| 163 | '(calcFunc-sec (/ (var pi var-pi) 4)))) | ||
| 164 | (should (equal (math-simplify '(calcFunc-csc (/ (var pi var-pi) 4))) | ||
| 165 | '(calcFunc-csc (/ (var pi var-pi) 4)))) | ||
| 166 | (should (equal (math-simplify '(calcFunc-tan (/ (var pi var-pi) 3))) | ||
| 167 | '(calcFunc-tan (/ (var pi var-pi) 3)))) | ||
| 168 | (should (equal (math-simplify '(calcFunc-cot (/ (var pi var-pi) 3))) | ||
| 169 | '(calcFunc-cot (/ (var pi var-pi) 3))))))) | ||
| 170 | |||
| 141 | (ert-deftest calc-test-solve-linear-system () | 171 | (ert-deftest calc-test-solve-linear-system () |
| 142 | "Test linear system solving (bug#35374)." | 172 | "Test linear system solving (bug#35374)." |
| 143 | ;; x + y = 3 | 173 | ;; x + y = 3 |