From 657b34244ac5492ea60c5097a7d6641d733fb0e7 Mon Sep 17 00:00:00 2001 From: Fabrice Le Fessant Date: Wed, 24 Aug 2016 15:14:13 +0200 Subject: [PATCH] Check that the argument to Digest.to_hex has the correct length (#763) * Check that the argument to Digest.to_hex has the correct length and improve documentation for Digest.to_hex --- stdlib/digest.ml | 3 ++- stdlib/digest.mli | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/stdlib/digest.ml b/stdlib/digest.ml index 9ad393cf1..408194b50 100644 --- a/stdlib/digest.ml +++ b/stdlib/digest.ml @@ -50,6 +50,7 @@ let char_hex n = Char.unsafe_chr (n + if n < 10 then Char.code '0' else (Char.code 'a' - 10)) let to_hex d = + if String.length d <> 16 then invalid_arg "Digest.to_hex"; let result = Bytes.create 32 in for i = 0 to 15 do let x = Char.code d.[i] in @@ -59,7 +60,7 @@ let to_hex d = Bytes.unsafe_to_string result let from_hex s = - if String.length s <> 32 then raise (Invalid_argument "Digest.from_hex"); + if String.length s <> 32 then invalid_arg "Digest.from_hex"; let digit c = match c with | '0'..'9' -> Char.code c - Char.code '0' diff --git a/stdlib/digest.mli b/stdlib/digest.mli index c5e91f50e..2c9bebc52 100644 --- a/stdlib/digest.mli +++ b/stdlib/digest.mli @@ -73,7 +73,9 @@ val input : in_channel -> t (** Read a digest from the given input channel. *) val to_hex : t -> string -(** Return the printable hexadecimal representation of the given digest. *) +(** Return the printable hexadecimal representation of the given digest. + Raise [Invalid_argument] if the argument is not exactly 16 bytes. + *) val from_hex : string -> t (** Convert a hexadecimal representation back into the corresponding digest.