Skip to content

Commit

Permalink
Merge pull request #35 from cfcs/safe-string
Browse files Browse the repository at this point in the history
compile with -safe_string to make it ready for ocaml 4.06
  • Loading branch information
djs55 authored Dec 3, 2017
2 parents c974568 + 38eab12 commit dfa2a23
Show file tree
Hide file tree
Showing 16 changed files with 46 additions and 42 deletions.
1 change: 1 addition & 0 deletions client_lwt/jbuild
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
(public_name xenstore.client)
(wrapped false)
(libraries (lwt xenstore))
(flags (:standard -safe-string))
))
2 changes: 1 addition & 1 deletion client_lwt/xs_client_lwt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module type IO = sig
type channel
val create: unit -> channel t
val destroy: channel -> unit t
val read: channel -> string -> int -> int -> int t
val read: channel -> bytes -> int -> int -> int t
val write: channel -> string -> int -> int -> unit t
end

Expand Down
2 changes: 1 addition & 1 deletion client_lwt/xs_client_lwt.mli
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module type IO = sig
type channel
val create: unit -> channel t
val destroy: channel -> unit t
val read: channel -> string -> int -> int -> int t
val read: channel -> bytes -> int -> int -> int t
val write: channel -> string -> int -> int -> unit t
end

Expand Down
1 change: 1 addition & 0 deletions client_unix/jbuild
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
(public_name xenstore.unix)
(wrapped false)
(libraries (unix threads xenstore))
(flags (:standard -safe-string))
))
2 changes: 1 addition & 1 deletion client_unix/xs_client_unix.ml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module type IO = sig
type channel
val create: unit -> channel t
val destroy: channel -> unit t
val read: channel -> string -> int -> int -> int t
val read: channel -> bytes -> int -> int -> int t
val write: channel -> string -> int -> int -> unit t
end

Expand Down
2 changes: 1 addition & 1 deletion client_unix/xs_client_unix.mli
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module type IO = sig
type channel
val create: unit -> channel t
val destroy: channel -> unit t
val read: channel -> string -> int -> int -> int t
val read: channel -> bytes -> int -> int -> int t
val write: channel -> string -> int -> int -> unit t
end

Expand Down
1 change: 1 addition & 0 deletions core/jbuild
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
(wrapped false)
(libraries (cstruct))
(preprocess (pps (cstruct.ppx)))
(flags (:standard -safe-string))
))

38 changes: 19 additions & 19 deletions core/xs_protocol.ml
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,11 @@ module Parser = struct
| Packet of t

type parse =
| ReadingHeader of int * string
| ReadingHeader of int * bytes
| ReadingBody of t
| Finished of state

let start () = ReadingHeader (0, String.make header_size '\000')
let start () = ReadingHeader (0, Bytes.make header_size '\000')

let state = function
| ReadingHeader(got_already, _) -> Need_more_data (header_size - got_already)
Expand Down Expand Up @@ -224,21 +224,21 @@ module Parser = struct
| None -> Finished (Unknown_operation ty)
end

let input state bytes =
let input state (bytes : string) =
match state with
| ReadingHeader(got_already, str) ->
String.blit bytes 0 str got_already (String.length bytes);
let got_already = got_already + (String.length bytes) in
if got_already < header_size
then ReadingHeader(got_already, str)
else parse_header str
| ReadingBody x ->
Buffer.add_string x.data bytes;
let needed = x.len - (Buffer.length x.data) in
if needed > 0
then ReadingBody x
else Finished (Packet x)
| Finished f -> Finished f
| ReadingHeader(got_already, (str : bytes)) ->
Bytes.blit_string bytes 0 str got_already (String.length bytes);
let got_already = got_already + (String.length bytes) in
if got_already < header_size
then ReadingHeader(got_already, str)
else parse_header (Bytes.to_string str)
| ReadingBody x ->
Buffer.add_string x.data bytes;
let needed = x.len - (Buffer.length x.data) in
if needed > 0
then ReadingBody x
else Finished (Packet x)
| Finished f -> Finished f
end

(* Should we switch to an explicit stream abstraction here? *)
Expand All @@ -248,7 +248,7 @@ module type IO = sig
val ( >>= ): 'a t -> ('a -> 'b t) -> 'b t

type channel
val read: channel -> string -> int -> int -> int t
val read: channel -> bytes -> int -> int -> int t
val write: channel -> string -> int -> int -> unit t
end

