Skip to content

Commit 31484fb

Browse files
authored
Merge pull request #337 from chrovis/fix/unchecked-math-overflow
Fix overflow issues occurs when *unchecked-math* is disabled
2 parents 2c12beb + 1e69b95 commit 31484fb

File tree

8 files changed

+28
-30
lines changed

8 files changed

+28
-30
lines changed

project.clj

+3-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
:1.8 {:dependencies [[org.clojure/clojure "1.8.0"]]}
3333
:1.9 {:dependencies [[org.clojure/clojure "1.9.0"]]}
3434
:1.10 {:dependencies [[org.clojure/clojure "1.10.3"]]}
35-
:1.11 {:dependencies [[org.clojure/clojure "1.11.4"]]}}
35+
:1.11 {:dependencies [[org.clojure/clojure "1.11.4"]]}
36+
:test {:global-vars ^:replace {*warn-on-reflection* false
37+
*unchecked-math* false}}}
3638
:deploy-repositories [["snapshots" {:url "https://clojars.org/repo/"
3739
:username [:env/clojars_username :gpg]
3840
:password [:env/clojars_password :gpg]}]]

src/cljam/io/cram/data_series.clj

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
(dotimes [i n]
106106
(let [b (bit-or (bit-shift-left (Character/digit (aget s (* 2 i)) 16) 4)
107107
(Character/digit (aget s (inc (* 2 i))) 16))]
108-
(aset arr i (byte b))))
108+
(aset arr i (unchecked-byte b))))
109109
arr))
110110
\B (fn [^ByteBuffer bb]
111111
(let [tag-type' (char (.get bb))

src/cljam/io/cram/encode/structure.clj

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
out' (CheckedOutputStream. out crc)
7575
bb (bb/allocate-lsb-byte-buffer 4)]
7676
(f out')
77-
(.putInt bb (int (.getValue crc)))
77+
(.putInt bb (unchecked-int (.getValue crc)))
7878
(lsb/write-bytes out (.array bb))))
7979

8080
(defn encode-container-header
@@ -145,7 +145,7 @@
145145
(let [r (nth all-bases i)
146146
codes (get m r)]
147147
(aset ret i
148-
(byte
148+
(unchecked-byte
149149
(loop [j 0, k 0, acc 0]
150150
(if (< j 5)
151151
(if (= i j)

src/cljam/io/util/lsb/io_stream.clj

+3-7
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,12 @@
103103
(defn write-ubyte
104104
"Writes 1 byte."
105105
[^OutputStream stream b]
106-
(let [bb (bb/allocate-lsb-byte-buffer)]
107-
(.putShort bb b)
108-
(.write stream (.array bb) 0 1)))
106+
(.write stream (unchecked-byte b)))
109107

110108
(defn write-char
111109
"Writes a 1-byte ascii character."
112110
[^OutputStream stream b]
113-
(let [bb (bb/allocate-lsb-byte-buffer)]
114-
(.putChar bb b)
115-
(.write stream (.array bb) 0 1)))
111+
(.write stream (unchecked-byte (int b))))
116112

117113
(defn write-short
118114
"Writes a 2-byte short value."
@@ -125,7 +121,7 @@
125121
"Writes a 2-byte unsigned short value."
126122
[^OutputStream stream n]
127123
(let [bb (bb/allocate-lsb-byte-buffer)]
128-
(.putInt bb n)
124+
(.putShort bb (unchecked-short n))
129125
(.write stream (.array bb) 0 2)))
130126

131127
(defn write-int

test/cljam/io/cram/data_series_test.clj

+15-15
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,15 @@
131131
:data (doto (bb/allocate-lsb-byte-buffer 8)
132132
(.putShort 0x0123)
133133
(.putShort 0x4567)
134-
(.putShort 0x89ab)
135-
(.putShort 0xcdef)
134+
(.putShort (unchecked-short 0x89ab))
135+
(.putShort (unchecked-short 0xcdef))
136136
.flip)}
137137
{:content-id 7697235
138138
:data (doto (bb/allocate-lsb-byte-buffer 8)
139139
(.putShort 0x0123)
140140
(.putShort 0x4567)
141-
(.putShort 0x89ab)
142-
(.putShort 0xcdef)
141+
(.putShort (unchecked-short 0x89ab))
142+
(.putShort (unchecked-short 0xcdef))
143143
.flip)}]
144144
decoders (ds/build-tag-decoders {:tags encodings} nil blocks)
145145
ss (get-in decoders [:ss \s])
@@ -171,15 +171,15 @@
171171
:data (doto (bb/allocate-lsb-byte-buffer 16)
172172
(.putInt 0)
173173
(.putInt 0x01234567)
174-
(.putInt 0x89abcdef)
175-
(.putInt 0xffffffff)
174+
(.putInt (unchecked-int 0x89abcdef))
175+
(.putInt (unchecked-int 0xffffffff))
176176
.flip)}
177177
{:content-id 7694665
178178
:data (doto (bb/allocate-lsb-byte-buffer 16)
179179
(.putInt 0)
180180
(.putInt 0x01234567)
181-
(.putInt 0x89abcdef)
182-
(.putInt 0xffffffff)
181+
(.putInt (unchecked-int 0x89abcdef))
182+
(.putInt (unchecked-int 0xffffffff))
183183
.flip)}]
184184
decoders (ds/build-tag-decoders {:tags encodings} nil blocks)
185185
si (get-in decoders [:si \i])
@@ -275,7 +275,7 @@
275275
[0xfc 0xfd 0xfe 0xff]]]
276276
(.put bb (byte (int \c)))
277277
(.putInt bb 4)
278-
(doseq [v vs] (.put bb (byte v))))
278+
(doseq [v vs] (.put bb (unchecked-byte v))))
279279
(.flip bb))}
280280
{:content-id 7692866
281281
:data (let [bb (bb/allocate-lsb-byte-buffer 36)]
@@ -285,7 +285,7 @@
285285
[0xfc 0xfd 0xfe 0xff]]]
286286
(.put bb (byte (int \C)))
287287
(.putInt bb 4)
288-
(doseq [v vs] (.put bb (byte v))))
288+
(doseq [v vs] (.put bb (unchecked-byte v))))
289289
(.flip bb))}]
290290
decoders (ds/build-tag-decoders {:tags encodings} nil blocks)
291291
sb (get-in decoders [:sb \B])
@@ -321,7 +321,7 @@
321321
[0xfffe 0xffff]]]
322322
(.put bb (byte (int \s)))
323323
(.putInt bb 2)
324-
(doseq [v vs] (.putShort bb v)))
324+
(doseq [v vs] (.putShort bb (unchecked-short v))))
325325
(.flip bb))}
326326
{:content-id 7697218
327327
:data (let [bb (bb/allocate-lsb-byte-buffer 36)]
@@ -331,7 +331,7 @@
331331
[0xfffe 0xffff]]]
332332
(.put bb (byte (int \S)))
333333
(.putInt bb 2)
334-
(doseq [v vs] (.putShort bb v)))
334+
(doseq [v vs] (.putShort bb (unchecked-short v))))
335335
(.flip bb))}]
336336
decoders (ds/build-tag-decoders {:tags encodings} nil blocks)
337337
ss (get-in decoders [:ss \B])
@@ -369,7 +369,7 @@
369369
[0xfffffffe 0xffffffff]]]
370370
(.put bb (byte (int \i)))
371371
(.putInt bb 2)
372-
(doseq [v vs] (.putInt bb v)))
372+
(doseq [v vs] (.putInt bb (unchecked-int v))))
373373
(.flip bb))}
374374
{:content-id 7694658
375375
:data (let [bb (bb/allocate-lsb-byte-buffer 52)]
@@ -379,7 +379,7 @@
379379
[0xfffffffe 0xffffffff]]]
380380
(.put bb (byte (int \I)))
381381
(.putInt bb 2)
382-
(doseq [v vs] (.putInt bb v)))
382+
(doseq [v vs] (.putInt bb (unchecked-int v))))
383383
(.flip bb))}]
384384
decoders (ds/build-tag-decoders {:tags encodings} nil blocks)
385385
si (get-in decoders [:si \B])
@@ -903,7 +903,7 @@
903903
(.put bb (byte (int \I)))
904904
(.putInt bb (count encoded))
905905
(run! #(.putInt bb %) encoded)
906-
(.put bb (byte 0xff))
906+
(.put bb (unchecked-byte 0xff))
907907
(.array bb)))
908908
vs)
909909
(seq (decompress res)))))))

