Skip to content

Commit 25b33cc

Browse files
authored
Merge pull request #226 from mseri/master
xcp-idl: port to safe-strings without changing the interface
2 parents 0600afa + 828d781 commit 25b33cc

File tree

7 files changed

+38
-33
lines changed

7 files changed

+38
-33
lines changed

lib/cohttp_posix_io.ml

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ module Unbuffered_IO = struct
4444
let buf = Buffer.create 128 in
4545
(* We can safely read everything up to this marker: *)
4646
let end_of_headers = "\r\n\r\n" in
47-
let tmp = String.make (String.length end_of_headers) '\000' in
47+
let tmp = Bytes.make (String.length end_of_headers) '\000' in
4848
let module Scanner = struct
4949
type t = {
5050
marker: string;
5151
mutable i: int;
5252
}
5353
let make x = { marker = x; i = 0 }
5454
let input x c =
55-
if c = x.marker.[x.i] then x.i <- x.i + 1 else x.i <- 0
55+
if c = String.get x.marker x.i then x.i <- x.i + 1 else x.i <- 0
5656
let remaining x = String.length x.marker - x.i
5757
let matched x = x.i = String.length x.marker
5858
end in
@@ -68,8 +68,8 @@ module Unbuffered_IO = struct
6868
if n = 0 then raise End_of_file;
6969

7070
for j = 0 to n - 1 do
71-
Scanner.input marker tmp.[j];
72-
Buffer.add_char buf tmp.[j]
71+
Scanner.input marker (Bytes.get tmp j);
72+
Buffer.add_char buf (Bytes.get tmp j)
7373
done;
7474
done;
7575
Buffer.contents buf
@@ -110,13 +110,15 @@ module Unbuffered_IO = struct
110110
| false -> return None
111111

112112
let read ic n =
113-
let buf = String.make n '\000' in
113+
let buf = Bytes.make n '\000' in
114114
let actually_read = Unix.read ic.fd buf 0 n in
115115
if actually_read = n
116-
then buf
117-
else String.sub buf 0 actually_read
116+
then Bytes.unsafe_to_string buf
117+
else Bytes.sub_string buf 0 actually_read
118118

119-
let write oc x = ignore(Unix.write oc x 0 (String.length x))
119+
let write oc x =
120+
Unix.write oc (Bytes.unsafe_of_string x) 0 (String.length x)
121+
|> ignore
120122

121123
let flush _oc = ()
122124
end
@@ -152,11 +154,11 @@ module Buffered_IO = struct
152154
| false -> return None
153155

154156
let read ic n =
155-
let buf = String.make n '\000' in
157+
let buf = Bytes.make n '\000' in
156158
let actually_read = input ic buf 0 n in
157159
if actually_read = n
158-
then buf
159-
else String.sub buf 0 actually_read
160+
then Bytes.unsafe_to_string buf
161+
else Bytes.sub_string buf 0 actually_read
160162

161163
let write oc x = output_string oc x; flush oc
162164

