Skip to content

Commit

Permalink
Merge pull request #2614 from guusdk/OF-2911_NPE
Browse files Browse the repository at this point in the history
OF-2911: Catch NPE in Parser that is in an invalid state
  • Loading branch information
akrherz authored Nov 20, 2024
2 parents 6dfbcc2 + 5e578b0 commit e651adf
Showing 1 changed file with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets;
Expand All @@ -36,7 +35,7 @@
* the channel.
* When a message is complete you can retrieve messages invoking the method
* getMsgs() and you can invoke the method areThereMsgs() to know if at least
* an message is presents.
* a message is presents.
*
* @author Daniele Piras
* @author Gaston Dombiak
Expand Down Expand Up @@ -113,7 +112,7 @@ public class XMLLightweightParser {
* true if the parser has found some complete xml message.
*/
public boolean areThereMsgs() {
return (msgs.size() > 0);
return (!msgs.isEmpty());
}

/*
Expand All @@ -133,7 +132,7 @@ public String[] getMsgs() {
* Method use to re-initialize the buffer
*/
protected void invalidateBuffer() {
if (buffer.length() > 0) {
if (!buffer.isEmpty()) {
String str = buffer.substring(startLastMsg);
buffer.delete(0, buffer.length());
buffer.append(str);
Expand Down Expand Up @@ -170,6 +169,9 @@ public boolean isMaxBufferSizeExceeded() {
}

public void read(ByteBuf in) throws Exception {
if (buffer == null) {
throw new IllegalStateException("Unable to parse any more data, as previous data was invalid. This is unrecoverable.");
}
invalidateBuffer();
// Check that the buffer is not bigger than 1 Megabyte. For security reasons
// we will abort parsing when 1 Mega of queued chars was found.
Expand Down Expand Up @@ -347,7 +349,7 @@ else if (ch == '!') {
insideChildrenTag = false;
continue;
}
else if (ch == '/' && head.length() > 0) {
else if (ch == '/' && !head.isEmpty()) {
status = XMLLightweightParser.VERIFY_CLOSE_TAG;
depth--;
}
Expand Down

0 comments on commit e651adf

Please sign in to comment.