From 84831308f0cb73da6b12e3b309c86c73c71dc11c Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Mon, 7 May 2018 21:36:07 +0700 Subject: [PATCH] Make sure that Alias0.dir always goes through dir check the directory must always be inside the build dir, so we make sure that any way to create the record validates this invariant --- src/build_system.ml | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/src/build_system.ml b/src/build_system.ml index 2ca8095e..890ddf67 100644 --- a/src/build_system.ml +++ b/src/build_system.ml @@ -212,7 +212,27 @@ module File_spec = struct end module Alias0 = struct - type t = { dir : Path.t; name : string } + module T : sig + type t = private + { dir : Path.t + ; name : string + } + val make : string -> dir:Path.t -> t + end = struct + type t = + { dir : Path.t + ; name : string + } + + let make name ~dir = + if not (Path.is_in_build_dir dir) || String.contains name '/' then + Exn.code_error "Alias0.make: Invalid alias" + [ "name", Sexp.To_sexp.string name + ; "dir", Path.sexp_of_t dir + ]; + { dir; name } + end + include T let pp fmt t = Path.pp fmt (Path.relative t.dir t.name) @@ -222,24 +242,13 @@ module Alias0 = struct if not (Path.is_in_build_dir path) then die "Invalid alias!\nTried to reference alias %S" (Path.to_string_maybe_quoted path); - { dir = Path.parent path - ; name = Path.basename path - } + make ~dir:(Path.parent path) (Path.basename path) let name t = t.name let dir t = t.dir let fully_qualified_name t = Path.relative t.dir t.name - let make name ~dir = - assert (not (String.contains name '/')); - if not (Path.is_in_build_dir dir) then - Exn.code_error "Alias0.make: Invalid alias" - [ "name", Sexp.To_sexp.string name - ; "dir", Path.sexp_of_t dir - ]; - { dir; name } - let stamp_file t = Path.relative (Path.insert_after_build_dir_exn t.dir ".aliases") (t.name ^ suffix)