emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] emacs-26 bd0f173: Fix trig simplification crash (bug#33052


From: Mattias Engdegård
Subject: [Emacs-diffs] emacs-26 bd0f173: Fix trig simplification crash (bug#33052)
Date: Mon, 23 Sep 2019 09:45:07 -0400 (EDT)

branch: emacs-26
commit bd0f173199c112b2b146e727f80e973e7fc12143
Author: Mattias Engdegård <address@hidden>
Commit: Mattias Engdegård <address@hidden>

    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.
---
 lisp/calc/calc-alg.el        | 18 ++++++++++++------
 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 8e3476d..b41749c 100644
--- a/lisp/calc/calc-alg.el
+++ b/lisp/calc/calc-alg.el
@@ -847,11 +847,13 @@
       (and (eq calc-angle-mode 'rad)
           (let ((n (math-linear-in (nth 1 math-simplify-expr) '(var pi 
var-pi))))
             (and n
-                 (math-div 1 (math-known-sin (car n) (nth 1 n) 120 300)))))
+                  (let ((s (math-known-sin (car n) (nth 1 n) 120 300)))
+                    (and s (math-div 1 s))))))
       (and (eq calc-angle-mode 'deg)
           (let ((n (math-integer-plus (nth 1 math-simplify-expr))))
             (and n
-                  (math-div 1 (math-known-sin (car n) (nth 1 n) '(frac 2 3) 
300)))))
+                  (let ((s (math-known-sin (car n) (nth 1 n) '(frac 2 3) 300)))
+                    (and s (math-div 1 s))))))
       (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arcsin)
            (math-div
             1
@@ -872,11 +874,13 @@
       (and (eq calc-angle-mode 'rad)
           (let ((n (math-linear-in (nth 1 math-simplify-expr) '(var pi 
var-pi))))
             (and n
-                  (math-div 1 (math-known-sin (car n) (nth 1 n) 120 0)))))
+                  (let ((s (math-known-sin (car n) (nth 1 n) 120 0)))
+                    (and s (math-div 1 s))))))
       (and (eq calc-angle-mode 'deg)
           (let ((n (math-integer-plus (nth 1 math-simplify-expr))))
             (and n
-                  (math-div 1 (math-known-sin (car n) (nth 1 n) '(frac 2 3) 
0)))))
+                  (let ((s (math-known-sin (car n) (nth 1 n) '(frac 2 3) 0)))
+                    (and s (math-div 1 s))))))
       (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arcsin)
           (math-div 1 (nth 1 (nth 1 math-simplify-expr))))
       (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arccos)
@@ -977,11 +981,13 @@
       (and (eq calc-angle-mode 'rad)
           (let ((n (math-linear-in (nth 1 math-simplify-expr) '(var pi 
var-pi))))
             (and n
-                  (math-div 1 (math-known-tan (car n) (nth 1 n) 120)))))
+                  (let ((tn (math-known-tan (car n) (nth 1 n) 120)))
+                    (and tn (math-div 1 tn))))))
       (and (eq calc-angle-mode 'deg)
           (let ((n (math-integer-plus (nth 1 math-simplify-expr))))
             (and n
-                  (math-div 1 (math-known-tan (car n) (nth 1 n) '(frac 2 
3))))))
+                  (let ((tn (math-known-tan (car n) (nth 1 n) '(frac 2 3))))
+                    (and tn (math-div 1 tn))))))
       (and (eq (car-safe (nth 1 math-simplify-expr)) 'calcFunc-arcsin)
           (math-div (list 'calcFunc-sqrt
                           (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 3f0b65a..1c89857 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."
                          (nth 1 (calcFunc-cos 1)))
                         0 4))))))
 
+(ert-deftest calc-test-trig ()
+  "Trigonometric simplification; bug#33052."
+  (let ((calc-angle-mode 'rad))
+    (let ((calc-symbolic-mode t))
+      (should (equal (math-simplify '(calcFunc-sin (/ (var pi var-pi) 4)))
+                     '(/ (calcFunc-sqrt 2) 2)))
+      (should (equal (math-simplify '(calcFunc-cos (/ (var pi var-pi) 4)))
+                     '(/ (calcFunc-sqrt 2) 2)))
+      (should (equal (math-simplify '(calcFunc-sec (/ (var pi var-pi) 4)))
+                     '(calcFunc-sqrt 2)))
+      (should (equal (math-simplify '(calcFunc-csc (/ (var pi var-pi) 4)))
+                     '(calcFunc-sqrt 2)))
+      (should (equal (math-simplify '(calcFunc-tan (/ (var pi var-pi) 3)))
+                     '(calcFunc-sqrt 3)))
+      (should (equal (math-simplify '(calcFunc-cot (/ (var pi var-pi) 3)))
+                     '(/ (calcFunc-sqrt 3) 3))))
+    (let ((calc-symbolic-mode nil))
+      (should (equal (math-simplify '(calcFunc-sin (/ (var pi var-pi) 4)))
+                     '(calcFunc-sin (/ (var pi var-pi) 4))))
+      (should (equal (math-simplify '(calcFunc-cos (/ (var pi var-pi) 4)))
+                     '(calcFunc-cos (/ (var pi var-pi) 4))))
+      (should (equal (math-simplify '(calcFunc-sec (/ (var pi var-pi) 4)))
+                     '(calcFunc-sec (/ (var pi var-pi) 4))))
+      (should (equal (math-simplify '(calcFunc-csc (/ (var pi var-pi) 4)))
+                     '(calcFunc-csc (/ (var pi var-pi) 4))))
+      (should (equal (math-simplify '(calcFunc-tan (/ (var pi var-pi) 3)))
+                     '(calcFunc-tan (/ (var pi var-pi) 3))))
+      (should (equal (math-simplify '(calcFunc-cot (/ (var pi var-pi) 3)))
+                     '(calcFunc-cot (/ (var pi var-pi) 3)))))))
+
 (ert-deftest calc-test-solve-linear-system ()
   "Test linear system solving (bug#35374)."
   ;;   x + y =   3



reply via email to

[Prev in Thread] Current Thread [Next in Thread]