tmf: Bug 436376: CustomXML Trace Parser Undefined behaviour on closely
authorPatrick Tasse <patrick.tasse@gmail.com>
Mon, 2 Jun 2014 21:27:59 +0000 (17:27 -0400)
committerPatrick Tasse <patrick.tasse@gmail.com>
Wed, 4 Jun 2014 17:16:37 +0000 (13:16 -0400)
The Custom XML parser now properly handles element names which are a
truncation of another element's name.

Change-Id: I5bbf7d4832976fc75d8e60a9ebc0e09d1463bd51
Signed-off-by: Patrick Tasse <patrick.tasse@gmail.com>
Reviewed-on: https://git.eclipse.org/r/27771
Tested-by: Hudson CI
Reviewed-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
Tested-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
(cherry picked from commit 33937d3c8a9f55f7f1505a6617cf978536a933ee)
Reviewed-on: https://git.eclipse.org/r/27853

org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/parsers/custom/CustomXmlTrace.java

index e300a79e89089a5bbd2c34a727492169f5997122..c6a333de186791326ed6f5318f620d67bce64d63 100644 (file)
@@ -148,11 +148,10 @@ public class CustomXmlTrace extends TmfTrace implements ITmfEventParser, ITmfPer
             } else if (location.getLocationInfo() instanceof Long) {
                 fFile.seek((Long) location.getLocationInfo());
             }
             } else if (location.getLocationInfo() instanceof Long) {
                 fFile.seek((Long) location.getLocationInfo());
             }
-            final String recordElementStart = "<" + fRecordInputElement.elementName; //$NON-NLS-1$
             long rawPos = fFile.getFilePointer();
             String line = fFile.getNextLine();
             while (line != null) {
             long rawPos = fFile.getFilePointer();
             String line = fFile.getNextLine();
             while (line != null) {
-                final int idx = line.indexOf(recordElementStart);
+                final int idx = indexOfElement(fRecordInputElement.elementName, line, 0);
                 if (idx != -1) {
                     context.setLocation(new TmfLongLocation(rawPos + idx));
                     return context;
                 if (idx != -1) {
                     context.setLocation(new TmfLongLocation(rawPos + idx));
                     return context;
@@ -256,12 +255,10 @@ public class CustomXmlTrace extends TmfTrace implements ITmfEventParser, ITmfPer
             event = extractEvent(element, fRecordInputElement);
             ((StringBuffer) event.getContent().getValue()).append(elementBuffer);
 
             event = extractEvent(element, fRecordInputElement);
             ((StringBuffer) event.getContent().getValue()).append(elementBuffer);
 
-
-            final String recordElementStart = "<" + fRecordInputElement.elementName; //$NON-NLS-1$
             long rawPos = fFile.getFilePointer();
             String line = fFile.getNextLine();
             while (line != null) {
             long rawPos = fFile.getFilePointer();
             String line = fFile.getNextLine();
             while (line != null) {
-                final int idx = line.indexOf(recordElementStart);
+                final int idx = indexOfElement(fRecordInputElement.elementName, line, 0);
                 if (idx != -1) {
                     context.setLocation(new TmfLongLocation(rawPos + idx));
                     return event;
                 if (idx != -1) {
                     context.setLocation(new TmfLongLocation(rawPos + idx));
                     return event;
@@ -319,6 +316,23 @@ public class CustomXmlTrace extends TmfTrace implements ITmfEventParser, ITmfPer
         return null;
     }
 
         return null;
     }
 
+    private static int indexOfElement(String elementName, String line, int fromIndex) {
+        final String recordElementStart = '<' + elementName;
+        int index = line.indexOf(recordElementStart, fromIndex);
+        if (index == -1) {
+            return index;
+        }
+        int nextCharIndex = index + recordElementStart.length();
+        if (nextCharIndex < line.length()) {
+            char c = line.charAt(nextCharIndex);
+            // Check that the match is not just a substring of another element
+            if (Character.isLetterOrDigit(c)) {
+                return indexOfElement(elementName, line, nextCharIndex);
+            }
+        }
+        return index;
+    }
+
     private void readElement(final StringBuffer buffer, final RandomAccessFile raFile) {
         try {
             int numRead = 0;
     private void readElement(final StringBuffer buffer, final RandomAccessFile raFile) {
         try {
             int numRead = 0;
@@ -521,11 +535,10 @@ public class CustomXmlTrace extends TmfTrace implements ITmfEventParser, ITmfPer
         }
         try (BufferedRandomAccessFile rafile = new BufferedRandomAccessFile(path, "r")) { //$NON-NLS-1$
             int lineCount = 0;
         }
         try (BufferedRandomAccessFile rafile = new BufferedRandomAccessFile(path, "r")) { //$NON-NLS-1$
             int lineCount = 0;
-            final String recordElementStart = "<" + fRecordInputElement.elementName; //$NON-NLS-1$
             long rawPos = 0;
             String line = rafile.getNextLine();
             while ((line != null) && (lineCount++ < MAX_LINES)) {
             long rawPos = 0;
             String line = rafile.getNextLine();
             while ((line != null) && (lineCount++ < MAX_LINES)) {
-                final int idx = line.indexOf(recordElementStart);
+                final int idx = indexOfElement(fRecordInputElement.elementName, line, 0);
                 if (idx != -1) {
                     rafile.seek(rawPos + idx + 1); // +1 is for the <
                     final StringBuffer elementBuffer = new StringBuffer("<"); //$NON-NLS-1$
                 if (idx != -1) {
                     rafile.seek(rawPos + idx + 1); // +1 is for the <
                     final StringBuffer elementBuffer = new StringBuffer("<"); //$NON-NLS-1$
This page took 0.026693 seconds and 5 git commands to generate.