mawiesne commented on code in PR #1022:
URL: https://github.com/apache/opennlp/pull/1022#discussion_r3144075581


##########
opennlp-core/opennlp-ml/opennlp-ml-commons/src/main/java/opennlp/tools/ml/model/AbstractModelReader.java:
##########
@@ -24,11 +24,39 @@
 import java.util.StringTokenizer;
 import java.util.zip.GZIPInputStream;
 
+import opennlp.tools.util.InvalidFormatException;

Review Comment:
   From the 
[Javadoc](https://opennlp.apache.org/docs/2.5.8/apidocs/opennlp-tools/opennlp/tools/util/InvalidFormatException.html)
 of InvalidFormatException:
   
   > This exception indicates that a resource violates the expected data format.
   
   Imho, that is not the case here.
   
   Why?
   
   Model parameters are read and treated as arguments. There is no true 
"format" related problem with the model resource.
   
   Another exception type should be chosen.
   
   Candidate:
   `java.lang.IllegalArgumentException`



##########
opennlp-core/opennlp-ml/opennlp-ml-commons/src/main/java/opennlp/tools/ml/model/AbstractModelReader.java:
##########
@@ -131,6 +159,10 @@ public AbstractModel getModel() throws IOException {
    */
   protected String[] getOutcomes() throws IOException {
     int numOutcomes = readInt();
+    if (numOutcomes < 0 || numOutcomes > MAX_ENTRIES) {
+      throw new InvalidFormatException(

Review Comment:
   Please add "@throws" and short desc to the existing Javadoc (-> 
IllegalArgumentException)



##########
opennlp-core/opennlp-ml/opennlp-ml-commons/src/test/java/opennlp/tools/ml/model/AbstractModelReaderOomTest.java:
##########
@@ -0,0 +1,117 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package opennlp.tools.ml.model;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+
+import org.junit.jupiter.api.Test;
+
+import opennlp.tools.util.InvalidFormatException;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+class AbstractModelReaderOomTest {

Review Comment:
   The test class could benefit by a comment on the test approach, referring to 
the Issue or PR number. 



##########
opennlp-core/opennlp-ml/opennlp-ml-commons/src/test/java/opennlp/tools/ml/model/AbstractModelReaderOomTest.java:
##########
@@ -0,0 +1,117 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package opennlp.tools.ml.model;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+
+import org.junit.jupiter.api.Test;
+
+import opennlp.tools.util.InvalidFormatException;

Review Comment:
   See previous concern/comment above.



##########
opennlp-core/opennlp-ml/opennlp-ml-commons/src/main/java/opennlp/tools/ml/model/ModelParameterChunker.java:
##########
@@ -87,6 +89,10 @@ public static String readUTF(DataInputStream dis) throws 
IOException {
     if (data.startsWith(SIGNATURE_CHUNKED_PARAMS)) {
       String chunkElements = data.replace(SIGNATURE_CHUNKED_PARAMS, "");
       int chunkSize = Integer.parseInt(chunkElements);
+      if (chunkSize <= 0 || chunkSize > AbstractModelReader.MAX_ENTRIES) {
+        throw new InvalidFormatException(

Review Comment:
   Please add "@throws" and short desc to the existing Javadoc (-> 
IllegalArgumentException)



##########
opennlp-core/opennlp-ml/opennlp-ml-commons/src/main/java/opennlp/tools/ml/model/AbstractModelReader.java:
##########
@@ -142,6 +174,10 @@ protected String[] getOutcomes() throws IOException {
    */
   protected int[][] getOutcomePatterns() throws IOException {
     int numOCTypes = readInt();
+    if (numOCTypes < 0 || numOCTypes > MAX_ENTRIES) {
+      throw new InvalidFormatException(

Review Comment:
   Please add "@throws" and short desc to the existing Javadoc (-> 
IllegalArgumentException)



##########
opennlp-core/opennlp-ml/opennlp-ml-commons/src/main/java/opennlp/tools/ml/model/AbstractModelReader.java:
##########
@@ -160,6 +196,10 @@ protected int[][] getOutcomePatterns() throws IOException {
    */
   protected String[] getPredicates() throws IOException {
     NUM_PREDS = readInt();
+    if (NUM_PREDS < 0 || NUM_PREDS > MAX_ENTRIES) {
+      throw new InvalidFormatException(

Review Comment:
   Please add "@throws" and short desc to the existing Javadoc (-> 
IllegalArgumentException)



##########
opennlp-core/opennlp-ml/opennlp-ml-commons/src/main/java/opennlp/tools/ml/model/ModelParameterChunker.java:
##########
@@ -87,6 +89,10 @@ public static String readUTF(DataInputStream dis) throws 
IOException {
     if (data.startsWith(SIGNATURE_CHUNKED_PARAMS)) {
       String chunkElements = data.replace(SIGNATURE_CHUNKED_PARAMS, "");
       int chunkSize = Integer.parseInt(chunkElements);
+      if (chunkSize <= 0 || chunkSize > AbstractModelReader.MAX_ENTRIES) {

Review Comment:
   Note/Question:
   The value of chunkSize is _not_ used to allocate memory via an array in the 
below code. 
   
   Why should it be limited via an upper boundary for OOM protection reasons 
here? Please elaborate.
   
   The lower / negative case is also not harmful, as the loop would just be 
skipped.



-- 
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