Skip to content

Commit

Permalink
Merge pull request #45 from dra27/third-time-lucky
Browse files Browse the repository at this point in the history
Use Scanf correctly
  • Loading branch information
AltGr authored May 21, 2021
2 parents 007cda4 + 338fe81 commit e2d3ac0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
3 changes: 2 additions & 1 deletion CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
item. If opam-version is at the start and is greater than the library version,
`OpamLexer.Error` and `Parsing.Parse_error` are no longer raised; instead the
opam-version variable is returned along with an invalid group to signal the
parsing error, giving the client enough information to act. [#43, #44 @dra27]
parsing error, giving the client enough information to act.
[#43, #44, #45 @dra27]

2.1.2 [07 Jan 2021]
* Some hash-consing for strings [#27 @AltGr]
Expand Down
10 changes: 8 additions & 2 deletions src/opamBaseParser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,14 @@ let nopatch v =
in
f 0
in
try Scanf.sscanf s "%u.%u" (fun maj min -> (maj, min))
with Scanf.Scan_failure _ -> (0, 0)
try Scanf.sscanf s "%u.%u%!" (fun maj min -> (maj, min))
with Scanf.Scan_failure _
| Failure _
| End_of_file ->
try Scanf.sscanf s "%u%!" (fun maj -> (maj, 0))
with Scanf.Scan_failure _
| Failure _
| End_of_file -> (0, 0)

let with_clear_parser f x =
try
Expand Down
10 changes: 8 additions & 2 deletions src/opamPrinter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,14 @@ let nopatch v =
in
f 0
in
try Scanf.sscanf s "%u.%u" (fun maj min -> (maj, min))
with Scanf.Scan_failure _ -> (0, 0)
try Scanf.sscanf s "%u.%u%!" (fun maj min -> (maj, min))
with Scanf.Scan_failure _
| Failure _
| End_of_file ->
try Scanf.sscanf s "%u%!" (fun maj -> (maj, 0))
with Scanf.Scan_failure _
| Failure _
| End_of_file -> (0, 0)

let valid_opamfile_contents = function
| (Variable (_, "opam-version", String (_, ver)))::items
Expand Down
5 changes: 5 additions & 0 deletions tests/versions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ let has_sentinel =
| _ -> false

let tests_noexn = [
"opam-version 4 and parsing error",
{|
opam-version: "4"
!!
|};
"opam-version 42.0 and parsing error",
{|
opam-version: "42.0"
Expand Down

0 comments on commit e2d3ac0

Please sign in to comment.