Skip to content

Commit

Permalink
TRDR-73 Restrict Clojure symbols for array class notation
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahTheDuke authored and fogus committed Aug 19, 2024
1 parent 16d9be2 commit 31adc06
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/main/clojure/clojure/tools/reader/impl/commons.clj
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,14 @@
(let [ns-idx (inc ns-idx)]
(when-not (== ns-idx (count token))
(let [sym (subs token ns-idx)]
(when (and (not (numeric? (nth sym 0)))
(not (= "" sym))
(not (.endsWith ns ":"))
(or (= sym "/")
(== -1 (.indexOf sym "/"))))
(cond
(re-matches #"[1-9]" sym)
[ns sym]
(and (not (numeric? (nth sym 0)))
(not (= "" sym))
(not (.endsWith ns ":"))
(or (= sym "/")
(== -1 (.indexOf sym "/"))))
[ns sym]))))
(when (or (= token "/")
(== -1 (.indexOf token "/")))
Expand Down
35 changes: 35 additions & 0 deletions src/test/clojure/clojure/tools/reader_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,38 @@
["[a\rb]" "[a\r b]" "[a \rb]"])]
(doseq [pairs (partition 2 1 read-vals)]
(is (= (first pairs) (second pairs))))))

(deftest read-symbol
(is (= 'foo (read-string "foo")))
(is (= 'foo/bar (read-string "foo/bar")))
(is (= '*+!-_? (read-string "*+!-_?")))
(is (= 'abc:def:ghi (read-string "abc:def:ghi")))
(is (= 'abc.def/ghi (read-string "abc.def/ghi")))
(is (= 'abc/def.ghi (read-string "abc/def.ghi")))
(is (= 'abc:def/ghi:jkl.mno (read-string "abc:def/ghi:jkl.mno")))
(is (instance? clojure.lang.Symbol (read-string "alphabet")))
(is (= "foo//" (str (read-string "foo//"))))
(is (java.lang.Double/isNaN ^double (read-string "##NaN")))
(is (java.lang.Double/isInfinite ^double (read-string "##Inf")))
(is (java.lang.Double/isInfinite ^double (read-string "##-Inf")))
(testing "Correct array class symbols"
(doseq [n (range 1 10)
:let [sym (str "String/" n)
qsym (str "java.lang.String/" n)]]
(let [rsym (read-string sym)
rqsym (read-string qsym)]
(is (= ((juxt namespace name) rsym)
["String" (str n)]))
(is (= ((juxt namespace name) rqsym)
["java.lang.String" (str n)])))))
(testing "Correct prim array symbols"
(doseq [prim ["int" "long" "boolean" "byte" "char" "double" "float" "short"]]
(doseq [n (range 1 10)
:let [sym (str prim "/" n)]]
(let [rsym (read-string sym)]
(is (= ((juxt namespace name) rsym)
[prim (str n)]))))))
(testing "Incorrect Array class symbols"
(doseq [suffix ["" "0" "11" "1a"]
:let [sym (str "String/" suffix)]]
(is (thrown? clojure.lang.ExceptionInfo (read-string sym)) sym))))

0 comments on commit 31adc06

Please sign in to comment.