Skip to content

Commit

Permalink
Fix ocaml-ppx#97: version-specific magic numbers check in `Ast_4NN.re…
Browse files Browse the repository at this point in the history
…gister`

This commit fixes
ocaml-ppx#97
which caused segfaults: `Ast_4NN.register` now correctly checks
AST magic number against the version-specific magic number instead
of using the magic number defined in the `Config` module of the
current compiler.

Version-specific magic numbers was already defined by overriding
`Config` module later in the file: the fix just consists in riding up the
overriding of `Config` before the overriding of
`Ast_helper`. Following Gabriel Scherer's trick recommended in
ocaml-ppx/ppx_deriving#210 (comment)
a unit-value `in_ast_4nn` is defined in `Config` module and used in
`Ast_helper.register` to ensure that the module are well ordered.
  • Loading branch information
thierry-martinez committed May 22, 2020
1 parent 1947fe8 commit f336d74
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 20 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

- No longer expose the unwrapped modules

- Fix version-specific magic numbers check in `Ast_4NN.register`
(#97, @gasche, @kit-ty-kate, @thierry-martinez)

v1.7.3 2020-05-07 Canterbury
----------------------------

Expand Down
17 changes: 12 additions & 5 deletions src/ast_408.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2732,6 +2732,17 @@ end = struct
end
end

module Config = struct
(** [in_ast_408] allows {!val:Ast_mapper.register} to compile correctly only
if this [Config] module has been overriden with the version-specific
magic numbers.
See {{:https://github.com/ocaml-ppx/ocaml-migrate-parsetree/issues/97} *)
let in_ast_408 = ()

let ast_impl_magic_number = "Caml1999M025"
let ast_intf_magic_number = "Caml1999N025"
end

module Ast_mapper : sig
open Parsetree

Expand Down Expand Up @@ -3821,6 +3832,7 @@ end = struct
in

let ic = open_in_bin source in
let () = Config.in_ast_408 in
let magic =
really_input_string ic (String.length Config.ast_impl_magic_number)
in
Expand Down Expand Up @@ -4040,11 +4052,6 @@ module Outcometree = struct
| Ophr_exception of (exn * out_value)
end

module Config = struct
let ast_impl_magic_number = "Caml1999M025"
let ast_intf_magic_number = "Caml1999N025"
end

let map_signature mapper = mapper.Ast_mapper.signature mapper
let map_structure mapper = mapper.Ast_mapper.structure mapper

Expand Down
17 changes: 12 additions & 5 deletions src/ast_409.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2721,6 +2721,17 @@ end = struct
end
end

module Config = struct
(** [in_ast_409] allows {!val:Ast_mapper.register} to compile correctly only
if this [Config] module has been overriden with the version-specific
magic numbers.
See {{:https://github.com/ocaml-ppx/ocaml-migrate-parsetree/issues/97} *)
let in_ast_409 = ()

let ast_impl_magic_number = "Caml1999M026"
let ast_intf_magic_number = "Caml1999N026"
end

module Ast_mapper : sig
open Parsetree

Expand Down Expand Up @@ -3810,6 +3821,7 @@ end = struct
in

let ic = open_in_bin source in
let () = Config.in_ast_409 in
let magic =
really_input_string ic (String.length Config.ast_impl_magic_number)
in
Expand Down Expand Up @@ -4029,11 +4041,6 @@ module Outcometree = struct
| Ophr_exception of (exn * out_value)
end

module Config = struct
let ast_impl_magic_number = "Caml1999M026"
let ast_intf_magic_number = "Caml1999N026"
end

let map_signature mapper = mapper.Ast_mapper.signature mapper
let map_structure mapper = mapper.Ast_mapper.structure mapper

Expand Down
17 changes: 12 additions & 5 deletions src/ast_410.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2727,6 +2727,17 @@ end = struct
end
end

module Config = struct
(** [in_ast_410] allows {!val:Ast_mapper.register} to compile correctly only
if this [Config] module has been overriden with the version-specific
magic numbers.
See {{:https://github.com/ocaml-ppx/ocaml-migrate-parsetree/issues/97} *)
let in_ast_410 = ()

let ast_impl_magic_number = "Caml1999M027"
let ast_intf_magic_number = "Caml1999N027"
end

module Ast_mapper : sig
open Parsetree

Expand Down Expand Up @@ -3824,6 +3835,7 @@ end = struct
in

let ic = open_in_bin source in
let () = Config.in_ast_410 in
let magic =
really_input_string ic (String.length Config.ast_impl_magic_number)
in
Expand Down Expand Up @@ -4050,11 +4062,6 @@ module Outcometree = struct
| Ophr_exception of (exn * out_value)
end

module Config = struct
let ast_impl_magic_number = "Caml1999M027"
let ast_intf_magic_number = "Caml1999N027"
end

let map_signature mapper = mapper.Ast_mapper.signature mapper
let map_structure mapper = mapper.Ast_mapper.structure mapper

Expand Down
17 changes: 12 additions & 5 deletions src/ast_411.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2728,6 +2728,17 @@ end = struct
end
end

module Config = struct
(** [in_ast_411] allows {!val:Ast_mapper.register} to compile correctly only
if this [Config] module has been overriden with the version-specific
magic numbers.
See {{:https://github.com/ocaml-ppx/ocaml-migrate-parsetree/issues/97} *)
let in_ast_411 = ()

let ast_impl_magic_number = "Caml1999M028"
let ast_intf_magic_number = "Caml1999N028"
end

module Ast_mapper : sig
open Parsetree

Expand Down Expand Up @@ -3841,6 +3852,7 @@ end = struct
in

let ic = open_in_bin source in
let () = Config.in_ast_411 in
let magic =
really_input_string ic (String.length Config.ast_impl_magic_number)
in
Expand Down Expand Up @@ -4067,11 +4079,6 @@ module Outcometree = struct
| Ophr_exception of (exn * out_value)
end

module Config = struct
let ast_impl_magic_number = "Caml1999M028"
let ast_intf_magic_number = "Caml1999N028"
end

let map_signature mapper = mapper.Ast_mapper.signature mapper
let map_structure mapper = mapper.Ast_mapper.structure mapper

Expand Down

0 comments on commit f336d74

Please sign in to comment.