Skip to content

Commit 657b342

Browse files
authored
Check that the argument to Digest.to_hex has the correct length (ocaml#763)
* Check that the argument to Digest.to_hex has the correct length and improve documentation for Digest.to_hex
1 parent c0aeb56 commit 657b342

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

stdlib/digest.ml

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ let char_hex n =
5050
Char.unsafe_chr (n + if n < 10 then Char.code '0' else (Char.code 'a' - 10))
5151

5252
let to_hex d =
53+
if String.length d <> 16 then invalid_arg "Digest.to_hex";
5354
let result = Bytes.create 32 in
5455
for i = 0 to 15 do
5556
let x = Char.code d.[i] in
@@ -59,7 +60,7 @@ let to_hex d =
5960
Bytes.unsafe_to_string result
6061

6162
let from_hex s =
62-
if String.length s <> 32 then raise (Invalid_argument "Digest.from_hex");
63+
if String.length s <> 32 then invalid_arg "Digest.from_hex";
6364
let digit c =
6465
match c with
6566
| '0'..'9' -> Char.code c - Char.code '0'

stdlib/digest.mli

+3-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ val input : in_channel -> t
7373
(** Read a digest from the given input channel. *)
7474

7575
val to_hex : t -> string
76-
(** Return the printable hexadecimal representation of the given digest. *)
76+
(** Return the printable hexadecimal representation of the given digest.
77+
Raise [Invalid_argument] if the argument is not exactly 16 bytes.
78+
*)
7779

7880
val from_hex : string -> t
7981
(** Convert a hexadecimal representation back into the corresponding digest.

0 commit comments

Comments
 (0)