File tree Expand file tree Collapse file tree 2 files changed +20
-1
lines changed Expand file tree Collapse file tree 2 files changed +20
-1
lines changed Original file line number Diff line number Diff line change 895
895
(p/int->uint (.getInt b (p/+ idx b-offset))))]
896
896
(if (p/== 0 cmp)
897
897
(recur (p/+ idx 4 ))
898
- (p/* sign cmp)))))]
898
+ ; ; Use (if (pos? cmp) 1 -1) to ensure that the
899
+ ; ; sign of the value x returned by cmp-bufs (and
900
+ ; ; compare-bytes) is not modified when Clojure's
901
+ ; ; comparator infrastructure calls (.intValue
902
+ ; ; x). The intValue method truncates a Java
903
+ ; ; Long's most significant 32 bits away, which
904
+ ; ; in some cases changes the sign of the result,
905
+ ; ; and thus the direction of the comparison
906
+ ; ; result. Such code is not needed when
907
+ ; ; comparing individual bytes below, because the
908
+ ; ; subtraction result fits within the least
909
+ ; ; significant 9 bits, and (.intValue x) never
910
+ ; ; changes the sign.
911
+ (p/* sign (if (pos? cmp) 1 -1 ))))))]
899
912
(if (p/== 0 (long cmp))
900
913
(let [limit' (.remaining a)]
901
914
(loop [idx limit]
Original file line number Diff line number Diff line change 138
138
(is (bytes= text-bytes (-> text-bytes to-string to-byte-array)))
139
139
(is (bytes= text-bytes (-> text-bytes to-input-stream to-string to-byte-array)))
140
140
(is (bytes= text-bytes (-> text-bytes (to-input-stream {:chunk-size 128 }) to-string to-byte-array)))))
141
+
142
+ (deftest compare-bytes-former-bug
143
+ (let [bx (convert (byte-array [0x00 0x00 0x00 0x01 ]) java.nio.ByteBuffer)
144
+ by (convert (byte-array [0x80 0x00 0x00 0x01 ]) java.nio.ByteBuffer)]
145
+ (is (= [bx by] (sort compare-bytes [bx by])))
146
+ (is (= [bx by] (sort compare-bytes [by bx])))))
You can’t perform that action at this time.
0 commit comments