Skip to content

Commit 31adc06

Browse files
NoahTheDukefogus
authored andcommitted
TRDR-73 Restrict Clojure symbols for array class notation
1 parent 16d9be2 commit 31adc06

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

src/main/clojure/clojure/tools/reader/impl/commons.clj

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,14 @@
107107
(let [ns-idx (inc ns-idx)]
108108
(when-not (== ns-idx (count token))
109109
(let [sym (subs token ns-idx)]
110-
(when (and (not (numeric? (nth sym 0)))
111-
(not (= "" sym))
112-
(not (.endsWith ns ":"))
113-
(or (= sym "/")
114-
(== -1 (.indexOf sym "/"))))
110+
(cond
111+
(re-matches #"[1-9]" sym)
112+
[ns sym]
113+
(and (not (numeric? (nth sym 0)))
114+
(not (= "" sym))
115+
(not (.endsWith ns ":"))
116+
(or (= sym "/")
117+
(== -1 (.indexOf sym "/"))))
115118
[ns sym]))))
116119
(when (or (= token "/")
117120
(== -1 (.indexOf token "/")))

src/test/clojure/clojure/tools/reader_test.clj

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,38 @@
199199
["[a\rb]" "[a\r b]" "[a \rb]"])]
200200
(doseq [pairs (partition 2 1 read-vals)]
201201
(is (= (first pairs) (second pairs))))))
202+
203+
(deftest read-symbol
204+
(is (= 'foo (read-string "foo")))
205+
(is (= 'foo/bar (read-string "foo/bar")))
206+
(is (= '*+!-_? (read-string "*+!-_?")))
207+
(is (= 'abc:def:ghi (read-string "abc:def:ghi")))
208+
(is (= 'abc.def/ghi (read-string "abc.def/ghi")))
209+
(is (= 'abc/def.ghi (read-string "abc/def.ghi")))
210+
(is (= 'abc:def/ghi:jkl.mno (read-string "abc:def/ghi:jkl.mno")))
211+
(is (instance? clojure.lang.Symbol (read-string "alphabet")))
212+
(is (= "foo//" (str (read-string "foo//"))))
213+
(is (java.lang.Double/isNaN ^double (read-string "##NaN")))
214+
(is (java.lang.Double/isInfinite ^double (read-string "##Inf")))
215+
(is (java.lang.Double/isInfinite ^double (read-string "##-Inf")))
216+
(testing "Correct array class symbols"
217+
(doseq [n (range 1 10)
218+
:let [sym (str "String/" n)
219+
qsym (str "java.lang.String/" n)]]
220+
(let [rsym (read-string sym)
221+
rqsym (read-string qsym)]
222+
(is (= ((juxt namespace name) rsym)
223+
["String" (str n)]))
224+
(is (= ((juxt namespace name) rqsym)
225+
["java.lang.String" (str n)])))))
226+
(testing "Correct prim array symbols"
227+
(doseq [prim ["int" "long" "boolean" "byte" "char" "double" "float" "short"]]
228+
(doseq [n (range 1 10)
229+
:let [sym (str prim "/" n)]]
230+
(let [rsym (read-string sym)]
231+
(is (= ((juxt namespace name) rsym)
232+
[prim (str n)]))))))
233+
(testing "Incorrect Array class symbols"
234+
(doseq [suffix ["" "0" "11" "1a"]
235+
:let [sym (str "String/" suffix)]]
236+
(is (thrown? clojure.lang.ExceptionInfo (read-string sym)) sym))))

0 commit comments

Comments
 (0)