Skip to content

Commit

Permalink
Apparent issue in the reading code: the data might not come to Java f…
Browse files Browse the repository at this point in the history
…ast enough from the Python client (or wherever) for it to read the whole thing in one bite. To fix this, we read chunks until reading a chunk fails or there are no more chunks.

Note that this means a malicious input will cause the program to hang.  Perhaps a timeout is needed.  Of course, it is only expected to be used as a pipe on the other side of the Stanza pipeline tools.
  • Loading branch information
AngledLuffa committed Nov 9, 2022
1 parent 61fbc6c commit 412da5c
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/edu/stanford/nlp/util/ProcessProtobufRequest.java
Expand Up @@ -8,6 +8,7 @@

package edu.stanford.nlp.util;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
Expand Down Expand Up @@ -35,7 +36,7 @@ public abstract class ProcessProtobufRequest {
* https://developers.google.com/protocol-buffers/docs/techniques#streamimg
*/
public void processMultipleInputs(InputStream in, OutputStream out) throws IOException {
DataInputStream din = new DataInputStream(in);
DataInputStream din = new DataInputStream(new BufferedInputStream(in));
DataOutputStream dout = new DataOutputStream(out);
int size = 0;
do {
Expand All @@ -53,7 +54,15 @@ public void processMultipleInputs(InputStream in, OutputStream out) throws IOExc
}

byte[] inputArray = new byte[size];
din.read(inputArray, 0, size);
int lenRead = 0;
while (lenRead < size) {
int chunk = din.read(inputArray, lenRead, size - lenRead);
if (chunk <= 0) {
// Oops, guess something went wrong on the other side
throw new IOException("Failed to read as much data as we were supposed to!");
}
lenRead += chunk;
}
ByteArrayInputStream bin = new ByteArrayInputStream(inputArray);
ByteArrayOutputStream result = new ByteArrayOutputStream();
processInputStream(bin, result);
Expand Down

0 comments on commit 412da5c

Please sign in to comment.