Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/main/java/org/apache/xmlbeans/impl/store/Saver.java
Original file line number Diff line number Diff line change
Expand Up @@ -2106,10 +2106,11 @@ protected void emitCommentText(SaveCur c) {
int cch = c._cchSrc;
int off = c._offSrc;
int index = 0;
boolean lastWasDash = false;
while (index < cch) {
int indexLimit = Math.min(index + 512, cch);
CharUtil.getChars(_buf, 0, src, off + index, indexLimit - index);
entitizeAndWriteCommentText(indexLimit - index);
lastWasDash = entitizeAndWriteCommentText(indexLimit - index, lastWasDash, indexLimit == cch);
index = indexLimit;
}
}
Expand Down Expand Up @@ -2156,9 +2157,7 @@ private int entitizeAndWriteText(int bufLimit, int trailingBrackets) {
return trailingBrackets;
}

private void entitizeAndWriteCommentText(int bufLimit) {
boolean lastWasDash = false;

private boolean entitizeAndWriteCommentText(int bufLimit, boolean lastWasDash, boolean lastChunk) {
for (int i = 0; i < bufLimit; i++) {
char ch = _buf[i];

Expand All @@ -2181,11 +2180,15 @@ private void entitizeAndWriteCommentText(int bufLimit) {
}
}

if (_buf[bufLimit - 1] == '-') {
// A trailing '-' would form "--->" with the closing delimiter, so it
// is escaped only at the real end of the comment, not on every chunk.
if (lastChunk && _buf[bufLimit - 1] == '-') {
_buf[bufLimit - 1] = ' ';
lastWasDash = false;
}

emit(_buf, 0, bufLimit);
return lastWasDash;
}

private boolean entitizeAndWritePIText(int bufLimit, boolean lastWasQuestion) {
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/misc/checkin/SaveOptimizeForSpeedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,19 @@ void testBadCharInText() throws Exception {
XmlObject.Factory.parse(out);
}

@Test
void testCommentDashAtChunkBoundary() throws Exception {
// a lone '-' sitting on the 512-char chunk boundary. It is legal in
// comment content, but the speed path applied its trailing-dash fixup
// at the end of every chunk, so the boundary '-' was silently rewritten
// to a space and the saved comment no longer matched the source.
String data = repeat('a', 511) + "-" + repeat('b', 600);
XmlObject o = XmlObject.Factory.parse("<root><!--" + data + "--></root>");
String out = saveForSpeed(o);
XmlObject.Factory.parse(out);
assertTrue(out.contains(repeat('a', 511) + "-"));
}

@Test
void testProcInstTerminatorAcrossChunkBoundary() throws Exception {
// a '?>' that straddles the 512-char chunk boundary: '?' is the last char
Expand Down