test/cljam/io/cram/encode/structure_test.clj

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
(let [crc (CRC32.)]
2222
(.update crc bs start end)
2323
(-> (bb/allocate-lsb-byte-buffer 4)
24-
(.putInt (.getValue crc))
24+
(.putInt (unchecked-int (.getValue crc)))
2525
.array)))
2626

2727
(deftest encode-file-definition-test

test/cljam/io/util/byte_buffer_test.clj

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
(let [bb (doto (bb/allocate-lsb-byte-buffer 8)
1515
(.putLong 0x789ABCDEF0123456)
1616
(.flip))]
17-
(is (= (int 0xF0123456) (.getInt bb)))
17+
(is (= (unchecked-int 0xF0123456) (.getInt bb)))
1818
(is (= (int 0x789ABCDE) (.getInt bb)))))
1919

2020
(deftest allocate-msb-byte-buffer-test
2121
(let [bb (doto (bb/allocate-msb-byte-buffer 8)
2222
(.putLong 0x789ABCDEF0123456)
2323
(.flip))]
2424
(is (= (int 0x789ABCDE) (.getInt bb)))
25-
(is (= (int 0xF0123456) (.getInt bb)))))
25+
(is (= (unchecked-int 0xF0123456) (.getInt bb)))))
2626

2727
(deftest read-ops-test
2828
(let [bb (doto (bb/allocate-lsb-byte-buffer 8)

test/cljam/io/util/lsb/io_stream_test.clj

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@
133133
(is (= (seq ba) (seq ret)))))
134134

135135
(with-open [baos (ByteArrayOutputStream. 8)]
136-
(lsb/write-int baos 0xF0123456)
136+
(lsb/write-int baos (unchecked-int 0xF0123456))
137137
(lsb/write-int baos 0x789ABCDE)
138138
(let [ret (.toByteArray baos)]
139139
(is (= (count ba) (count ret)))

0 commit comments

Comments
 (0)