From d38ca95f90ba9204f7061276a5d86106b002ce90 Mon Sep 17 00:00:00 2001 From: Romain Beauxis Date: Sun, 12 Feb 2023 10:17:19 -0600 Subject: [PATCH] Fix crash in `string.interpolate` (Fixes: #2883) --- CHANGES.md | 1 + src/tools/utils.ml | 25 ++++++++++++++++--------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 2f83bab605..0f6931c398 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -29,6 +29,7 @@ Fixed: * Fixed input polling stop (#2769) * Fixed crash in external processes when received a `Unix.EINTR` event (#2861) +* Fixed crash in `string.interpolate` (#2883) * Cleaned up srt support. --- diff --git a/src/tools/utils.ml b/src/tools/utils.ml index d793beee6d..a9c75e6e17 100644 --- a/src/tools/utils.ml +++ b/src/tools/utils.ml @@ -20,6 +20,14 @@ *****************************************************************************) +(* Util to log exception and backtrace together + when log level is set to info and just exception + as severe otherwise. Backtrace should be captured as early + as possible. *) +let log_exception ~(log : Log.t) ~bt msg = + if log#active 4 (* info *) then log#info "%s\n%s" msg bt + else log#severe "%s" msg + (* Force locale to C *) external force_locale : unit -> unit = "liquidsoap_set_locale" [@@noalloc] @@ -380,6 +388,7 @@ let unbreak_md md = * and interpolates them just like make does, using the hash table for * variable definitions. *) let interpolate = + let log = Log.make ["string"; "interpolate"] in (* TODO Use PCRE *) let quoted = "\"\\(\\([^\"\\]\\|\\(\\\\\"\\)\\)*\\)\"" in (* 3 groups *) @@ -418,7 +427,13 @@ let interpolate = if p = "\\" then Str.matched_group 2 s else p ^ find (Str.matched_group 3 s)) in - interpolate (process_if s) + try interpolate (process_if s) + with exn -> + let bt = Printexc.get_backtrace () in + log_exception ~log ~bt + (Printf.sprintf "Error while interpolating string: %s" + (Printexc.to_string exn)); + s (** [which s] is equivalent to /usr/bin/which s, raises Not_found on error *) let which ~path s = @@ -869,14 +884,6 @@ let string_of_matrix a = done; Strings.Mutable.to_string ans -(* Util to log exception and backtrace together - when log level is set to info and just exception - as severe otherwise. Backtrace should be captured as early - as possible. *) -let log_exception ~(log : Log.t) ~bt msg = - if log#active 4 (* info *) then log#info "%s\n%s" msg bt - else log#severe "%s" msg - (** Operations on versions of Liquidsoap. *) module Version = struct type t = int list * string