From 338fe816d942c7bae2992dd4403a42976dade64c Mon Sep 17 00:00:00 2001 From: David Allsopp Date: Thu, 20 May 2021 17:37:04 +0100 Subject: [PATCH] Use Scanf correctly --- CHANGES | 3 ++- src/opamBaseParser.mly | 10 ++++++++-- src/opamPrinter.ml | 10 ++++++++-- tests/versions.ml | 5 +++++ 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/CHANGES b/CHANGES index 22fa3d2..17f58e9 100644 --- a/CHANGES +++ b/CHANGES @@ -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] diff --git a/src/opamBaseParser.mly b/src/opamBaseParser.mly index 7a03b59..933676d 100644 --- a/src/opamBaseParser.mly +++ b/src/opamBaseParser.mly @@ -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 diff --git a/src/opamPrinter.ml b/src/opamPrinter.ml index 1f2e6a5..2fe25bb 100644 --- a/src/opamPrinter.ml +++ b/src/opamPrinter.ml @@ -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 diff --git a/tests/versions.ml b/tests/versions.ml index 6095a2a..07be7b6 100644 --- a/tests/versions.ml +++ b/tests/versions.ml @@ -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"