From 7cc3a29da65788aebffd172ff53d6e5c147163b1 Mon Sep 17 00:00:00 2001 From: ix0rai Date: Fri, 25 Oct 2024 12:09:38 -0500 Subject: [PATCH] validate version specifier in v2 reader --- .../datadriven/parser/v2/UnpickV2Reader.java | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/unpick-format-utils/src/main/java/daomephsta/unpick/constantmappers/datadriven/parser/v2/UnpickV2Reader.java b/unpick-format-utils/src/main/java/daomephsta/unpick/constantmappers/datadriven/parser/v2/UnpickV2Reader.java index 4fc33ed..2361a96 100644 --- a/unpick-format-utils/src/main/java/daomephsta/unpick/constantmappers/datadriven/parser/v2/UnpickV2Reader.java +++ b/unpick-format-utils/src/main/java/daomephsta/unpick/constantmappers/datadriven/parser/v2/UnpickV2Reader.java @@ -38,10 +38,13 @@ public UnpickV2Reader(InputStream definitionsStream) */ public void accept(Visitor visitor) { - try(LineNumberReader reader = new LineNumberReader(new InputStreamReader(definitionsStream))) + try(InputStreamReader streamReader = new InputStreamReader(definitionsStream)) { + validateVersion(streamReader); + LineNumberReader reader = new LineNumberReader(streamReader); + Iterator lineTokensIter = reader.lines() - .skip(1) //Skip version + .skip(1) // we already checked the version .map(s -> stripComment(s.trim())) .filter(s -> !s.isEmpty()) //Discard empty lines & lines that are empty once comments are stripped .map(l -> Arrays.stream(WHITESPACE_SPLITTER.split(l)).filter(s -> !s.isEmpty()).toArray(String[]::new)) //Tokenise lines @@ -98,6 +101,28 @@ public void accept(Visitor visitor) } } + private void validateVersion(InputStreamReader definitionStream) throws IOException { + // only consume chars for the version + char[] versionChars = new char [2]; + definitionStream.read(versionChars); + if (versionChars[0] == 'v') + { + switch (versionChars[1]) + { + case '1': + throw new UnpickSyntaxException(1, "Incompatible version! (v1 data passed to v2 reader)"); + + case '2': + return; + + default : + throw new UnpickSyntaxException(1, "Unknown version " + versionChars[1]); + } + } + else + throw new UnpickSyntaxException(1, "Missing version"); + } + private void visitParameterConstantGroupDefinition(Visitor visitor, String[] tokens, int lineNumber) { if (lastTargetMethodVisitor == null)