Skip to content

Commit

Permalink
Fix #97
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Dec 6, 2019
1 parent fee31c2 commit 489149b
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 22 deletions.
5 changes: 5 additions & 0 deletions release-notes/CREDITS
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,8 @@ Ivo Studensky (istudens@github)

* Reported #93: Stax `maxAttributeSize` limit is only vaguely respected
(6.0.3)

Daniel Lowe (dan2097@github)

* Reported #97: `copyEventFromReader()` `ArrayIndexOutOfBoundsException`
(6.0.3)
4 changes: 3 additions & 1 deletion release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ Project: woodstox

#88: Missing closing quote for attribute values during in Validating output mode
(reported, fixed by michaelsiegel@github)
#93: Stax `maxAttributeSize` limit is only vaguely respected
#93: Stax2 `maxAttributeSize` limit is only vaguely respected
(reported by Ivo S)
#97: `copyEventFromReader()` `ArrayIndexOutOfBoundsException`
(reported by Daniel L)

6.0.2 (12-Oct-2019)

Expand Down
46 changes: 27 additions & 19 deletions src/main/java/com/ctc/wstx/sr/BasicStreamReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -5140,7 +5140,7 @@ private int readAndWriteText(Writer w)

main_loop:
while (true) {
char c;
char c;
// Reached the end of buffer? Need to flush, then
if (mInputPtr >= mInputEnd) {
int len = mInputPtr - start;
Expand All @@ -5160,12 +5160,12 @@ private int readAndWriteText(Writer w)
markLF();
} else if (c == '\r') {
char d;
if (mInputPtr >= mInputEnd) {
/* If we can't peek easily, let's flush past stuff
* and load more... (have to flush, since new read
* will overwrite inbut buffers)
*/
int len = mInputPtr - start;
final boolean atBoundary = (mInputPtr >= mInputEnd);
if (atBoundary) {
// If we can't peek easily, let's flush past stuff and load
// more... (have to flush, since new read will overwrite input buffers)
// 06-Dec-2019, tatu: [woodstox-core#97] Need to avoid copying \r tho:
int len = mInputPtr - start - 1;
if (len > 0) {
w.write(mInputBuffer, start, len);
count += len;
Expand All @@ -5177,24 +5177,33 @@ private int readAndWriteText(Writer w)
}
if (d == '\n') {
if (mNormalizeLFs) {
/* Let's flush content prior to 2-char LF, and
* start the new segment on the second char...
* this way, no mods are needed for the buffer,
* AND it'll also work on split 2-char lf!
*/
int len = mInputPtr - 2 - start;
// Let's flush content prior to 2-char LF, and start the new
// segment on the second char... this way, no mods are needed
// for the buffer, AND it'll also work on split 2-char lf!
int len = mInputPtr - start - 2;
if (len > 0) {
w.write(mInputBuffer, start, len);
count += len;
}
start = mInputPtr-1; // so '\n' is the first char
} else {
; // otherwise it's good as is
// otherwise it's good as is... almost
if (atBoundary) { // except, we don't want to lose that \r!
w.write(c);
}
}
} else { // not 2-char... need to replace?
// First: push back whatever non-linefeed we got:
--mInputPtr;
if (mNormalizeLFs) {
mInputBuffer[mInputPtr-1] = '\n';
// 06-Dec-2019, tatu: But beware [woodstox-core#97]
if (atBoundary) {
// If at boundary, no room to replace; must write single lf char
w.write(mNormalizeLFs ? '\n' : c);
++count;
} else { // but if not at boundary, can just replace lone '\r' if need be
if (mNormalizeLFs) { // replace \r with \n
mInputBuffer[mInputPtr-1] = '\n';
}
}
}
markLF();
Expand All @@ -5204,9 +5213,8 @@ private int readAndWriteText(Writer w)
} else if (c == '<') { // end is nigh!
break main_loop;
} else if (c == '&') {
/* Have to flush all stuff, since entities pretty much
* force it; input buffer won't be contiguous
*/
// Have to flush all stuff, since entities pretty much
// force it; input buffer won't be contiguous
int len = mInputPtr - 1 - start; // -1 to remove ampersand
if (len > 0) {
w.write(mInputBuffer, start, len);
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/stax2/wstream/BaseWriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public abstract class BaseWriterTest
public XMLStreamWriter2 getRepairingWriter(Writer w)
throws XMLStreamException
{
XMLOutputFactory f = getOutputFactory();
XMLOutputFactory2 f = getOutputFactory();
f.setProperty(XMLStreamProperties.XSP_NAMESPACE_AWARE, Boolean.TRUE);
f.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES,
Boolean.TRUE);
Expand All @@ -38,7 +38,7 @@ public XMLStreamWriter2 getRepairingWriter(Writer w, String enc)
public XMLStreamWriter2 getNonRepairingWriter(Writer w, boolean nsAware)
throws XMLStreamException
{
XMLOutputFactory f = getOutputFactory();
XMLOutputFactory2 f = getOutputFactory();
f.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES,
Boolean.FALSE);
f.setProperty(XMLStreamProperties.XSP_NAMESPACE_AWARE,
Expand Down
31 changes: 31 additions & 0 deletions src/test/java/stax2/wstream/TestCopyEventFromReader97.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package stax2.wstream;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import org.codehaus.stax2.XMLInputFactory2;
import org.codehaus.stax2.XMLOutputFactory2;
import org.codehaus.stax2.XMLStreamReader2;
import org.codehaus.stax2.XMLStreamWriter2;

public class TestCopyEventFromReader97
extends BaseWriterTest
{
// [woodstox-core#97]
public void testUTF8MsLinefeedCopyEvent() throws Exception
{
final XMLInputFactory2 xmlIn = getInputFactory();
final XMLOutputFactory2 xmlOut = getOutputFactory();
InputStream in = getClass().getResource("issue97.xml").openStream();

ByteArrayOutputStream bogus = new ByteArrayOutputStream();
XMLStreamReader2 reader = (XMLStreamReader2) xmlIn.createXMLStreamReader(in);
XMLStreamWriter2 writer = (XMLStreamWriter2) xmlOut.createXMLStreamWriter(bogus, "UTF-8");
while (reader.hasNext()) {
reader.next();
writer.copyEventFromReader(reader, false);
}

in.close();
}
}
1 change: 1 addition & 0 deletions src/test/resources/stax2/wstream/issue97.xml

Large diffs are not rendered by default.

0 comments on commit 489149b

Please sign in to comment.