Author: fanningpj
Date: Wed Jul 15 22:29:01 2026
New Revision: 1936180

Log:
escape comment trailing dash only on the final chunk. Thanks to aizu-m. This 
closes #78

Modified:
   xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/store/Saver.java
   xmlbeans/trunk/src/test/java/misc/checkin/SaveOptimizeForSpeedTest.java

Modified: xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/store/Saver.java
==============================================================================
--- xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/store/Saver.java      
Wed Jul 15 22:23:43 2026        (r1936179)
+++ xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/store/Saver.java      
Wed Jul 15 22:29:01 2026        (r1936180)
@@ -2106,10 +2106,11 @@ abstract class Saver {
             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;
             }
         }
@@ -2156,9 +2157,7 @@ abstract class Saver {
             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];
 
@@ -2181,11 +2180,15 @@ abstract class Saver {
                 }
             }
 
-            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) {

Modified: 
xmlbeans/trunk/src/test/java/misc/checkin/SaveOptimizeForSpeedTest.java
==============================================================================
--- xmlbeans/trunk/src/test/java/misc/checkin/SaveOptimizeForSpeedTest.java     
Wed Jul 15 22:23:43 2026        (r1936179)
+++ xmlbeans/trunk/src/test/java/misc/checkin/SaveOptimizeForSpeedTest.java     
Wed Jul 15 22:29:01 2026        (r1936180)
@@ -116,6 +116,19 @@ public class SaveOptimizeForSpeedTest {
     }
 
     @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
         // of the first chunk, '>' the first char of the second. The per-chunk


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to