Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clj-stacktrace.rpl/pst-on raises IndexOutOfBoundsException for exceptions with stack traces one or two elements deep #26

Open
nahuel opened this issue Aug 19, 2013 · 0 comments

Comments

@nahuel
Copy link
Contributor

nahuel commented Aug 19, 2013

The following code raises a java.lang.ArithmeticException with an one-element stack trace that clj-stacktrace.repl/find-source-width can't handle, so an IndexOutOfBoundsException is raised when trying to print it. Versions used: [clj-stacktrace "0.2.6"] and [org.clojure/tools.trace "0.7.5"].

(try
  (clojure.tools.trace/trace-forms
    (let [a (+ 1 1)
          b (* 2 2)
          c (* a b (/ 4 0))]
      c))
  (catch Exception e
    ;; this will throw IndexOutOfBoundsException:
    (clj-stacktrace.repl/pst-on *err* false e)    
   ))

clojure.tools.trace/trace-forms creates an exception with a single element in his stacktrace (check clojure.tools.trace/trace-compose-exception). When this exception is parsed by clj-stacktrace.core/parse-exception you get something like this:

{:class java.lang.ArithmeticException, 
 :message "Divide by zero\n  Form failed: (/ 4 0)\n  Form failed: (* a b (/ 4 0))\n         Form failed: (let* [a (+ 1 1) b (* 2 2) c (* a b (/ 4 0))] c)\n  Form failed: (let [a (+ 1 1) b (* 2 2) c (* a b (/ 4 0))] c)\n", 
 :trace-elems ({:method "divide", 
                :class "clojure.lang.Numbers", 
                :java true, 
                :file "Numbers.java", :line 156})}

but clj-stacktrace.utils/quartile3 is BROKEN for one-element collections, so when is called (after pst-on -> find-source-width -> utils/fence) it fails on the following expression:

(defn quartile3
  [coll]
  (let [c (count coll)]                    ;=> c = 1
    (nth coll (if (even? c)
                (/ (+ (* 3 c) 2) 4)        ;=> 5/4
                (/ (inc (* 3 c)) 4)))))

;=>  (nth coll 5/4) ==>  IndexOutOfBoundsException

Simple test:

(clj-stacktrace.utils/quartile3 [1])      ;=> IndexOutOfBoundsException
(clj-stacktrace.utils/quartile3 [1 2])    ;=> IndexOutOfBoundsException
(clj-stacktrace.utils/quartile3 [1 2 3])  ;=> ok if count > 2

I think this bug is related to the following closed ticket:

#20

But the @jonpither fix in that ticket didn't resolved this problem. It happens in clj-stacktrace 0.2.5 before the fix and after the fix was merged in 0.2.6.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant