Skip to content

Commit

Permalink
Prevent initializing stereotool with two different libraries. (#4182)
Browse files Browse the repository at this point in the history
  • Loading branch information
toots authored Oct 20, 2024
1 parent 3d2cb3b commit 72e8c6b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/core/operators/stereotool_op.ml
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,18 @@ let _ =
let preset = Lang.to_valued_option Lang.to_string preset_val in
let handler =
let library = Utils.check_readable ~pos:(Lang.pos p) library in
try Stereotool.init ?license_key ~filename:library ()
with Stereotool.Library_not_found ->
Runtime_error.raise ~pos:(Lang.pos p)
~message:"Invalid stereotool library" "invalid"
try Stereotool.init ?license_key ~filename:library () with
| Stereotool.Library_not_found ->
Runtime_error.raise ~pos:(Lang.pos p)
~message:"Invalid stereotool library" "invalid"
| Stereotool.Library_initialized f ->
Runtime_error.raise ~pos:(Lang.pos p)
~message:
(Printf.sprintf
"Stereotool already initialized with a different library: \
%s"
(Lang_string.quote_string f))
"invalid"
in
(match preset with
| None -> ()
Expand Down
6 changes: 6 additions & 0 deletions src/stereotool/stereotool.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module type Config = sig
end

exception Library_not_found
exception Library_initialized of string

let strnlen = foreign "strnlen" (ocaml_bytes @-> int @-> returning int)

Expand Down Expand Up @@ -78,7 +79,12 @@ let int_of_load_type = function
| `Repair_no_pnr -> 11069
| `Sublevel_pnr -> 10699

let initialized = Atomic.make None

let init ?license_key ~filename () =
(match Atomic.get initialized with
| Some f when f <> filename -> raise (Library_initialized f)
| _ -> Atomic.set initialized (Some filename));
try
let module C = C (struct
let filename = filename
Expand Down
1 change: 1 addition & 0 deletions src/stereotool/stereotool.mli
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type load_type =
| `Sublevel_pnr ]

exception Library_not_found
exception Library_initialized of string

val init : ?license_key:string -> filename:string -> unit -> t
val software_version : t -> int
Expand Down

0 comments on commit 72e8c6b

Please sign in to comment.