Expand Down Expand Up @@ -281,12 +281,12 @@ module PacketStream = functor(IO: IO) -> struct
t.incoming_pkt <- start ();
return (Ok pkt)
| Need_more_data x ->
let buf = String.make x '\000' in
let buf = Bytes.make x '\000' in
IO.read t.channel buf 0 x
>>= (function
| 0 -> return (Exception EOF)
| n ->
let fragment = String.sub buf 0 n in
let fragment = Bytes.sub_string buf 0 n in
t.incoming_pkt <- input t.incoming_pkt fragment;
recv t)
| Unknown_operation x -> return (Exception (Unknown_xenstore_operation x))
Expand Down
2 changes: 1 addition & 1 deletion core/xs_protocol.mli
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ module type IO = sig
val ( >>= ): 'a t -> ('a -> 'b t) -> 'b t

type channel
val read: channel -> string -> int -> int -> int t
val read: channel -> bytes -> int -> int -> int t
val write: channel -> string -> int -> int -> unit t
end

Expand Down
1 change: 1 addition & 0 deletions core_test/jbuild
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(executables
((names (xs_test))
(libraries (lwt lwt.unix xenstore oUnit))
(flags (:standard -safe-string))
))
(alias
((name runtest)
Expand Down
1 change: 1 addition & 0 deletions server/jbuild
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
((name xenstore_server)
(public_name xenstore.server)
(libraries (lwt xenstore))
(flags (:standard -safe-string))
))
18 changes: 9 additions & 9 deletions server/junk.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type ('a, 'b) either = Right of 'a | Left of 'b
(** apply the clean_f function after fct function has been called.
* Even if fct raises an exception, clean_f is applied
*)
let exnhook = ref None
let exnhook = ref None

let finally' fct clean_f =
let result = try
Expand Down Expand Up @@ -89,23 +89,23 @@ let list_tl_multi n l =

let hexify s =
let hexseq_of_char c = Printf.sprintf "%02x" (Char.code c) in
let hs = String.create (String.length s * 2) in
let hs = Bytes.create (String.length s * 2) in
for i = 0 to String.length s - 1
do
let seq = hexseq_of_char s.[i] in
hs.[i * 2] <- seq.[0];
hs.[i * 2 + 1] <- seq.[1];
Bytes.set hs (i * 2) seq.[0];
Bytes.set hs (i * 2 + 1) seq.[1];
done;
hs
Bytes.to_string hs

let unhexify hs =
let char_of_hexseq seq0 seq1 = Char.chr (int_of_string (Printf.sprintf "0x%c%c" seq0 seq1)) in
let s = String.create (String.length hs / 2) in
for i = 0 to String.length s - 1
let s = Bytes.create (String.length hs / 2) in
for i = 0 to Bytes.length s - 1
do
s.[i] <- char_of_hexseq hs.[i * 2] hs.[i * 2 + 1]
Bytes.set s i @@ char_of_hexseq hs.[i * 2] hs.[i * 2 + 1]
done;
s
Bytes.to_string s

let trim_path path =
try
Expand Down
10 changes: 5 additions & 5 deletions server/logging.ml
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,13 @@ let access_type_disabled = function
let access_type_enabled x = not(access_type_disabled x)

let sanitize_data data =
let data = String.copy data in
for i = 0 to String.length data - 1
let data = Bytes.of_string data in
for i = 0 to Bytes.length data - 1
do
if data.[i] = '\000' then
data.[i] <- ' '
if Bytes.get data i = '\000' then
Bytes.set data i ' '
done;
String.escaped data
String.escaped (Bytes.to_string data)

let access_logging ~con ~tid ?(data="") access_type =
if access_type_enabled access_type then begin
Expand Down
4 changes: 1 addition & 3 deletions server/quota.ml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
* GNU Lesser General Public License for more details.
*)

let debug fmt = Logging.debug "quota" fmt
let info fmt = Logging.info "quota" fmt
let warn fmt = Logging.warn "quota" fmt

exception Limit_reached
Expand Down Expand Up @@ -67,7 +65,7 @@ let create () =

let copy quota = { cur = (Hashtbl.copy quota.cur) }

let del quota id = Hashtbl.remove quota.cur id
(*let del quota id = Hashtbl.remove quota.cur id*)

let check quota id size =
if size > !maxsize then (
Expand Down
2 changes: 1 addition & 1 deletion server/xs_server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module type TRANSPORT = sig
val listen: unit -> server Lwt.t

type channel
val read: channel -> string -> int -> int -> int Lwt.t
val read: channel -> bytes -> int -> int -> int Lwt.t
val write: channel -> string -> int -> int -> unit Lwt.t
val destroy: channel -> unit Lwt.t
val address_of: channel -> Xs_protocol.address Lwt.t
Expand Down
1 change: 1 addition & 0 deletions server_test/jbuild
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(executables
((names (server_test))
(libraries (lwt lwt.unix xenstore xenstore.server oUnit))
(flags (:standard -safe-string))
))
(alias
((name runtest)
Expand Down

0 comments on commit dfa2a23

Please sign in to comment.