Skip to content

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

@nahuel

Description

@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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions