Skip to content

Commit b9788fa

Browse files
committed
Added -safe-string compilation flag
1 parent d8d0f62 commit b9788fa

7 files changed

+53
-21
lines changed

_tags

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,5 @@
6565
true: -traverse
6666
<{lib,examples}/**>: traverse
6767

68-
<**/*.ml{,i}>: warn(Aer-44), strict_sequence, annot
68+
<**/*.ml{,i}>: warn(Aer-44), strict_sequence, safe_string, annot
6969
"lib/postgresql.cmxs": use_libpostgresql_stubs

lib/postgresql.ml

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2525
*)
2626

27+
open Postgresql_compat
2728
open Printf
2829

2930
type oid = int
@@ -447,10 +448,10 @@ module Stub = struct
447448
(* Functions Associated with the COPY Command *)
448449

449450
external getline :
450-
connection -> string -> int -> int -> int = "PQgetline_stub"
451+
connection -> Bytes.t -> int -> int -> int = "PQgetline_stub"
451452

452453
external getline_async :
453-
connection -> string -> int -> int -> int = "PQgetlineAsync_stub" "noalloc"
454+
connection -> Bytes.t -> int -> int -> int = "PQgetlineAsync_stub" "noalloc"
454455

455456
external putline : connection -> string -> int = "PQputline_stub"
456457

@@ -485,7 +486,7 @@ module Stub = struct
485486
external lo_unlink : connection -> oid -> oid = "lo_unlink_stub"
486487

487488
external lo_read :
488-
connection -> large_object -> string -> int -> int -> int = "lo_read_stub"
489+
connection -> large_object -> Bytes.t -> int -> int -> int = "lo_read_stub"
489490

490491
external lo_read_ba :
491492
connection -> large_object ->
@@ -856,7 +857,7 @@ object (self)
856857
(* Low level *)
857858

858859
method getline ?(pos = 0) ?len buf =
859-
let buf_len = String.length buf in
860+
let buf_len = Bytes.length buf in
860861
let len = match len with Some len -> len | None -> buf_len - pos in
861862
if len < 0 || pos < 0 || pos + len > buf_len then
862863
invalid_arg "Postgresql.connection#getline";
@@ -868,7 +869,7 @@ object (self)
868869
| _ -> assert false)
869870

870871
method getline_async ?(pos = 0) ?len buf =
871-
let buf_len = String.length buf in
872+
let buf_len = Bytes.length buf in
872873
let len = match len with Some len -> len | None -> buf_len - pos in
873874
if len < 0 || pos < 0 || pos + len > buf_len then
874875
invalid_arg "Postgresql.connection#getline_async";
@@ -877,7 +878,7 @@ object (self)
877878
| -1 -> if Stub.endcopy conn <> 0 then signal_error conn else EndOfData
878879
| 0 -> NoData
879880
| n when n > 0 ->
880-
if buf.[pos + n - 1] = '\n' then DataRead n else PartDataRead n
881+
if Bytes.get buf (pos + n - 1) = '\n' then DataRead n else PartDataRead n
881882
| _ -> assert false)
882883

883884
method putline buf =
@@ -900,17 +901,17 @@ object (self)
900901
method copy_out f =
901902
let buf = Buffer.create 1024 in
902903
let len = 512 in
903-
let s = String.create len in
904+
let bts = Bytes.create len in
904905
wrap_conn (fun conn ->
905906
let rec loop () =
906-
let r = Stub.getline conn s 0 len in
907+
let r = Stub.getline conn bts 0 len in
907908
if r = 1 then begin (* Buffer full *)
908-
Buffer.add_substring buf s 0 len;
909+
buffer_add_subbytes buf bts 0 len;
909910
loop ()
910911
end
911912
else if r = 0 then (* Line read *)
912-
let zero = String.index s '\000' in
913-
Buffer.add_substring buf s 0 zero;
913+
let zero = Bytes.index bts '\000' in
914+
buffer_add_subbytes buf bts 0 zero;
914915
match Buffer.contents buf with
915916
| "\\." -> ()
916917
| line -> Buffer.clear buf; f line; loop ()
@@ -1000,9 +1001,9 @@ object (self)
10001001
if w < len then signal_error conn)
10011002

10021003
method lo_read lo ?(pos = 0) ?len buf =
1003-
let buf_len = String.length buf in
1004+
let buf_len = Bytes.length buf in
10041005
let len = match len with Some len -> len | None -> buf_len - pos in
1005-
if len < 0 || pos < 0 || pos + len > String.length buf then
1006+
if len < 0 || pos < 0 || pos + len > buf_len then
10061007
invalid_arg "Postgresql.connection#lo_read";
10071008
wrap_conn (fun conn ->
10081009
let read = Stub.lo_read conn lo buf pos len in

lib/postgresql.mli

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2525
*)
2626

27+
open Postgresql_compat
28+
2729
(** Client-interface to the PostgreSQL database. *)
2830

2931
(** Please learn about more details in the database documentation! *)
@@ -669,30 +671,30 @@ object
669671

670672
(** Low level *)
671673

672-
method getline : ?pos : int -> ?len : int -> string -> getline_result
674+
method getline : ?pos : int -> ?len : int -> Bytes.t -> getline_result
673675
(** [getline ?pos ?len buf] reads a newline-terminated line of at most
674676
[len] characters into [buf] starting at position [pos].
675677
676678
@return getline_result
677679
678680
@param pos default = 0
679-
@param len default = String.length buf - pos
681+
@param len default = Bytes.length buf - pos
680682
681683
@raise Invalid_argument if the buffer parameters are invalid.
682684
@raise Error if there is a connection error.
683685
*)
684686

685687
method getline_async :
686-
?pos : int -> ?len : int -> string -> getline_async_result
688+
?pos : int -> ?len : int -> Bytes.t -> getline_async_result
687689
(** [getline_async ?pos ?len buf] reads a newline-terminated line of
688690
at most [len] characters into [buf] starting at position [pos]
689-
(asynchronously). No need to call [endcopy] after receiving
691+
(asynchronously). No need to call [endcopy] after receiving
690692
[EndOfData].
691693
692694
@return getline_async_result
693695
694696
@param pos default = 0
695-
@param len default = String.length buf - pos
697+
@param len default = Bytes.length buf - pos
696698
697699
@raise Invalid_argument if the buffer parameters are invalid.
698700
@raise Error if there is a connection error.
@@ -865,12 +867,12 @@ object
865867
large_object -> unit
866868
(** As [lo_write], but performs a zero-copy write from the given Bigarray. *)
867869

868-
method lo_read : large_object -> ?pos : int -> ?len : int -> string -> int
870+
method lo_read : large_object -> ?pos : int -> ?len : int -> Bytes.t -> int
869871
(** [lo_read lo ?pos ?len buf] reads [len] bytes from large object [lo]
870872
to buffer [buf] starting at position [pos].
871873
872874
@param pos default = 0
873-
@param len default = String.length buf - pos
875+
@param len default = Bytes.length buf - pos
874876
875877
@raise Invalid_argument if the buffer parameters are invalid.
876878
@raise Error if [len] bytes could not be read.

lib/postgresql_compat.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module Bytes = Bytes
2+
3+
let buffer_add_subbytes = Buffer.add_subbytes

lib/postgresql_compat312.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module Bytes = String
2+
3+
let buffer_add_subbytes = Buffer.add_substring

lib/postgresql_compat402.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module Bytes = Bytes
2+
3+
let buffer_add_subbytes = Buffer.add_subbytes

setup.ml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7075,4 +7075,24 @@ let setup () = BaseSetup.setup setup_t;;
70757075

70767076
# 7077 "setup.ml"
70777077
(* OASIS_STOP *)
7078+
7079+
let () =
7080+
let ocaml_major, ocaml_minor =
7081+
Scanf.sscanf Sys.ocaml_version "%i.%i" (fun v1 v2 -> v1, v2) in
7082+
let cp_file src dst =
7083+
let ic = open_in src in
7084+
let oc = open_out dst in
7085+
try while true do output_string oc (input_line ic ^ "\n") done
7086+
with End_of_file ->
7087+
close_out oc;
7088+
close_in ic
7089+
in
7090+
let src =
7091+
if ocaml_major > 4 || (ocaml_major = 4 && ocaml_minor >= 2) then
7092+
"lib/postgresql_compat402.ml"
7093+
else
7094+
"lib/postgresql_compat312.ml"
7095+
in
7096+
cp_file src "lib/postgresql_compat.ml"
7097+
70787098
let () = setup ();;

0 commit comments

Comments
 (0)