Skip to content

Commit

Permalink
Fix sign bug in read_i32 on 64-bit ocaml
Browse files Browse the repository at this point in the history
  • Loading branch information
serp256 authored and thelema committed Feb 16, 2010
1 parent 9693d99 commit dac9f98
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/batIO.ml
Original file line number Diff line number Diff line change
Expand Up @@ -200,15 +200,17 @@ let read_i16 i =
else
n

let fix = lnot 0x7FFFFFFF (* -:) *)

let read_i32 ch =
let ch4 = read_byte ch in
let ch3 = read_byte ch in
let ch2 = read_byte ch in
let ch1 = read_byte ch in
if ch4 land 128 <> 0 then begin
if ch4 land 128 <> 0 then begin (* negative number *)
if ch4 land 64 = 0 then raise (Overflow "read_i32");
ch1 lor (ch2 lsl 8) lor (ch3 lsl 16) lor ((ch4 land 127) lsl 24)
end else begin
(ch1 lor (ch2 lsl 8) lor (ch3 lsl 16) lor ((ch4 land 127) lsl 24)) lor fix (* FIX HERE *)
end else begin (*positive number*)
if ch4 land 64 <> 0 then raise (Overflow "read_i32");
ch1 lor (ch2 lsl 8) lor (ch3 lsl 16) lor (ch4 lsl 24)
end
Expand Down
4 changes: 3 additions & 1 deletion src/batInnerIO.ml
Original file line number Diff line number Diff line change
Expand Up @@ -519,14 +519,16 @@ let read_i16 i =
else
n

let fix = lnot 0x7FFFFFFF (* -:) *)

let read_i32 ch =
let ch1 = read_byte ch in
let ch2 = read_byte ch in
let ch3 = read_byte ch in
let ch4 = read_byte ch in
if ch4 land 128 <> 0 then begin
if ch4 land 64 = 0 then raise (Overflow "read_i32");
ch1 lor (ch2 lsl 8) lor (ch3 lsl 16) lor ((ch4 land 127) lsl 24)
(ch1 lor (ch2 lsl 8) lor (ch3 lsl 16) lor ((ch4 land 127) lsl 24)) lor fix (* FIX HERE *)
end else begin
if ch4 land 64 <> 0 then raise (Overflow "read_i32");
ch1 lor (ch2 lsl 8) lor (ch3 lsl 16) lor (ch4 lsl 24)
Expand Down

0 comments on commit dac9f98

Please sign in to comment.