Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Open
nahuel opened this Issue · 0 comments

1 participant

@nahuel

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.

@nahuel nahuel referenced this issue from a commit in nahuel/clj-stacktrace
@nahuel nahuel fixes IndexOutOfBoundsExceptions when trying to print exceptions with…
… short stack traces (< 3 StackElements). Fixes mmcgrana/clj-stacktrace#25 and mmcgrana/clj-stacktrace#26
62d3d19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.