mbeckerle commented on a change in pull request #641:
URL: https://github.com/apache/daffodil/pull/641#discussion_r714019782



##########
File path: 
daffodil-core/src/main/scala/org/apache/daffodil/compiler/Compiler.scala
##########
@@ -283,7 +284,31 @@ class Compiler private (var validateDFDLSchemas: Boolean,
 
   def reload(savedParser: java.nio.channels.ReadableByteChannel): 
DFDL.DataProcessor = {
     try {
-      val objInput = new ObjectInputStream(new 
GZIPInputStream(Channels.newInputStream(savedParser))) {
+      val is = Channels.newInputStream(savedParser)
+
+      // Read the magic number and version information for this saved parser
+      // directly from the input stream. That information is not compressed or
+      // serialized
+      val magicNumber = "DAFFODIL "

Review comment:
       calling this "magic number" is just confusing. It's not a number, and 
it's not like magic numbers that are just a specific 2 bytes at start of a 
file. 
   
   Call it requiredDataPrefixString or something clearer. 

##########
File path: 
daffodil-core/src/main/scala/org/apache/daffodil/compiler/Compiler.scala
##########
@@ -283,7 +284,31 @@ class Compiler private (var validateDFDLSchemas: Boolean,
 
   def reload(savedParser: java.nio.channels.ReadableByteChannel): 
DFDL.DataProcessor = {
     try {
-      val objInput = new ObjectInputStream(new 
GZIPInputStream(Channels.newInputStream(savedParser))) {
+      val is = Channels.newInputStream(savedParser)
+
+      // Read the magic number and version information for this saved parser
+      // directly from the input stream. That information is not compressed or
+      // serialized
+      val magicNumber = "DAFFODIL "
+      magicNumber.foreach { c =>
+        if (is.read() != c.toInt) throw new InvalidParserException("The saved 
parser is only compatible with an older version of Daffodil")
+      }
+
+      val sb = new StringBuilder()
+      var byte: Int = -1
+      while ({ byte = is.read(); byte > 0 }) {
+        sb.append(byte.toChar)
+      }
+      if (byte == -1) {
+        throw new InvalidParserException("The saved parser file is not the 
correct format")

Review comment:
       This is worse than "not the correct format" it means the file is 
corrupted, ends early because at the point where we should get a NUL we instead 
get EOF.  At this point it has a proper prefix string, but doesn't have a 
proper version number and NUL separating it from the rest. This could say 
exactly that. Saved parser file has proper prefix 'DAFFODIL" but otherwise is 
not properly formed. 
   

##########
File path: 
daffodil-core/src/main/scala/org/apache/daffodil/compiler/Compiler.scala
##########
@@ -283,7 +284,31 @@ class Compiler private (var validateDFDLSchemas: Boolean,
 
   def reload(savedParser: java.nio.channels.ReadableByteChannel): 
DFDL.DataProcessor = {
     try {
-      val objInput = new ObjectInputStream(new 
GZIPInputStream(Channels.newInputStream(savedParser))) {
+      val is = Channels.newInputStream(savedParser)
+
+      // Read the magic number and version information for this saved parser
+      // directly from the input stream. That information is not compressed or
+      // serialized
+      val magicNumber = "DAFFODIL "
+      magicNumber.foreach { c =>
+        if (is.read() != c.toInt) throw new InvalidParserException("The saved 
parser is only compatible with an older version of Daffodil")
+      }
+
+      val sb = new StringBuilder()
+      var byte: Int = -1
+      while ({ byte = is.read(); byte > 0 }) {
+        sb.append(byte.toChar)
+      }
+      if (byte == -1) {
+        throw new InvalidParserException("The saved parser file is not the 
correct format")
+      }
+      if (sb.toString != Misc.getDaffodilVersion) {
+        throw new InvalidParserException("The saved parser is only compatible 
with Daffodil " + sb.toString)

Review comment:
       Add the current daffodil version that you are attempting to reload into 
here, to make the message more useful. 




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to