From 8ba1a23b1800ffd55bd85260f6f8702e6aa2e54a Mon Sep 17 00:00:00 2001 From: Anil Madhavapeddy Date: Wed, 27 May 2026 11:09:51 +0100 Subject: [PATCH] Return Error from of_string on unclosed character class Dont call Fmt.failwith on unclosed brackets, since the overall function returns an `error. --- src/glob.ml | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/glob.ml b/src/glob.ml index 3eb6326..7bd75a8 100644 --- a/src/glob.ml +++ b/src/glob.ml @@ -159,15 +159,31 @@ let of_string ?(dir_sep = default_dir_sep) s = let cs = char_class_of_chars cs in (cs, idx) in - let components = normal [] 0 in - Ok { dir_sep; components } + match normal [] 0 with + | components -> Ok { dir_sep; components } + | exception Failure msg -> Error msg let to_string t = let buf = Buffer.create 16 in + let add_literal_string s = + String.iter + (fun c -> + match c with + | '*' | '?' | '[' -> + Buffer.add_char buf '['; + Buffer.add_char buf c; + Buffer.add_char buf ']' + | c when Char.equal c t.dir_sep -> + Buffer.add_char buf '['; + Buffer.add_char buf c; + Buffer.add_char buf ']' + | c -> Buffer.add_char buf c) + s + in List.iter (function | Dir_sep -> Buffer.add_char buf t.dir_sep - | String s -> Buffer.add_string buf s + | String s -> add_literal_string s | Question -> Buffer.add_char buf '?' | Star -> Buffer.add_char buf '*' | Range cs -> -- 2.51.2