-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
poi: #60626 - ArrayIndexOutOfBoundsException in EvilUnclosedBRFixingInputStream #1451
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,240 @@ | ||
/* ==================================================================== | ||
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. | ||
==================================================================== */ | ||
|
||
|
||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
|
||
namespace NPOI.Util | ||
{ | ||
|
||
/// <summary> | ||
/// <para> | ||
/// Simple FilterInputStream that can replace occurrences of bytes with something else. | ||
/// </para> | ||
/// <para> | ||
/// This has been taken from inbot-utils. (MIT licensed) | ||
/// </para> | ||
/// </summary> | ||
/// @see <see href="https://github.com/Inbot/inbot-utils">inbot-utils</see> | ||
public class ReplacingInputStream : FilterInputStream | ||
{ | ||
|
||
// while matching, this is where the bytes go. | ||
int[] buf; | ||
private int matchedIndex=0; | ||
private int unbufferIndex=0; | ||
private int replacedIndex=0; | ||
|
||
private byte[] pattern; | ||
private byte[] replacement; | ||
private State state=State.NOT_MATCHED; | ||
|
||
// simple state machine for keeping track of what we are doing | ||
private enum State | ||
{ | ||
NOT_MATCHED, | ||
MATCHING, | ||
REPLACING, | ||
UNBUFFER | ||
} | ||
|
||
//private static Charset UTF8 = Charset.forName("UTF-8"); | ||
|
||
/// <summary> | ||
/// Replace occurrences of pattern in the input. Note: input is assumed to be UTF-8 encoded. If not the case use byte[] based pattern and replacement. | ||
/// </summary> | ||
/// <param name="in">input</param> | ||
/// <param name="pattern">pattern to replace.</param> | ||
/// <param name="replacement">the replacement or null</param> | ||
public ReplacingInputStream(InputStream in1, String pattern, String replacement) | ||
: this(in1, Encoding.UTF8.GetBytes(pattern), replacement==null ? null : Encoding.UTF8.GetBytes(replacement)) | ||
{ | ||
|
||
} | ||
|
||
/// <summary> | ||
/// <para> | ||
/// Replace occurrences of pattern in the input. | ||
/// </para> | ||
/// <para> | ||
/// If you want to normalize line endings DOS/MAC (\n\r | \r) to UNIX (\n), you can call the following:<br/> | ||
/// {@code new ReplacingInputStream(new ReplacingInputStream(is, "\n\r", "\n"), "\r", "\n")} | ||
/// </para> | ||
/// </summary> | ||
/// <param name="in">input</param> | ||
/// <param name="pattern">pattern to replace</param> | ||
/// <param name="replacement">the replacement or null</param> | ||
public ReplacingInputStream(InputStream in1, byte[] pattern, byte[] replacement) | ||
: base(in1) | ||
{ | ||
; | ||
if(pattern == null || pattern.Length == 0) | ||
{ | ||
throw new ArgumentException("pattern length should be > 0"); | ||
} | ||
this.pattern = pattern; | ||
this.replacement = replacement; | ||
// we will never match more than the pattern length | ||
buf = new int[pattern.Length]; | ||
} | ||
public override int Read(byte[] b, int off, int len) | ||
{ | ||
|
||
// copy of parent logic; we need to call our own read() instead of super.read(), which delegates instead of calling our read | ||
if(b == null) | ||
{ | ||
throw new NullReferenceException(); | ||
} | ||
else if(off < 0 || len < 0 || len > b.Length - off) | ||
{ | ||
throw new IndexOutOfRangeException(); | ||
} | ||
else if(len == 0) | ||
{ | ||
return 0; | ||
} | ||
|
||
int c = Read(); | ||
if(c == -1) | ||
{ | ||
return -1; | ||
} | ||
b[off] = (byte) c; | ||
|
||
int i = 1; | ||
for(; i < len; i++) | ||
{ | ||
c = Read(); | ||
if(c == -1) | ||
{ | ||
break; | ||
} | ||
b[off + i] = (byte) c; | ||
} | ||
return i; | ||
|
||
} | ||
public override int Read(byte[] b) | ||
{ | ||
|
||
// call our own read | ||
return Read(b, 0, b.Length); | ||
} | ||
public override int Read() | ||
{ | ||
|
||
// use a simple state machine to figure out what we are doing | ||
int next; | ||
switch(state) | ||
{ | ||
default: | ||
case State.NOT_MATCHED: | ||
// we are not currently matching, replacing, or unbuffering | ||
next=base.Read(); | ||
if(pattern[0] != next) | ||
{ | ||
return next; | ||
} | ||
|
||
// clear whatever was there | ||
Arrays.Fill(buf, 0); | ||
// make sure we start at 0 | ||
matchedIndex=0; | ||
|
||
buf[matchedIndex++]=next; | ||
if(pattern.Length == 1) | ||
{ | ||
// edge-case when the pattern length is 1 we go straight to replacing | ||
state=State.REPLACING; | ||
// reset replace counter | ||
replacedIndex=0; | ||
} | ||
else | ||
{ | ||
// pattern of length 1 | ||
state=State.MATCHING; | ||
} | ||
// recurse to continue matching | ||
return Read(); | ||
|
||
case State.MATCHING: | ||
// the previous bytes matched part of the pattern | ||
next=base.Read(); | ||
if(pattern[matchedIndex]==next) | ||
{ | ||
buf[matchedIndex++]=next; | ||
if(matchedIndex==pattern.Length) | ||
{ | ||
// we've found a full match! | ||
if(replacement==null || replacement.Length==0) | ||
{ | ||
// the replacement is empty, go straight to NOT_MATCHED | ||
state=State.NOT_MATCHED; | ||
matchedIndex=0; | ||
} | ||
else | ||
{ | ||
// start replacing | ||
state=State.REPLACING; | ||
replacedIndex=0; | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
// mismatch -> unbuffer | ||
buf[matchedIndex++]=next; | ||
state=State.UNBUFFER; | ||
unbufferIndex=0; | ||
} | ||
return Read(); | ||
|
||
case State.REPLACING: | ||
// we've fully matched the pattern and are returning bytes from the replacement | ||
next=replacement[replacedIndex++]; | ||
if(replacedIndex==replacement.Length) | ||
{ | ||
state=State.NOT_MATCHED; | ||
replacedIndex=0; | ||
} | ||
return next; | ||
|
||
case State.UNBUFFER: | ||
// we partially matched the pattern before encountering a non matching byte | ||
// we need to serve up the buffered bytes before we go back to NOT_MATCHED | ||
next=buf[unbufferIndex++]; | ||
if(unbufferIndex==matchedIndex) | ||
{ | ||
state=State.NOT_MATCHED; | ||
matchedIndex=0; | ||
} | ||
return next; | ||
} | ||
} | ||
public override String ToString() | ||
{ | ||
return state.ToString() + " " + matchedIndex + " " + replacedIndex + " " + unbufferIndex; | ||
} | ||
|
||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't change this code. I remember this is used to fix something.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There were issues with the previous fix. This fix has added the test case
TestEvilUnclosedBRFixing
, the previous fix make it failed. I think it was used to fix the Unclosed BR tag issue.The Unclosed BR tag should not be deleted, but rather it should be corrected.