lib/posix_channel.ml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ exception Channel_setup_failed
66
module CBuf = struct
77
(** A circular buffer constructed from a string *)
88
type t = {
9-
mutable buffer: string;
9+
mutable buffer: bytes;
1010
mutable len: int; (** bytes of valid data in [buffer] *)
1111
mutable start: int; (** index of first valid byte in [buffer] *)
1212
mutable r_closed: bool; (** true if no more data can be read due to EOF *)
@@ -23,11 +23,11 @@ module CBuf = struct
2323

2424
let drop (x: t) n =
2525
if n > x.len then failwith (Printf.sprintf "drop %d > %d" n x.len);
26-
x.start <- (x.start + n) mod (String.length x.buffer);
26+
x.start <- (x.start + n) mod (Bytes.length x.buffer);
2727
x.len <- x.len - n
2828

2929
let should_read (x: t) =
30-
not x.r_closed && (x.len < (String.length x.buffer - 1))
30+
not x.r_closed && (x.len < (Bytes.length x.buffer - 1))
3131
let should_write (x: t) =
3232
not x.w_closed && (x.len > 0)
3333

@@ -36,18 +36,18 @@ module CBuf = struct
3636

3737
let write (x: t) fd =
3838
(* Offset of the character after the substring *)
39-
let next = min (String.length x.buffer) (x.start + x.len) in
39+
let next = min (Bytes.length x.buffer) (x.start + x.len) in
4040
let len = next - x.start in
4141
let written = try Unix.single_write fd x.buffer x.start len with _e -> x.w_closed <- true; len in
4242
drop x written
4343

4444
let read (x: t) fd =
4545
(* Offset of the next empty character *)
46-
let next = (x.start + x.len) mod (String.length x.buffer) in
47-
let len = min (String.length x.buffer - next) (String.length x.buffer - x.len) in
46+
let next = (x.start + x.len) mod (Bytes.length x.buffer) in
47+
let len = min (Bytes.length x.buffer - next) (Bytes.length x.buffer - x.len) in
4848
let read = Unix.read fd x.buffer next len in
4949
if read = 0 then x.r_closed <- true;
50-
x.len <- x.len + read
50+
x.len <- x.len + read
5151
end
5252

5353
let proxy (a: Unix.file_descr) (b: Unix.file_descr) =
@@ -145,9 +145,9 @@ let send proxy_socket =
145145
if List.mem s_unix readable then begin
146146
let fd, _peer = Unix.accept s_unix in
147147
to_close := fd :: !to_close;
148-
let buffer = String.make (String.length token) '\000' in
149-
let n = Unix.recv fd buffer 0 (String.length buffer) [] in
150-
let token' = String.sub buffer 0 n in
148+
let buffer = Bytes.make (String.length token) '\000' in
149+
let n = Unix.recv fd buffer 0 (Bytes.length buffer) [] in
150+
let token' = Bytes.sub_string buffer 0 n in
151151
if token = token' then begin
152152
let (_: int) = Fd_send_recv.send_fd fd token 0 (String.length token) [] proxy_socket in
153153
()
@@ -203,7 +203,7 @@ let receive protocols =
203203
finally
204204
(fun () ->
205205
Unix.connect s (Unix.ADDR_UNIX path);
206-
let (_: int) = Unix.send s token 0 (String.length token) [] in
206+
let (_: int) = Unix.send s (Bytes.unsafe_of_string token) 0 (String.length token) [] in
207207
let (_, _, fd) = Fd_send_recv.recv_fd s token 0 (String.length token) [] in
208208
fd
209209
) (fun () -> Unix.close s)

lib/scheduler.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ module Delay = struct
9191
mutex_execute x.m
9292
(fun () ->
9393
match x.pipe_in with
94-
| Some fd -> ignore(Unix.write fd "X" 0 1)
94+
| Some fd -> ignore(Unix.write fd (Bytes.of_string "X") 0 1)
9595
| None -> x.signalled <- true (* If the wait hasn't happened yet then store up the signal *)
9696
)
9797
end

lib/xcp_client.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,12 @@ let binary_rpc string_of_call response_of_string ?(srcstr="unset") ?(dststr="uns
112112
output_string oc len;
113113
output_string oc msg_buf;
114114
flush oc;
115-
let len_buf = String.make 16 '\000' in
115+
let len_buf = Bytes.make 16 '\000' in
116116
really_input ic len_buf 0 16;
117-
let len = int_of_string len_buf in
118-
let msg_buf = String.make len '\000' in
117+
let len = int_of_string (Bytes.unsafe_to_string len_buf) in
118+
let msg_buf = Bytes.make len '\000' in
119119
really_input ic msg_buf 0 len;
120-
let (response: Rpc.response) = response_of_string msg_buf in
120+
let (response: Rpc.response) = response_of_string (Bytes.unsafe_to_string msg_buf) in
121121
response
122122
)
123123

lib/xcp_service.ml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,9 @@ let http_handler call_of_string string_of_response process s =
389389
debug "Failed to read content-length"
390390
| Some content_length ->
391391
let content_length = int_of_string content_length in
392-
let request_txt = String.make content_length '\000' in
392+
let request_txt = Bytes.make content_length '\000' in
393393
really_input ic request_txt 0 content_length;
394-
let rpc_call = call_of_string request_txt in
394+
let rpc_call = call_of_string (Bytes.unsafe_to_string request_txt) in
395395
debug "%s" (Rpc.string_of_call rpc_call);
396396
let rpc_response = process rpc_call in
397397
debug " %s" (Rpc.string_of_response rpc_response);
@@ -493,8 +493,11 @@ let pidfile_write filename =
493493
finally
494494
(fun () ->
495495
let pid = Unix.getpid () in
496-
let buf = string_of_int pid ^ "\n" in
497-
let len = String.length buf in
496+
let buf =
497+
string_of_int pid ^ "\n"
498+
|> Bytes.of_string
499+
in
500+
let len = Bytes.length buf in
498501
if Unix.write fd buf 0 len <> len
499502
then failwith "pidfile_write failed")
500503
(fun () -> Unix.close fd)

lib_test/idl_test_common.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ let read_str filename =
2323
let s = Bytes.create n in
2424
really_input ic s 0 n;
2525
close_in ic;
26-
s
26+
Bytes.unsafe_to_string s
2727

2828
open Idl
2929

network/network_stats.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ module File_helpers = struct
9090
let rec fold acc =
9191
let n = Unix.read fd block 0 block_size in
9292
(* Consider making the interface explicitly use Substrings *)
93-
let s = if n = block_size then block else String.sub block 0 n in
93+
let s = if n = block_size then (Bytes.to_string block) else Bytes.sub_string block 0 n in
9494
if n = 0 then acc else fold (f acc s) in
9595
fold start
9696

0 commit comments

Comments
 (0)