diff options
| author | Mattias EngdegÄrd | 2019-07-10 19:24:58 +0200 |
|---|---|---|
| committer | Mattias EngdegÄrd | 2019-07-10 19:28:35 +0200 |
| commit | 83ed722c8d9d4d9d4143062584b89f79f3b8104a (patch) | |
| tree | f3198328e2c76888dff61833ba1ebad8440a9ce5 | |
| parent | ff5dd4ed76c9854ad8a566cea8ddcdf5c8ffabd6 (diff) | |
| download | emacs-83ed722c8d9d4d9d4143062584b89f79f3b8104a.tar.gz emacs-83ed722c8d9d4d9d4143062584b89f79f3b8104a.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.
| -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 136b18e48f5..c3efeeeb62c 100644 --- a/lisp/calc/calc-alg.el +++ b/lisp/calc/calc-alg.el | |||
| @@ -842,11 +842,13 @@ and should return the simplified expression to use (or nil)." | |||
| 842 | (and (eq calc-angle-mode 'rad) | 842 | (and (eq calc-angle-mode 'rad) |
| 843 | (let ((n (math-linear-in (nth 1 expr) '(var pi var-pi)))) | 843 | (let ((n (math-linear-in (nth 1 expr) '(var pi var-pi)))) |
| 844 | (and n | 844 | (and n |
| 845 | (math-div 1 (math-known-sin (car n) (nth 1 n) 120 300))))) | 845 | (let ((s (math-known-sin (car n) (nth 1 n) 120 300))) |
| 846 | (and s (math-div 1 s)))))) | ||
| 846 | (and (eq calc-angle-mode 'deg) | 847 | (and (eq calc-angle-mode 'deg) |
| 847 | (let ((n (math-integer-plus (nth 1 expr)))) | 848 | (let ((n (math-integer-plus (nth 1 expr)))) |
| 848 | (and n | 849 | (and n |
| 849 | (math-div 1 (math-known-sin (car n) (nth 1 n) '(frac 2 3) 300))))) | 850 | (let ((s (math-known-sin (car n) (nth 1 n) '(frac 2 3) 300))) |
| 851 | (and s (math-div 1 s)))))) | ||
| 850 | (and (eq (car-safe (nth 1 expr)) 'calcFunc-arcsin) | 852 | (and (eq (car-safe (nth 1 expr)) 'calcFunc-arcsin) |
| 851 | (math-div | 853 | (math-div |
| 852 | 1 | 854 | 1 |
| @@ -867,11 +869,13 @@ and should return the simplified expression to use (or nil)." | |||
| 867 | (and (eq calc-angle-mode 'rad) | 869 | (and (eq calc-angle-mode 'rad) |
| 868 | (let ((n (math-linear-in (nth 1 expr) '(var pi var-pi)))) | 870 | (let ((n (math-linear-in (nth 1 expr) '(var pi var-pi)))) |
| 869 | (and n | 871 | (and n |
| 870 | (math-div 1 (math-known-sin (car n) (nth 1 n) 120 0))))) | 872 | (let ((s (math-known-sin (car n) (nth 1 n) 120 0))) |
| 873 | (and s (math-div 1 s)))))) | ||
| 871 | (and (eq calc-angle-mode 'deg) | 874 | (and (eq calc-angle-mode 'deg) |
| 872 | (let ((n (math-integer-plus (nth 1 expr)))) | 875 | (let ((n (math-integer-plus (nth 1 expr)))) |
| 873 | (and n | 876 | (and n |
| 874 | (math-div 1 (math-known-sin (car n) (nth 1 n) '(frac 2 3) 0))))) | 877 | (let ((s (math-known-sin (car n) (nth 1 n) '(frac 2 3) 0))) |
| 878 | (and s (math-div 1 s)))))) | ||
| 875 | (and (eq (car-safe (nth 1 expr)) 'calcFunc-arcsin) | 879 | (and (eq (car-safe (nth 1 expr)) 'calcFunc-arcsin) |
| 876 | (math-div 1 (nth 1 (nth 1 expr)))) | 880 | (math-div 1 (nth 1 (nth 1 expr)))) |
| 877 | (and (eq (car-safe (nth 1 expr)) 'calcFunc-arccos) | 881 | (and (eq (car-safe (nth 1 expr)) 'calcFunc-arccos) |
| @@ -972,11 +976,13 @@ and should return the simplified expression to use (or nil)." | |||
| 972 | (and (eq calc-angle-mode 'rad) | 976 | (and (eq calc-angle-mode 'rad) |
| 973 | (let ((n (math-linear-in (nth 1 expr) '(var pi var-pi)))) | 977 | (let ((n (math-linear-in (nth 1 expr) '(var pi var-pi)))) |
| 974 | (and n | 978 | (and n |
| 975 | (math-div 1 (math-known-tan (car n) (nth 1 n) 120))))) | 979 | (let ((tn (math-known-tan (car n) (nth 1 n) 120))) |
| 980 | (and tn (math-div 1 tn)))))) | ||
| 976 | (and (eq calc-angle-mode 'deg) | 981 | (and (eq calc-angle-mode 'deg) |
| 977 | (let ((n (math-integer-plus (nth 1 expr)))) | 982 | (let ((n (math-integer-plus (nth 1 expr)))) |
| 978 | (and n | 983 | (and n |
| 979 | (math-div 1 (math-known-tan (car n) (nth 1 n) '(frac 2 3)))))) | 984 | (let ((tn (math-known-tan (car n) (nth 1 n) '(frac 2 3)))) |
| 985 | (and tn (math-div 1 tn)))))) | ||
| 980 | (and (eq (car-safe (nth 1 expr)) 'calcFunc-arcsin) | 986 | (and (eq (car-safe (nth 1 expr)) 'calcFunc-arcsin) |
| 981 | (math-div (list 'calcFunc-sqrt | 987 | (math-div (list 'calcFunc-sqrt |
| 982 | (math-sub 1 (math-sqr (nth 1 (nth 1 expr))))) | 988 | (math-sub 1 (math-sqr (nth 1 (nth 1 expr))))) |
diff --git a/test/lisp/calc/calc-tests.el b/test/lisp/calc/calc-tests.el index 96c75119c9c..92f74976b00 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 | (provide 'calc-tests) | 171 | (provide 'calc-tests) |
| 142 | ;;; calc-tests.el ends here | 172 | ;;; calc-tests.el ends here |
| 143 | 173 | ||