From b68b6a74dddcda369fff1f25b80337a696fd2f92 Mon Sep 17 00:00:00 2001 From: Andrew Niefer Date: Fri, 17 Sep 2004 15:05:17 +0000 Subject: fix 74176 - [Scanner] ArrayIndexOutOfBoundsException scanning a string --- .../core/parser/tests/scanner2/Scanner2Test.java | 10 ++++++ .../internal/core/parser/scanner2/Scanner2.java | 36 ++++++++++++++++------ 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner2/Scanner2Test.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner2/Scanner2Test.java index 791b9743721..a282b71fdfa 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner2/Scanner2Test.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner2/Scanner2Test.java @@ -1807,5 +1807,15 @@ public class Scanner2Test extends BaseScanner2Test assertTrue( callback.problems.isEmpty() ); } + public void testBug74176() throws Exception{ + initializeScanner( "#define MYSTRING \"X Y Z " ); //$NON-NLS-1$ + validateEOF(); + + initializeScanner( "#define m(b) #"); //$NON-NLS-1$ + validateEOF(); + + initializeScanner( "#define m(foo,b) #b"); //$NON-NLS-1$ + validateEOF(); + } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/Scanner2.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/Scanner2.java index 94a758f3126..94fb98f8942 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/Scanner2.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/Scanner2.java @@ -1715,15 +1715,24 @@ public class Scanner2 implements IScanner, IScannerData { ++bufferPos[bufferStackPos]; //advances us to the # if( skipOverWhiteSpace() ) encounteredMultilineComment = true; - ++bufferPos[bufferStackPos]; //advances us past the # (or last whitespace) + boolean isArg = false; - for( int i = 0; i < arglist.length && arglist[i] != null; i++ ){ - if( CharArrayUtils.equals( buffer, bufferPos[bufferStackPos], arglist[i].length, arglist[i] ) ){ - isArg = true; - //advance us to the end of the arg - bufferPos[bufferStackPos] += arglist[i].length - 1; - break; - } + if( bufferPos[bufferStackPos] + 1 < limit ) + { + ++bufferPos[bufferStackPos]; //advances us past the # (or last whitespace) + for( int i = 0; i < arglist.length && arglist[i] != null; i++ ) + { + if( bufferPos[bufferStackPos] + arglist[i].length - 1 < limit ) + { + if( CharArrayUtils.equals( buffer, bufferPos[bufferStackPos], arglist[i].length, arglist[i] ) ) + { + isArg = true; + //advance us to the end of the arg + bufferPos[bufferStackPos] += arglist[i].length - 1; + break; + } + } + } } if( !isArg ) handleProblem( IProblem.PREPROCESSOR_MACRO_PASTING_ERROR, bufferPos[bufferStackPos], null ); @@ -2055,6 +2064,7 @@ public class Scanner2 implements IScanner, IScannerData { --bufferPos[bufferStackPos]; return encounteredMultiLineComment; } + --bufferPos[bufferStackPos]; return encounteredMultiLineComment; } @@ -2122,6 +2132,11 @@ public class Scanner2 implements IScanner, IScannerData { escaped = false; } } + //if we hit the limit here, then the outer while loop will advance + //us 2 past the end and we'll back up one and still be past the end, + //so back up here as well to leave us at the last char. + if( bufferPos[bufferStackPos] == bufferLimit[bufferStackPos] ) + bufferPos[bufferStackPos]--; break; case '\'': escaped = false; @@ -2141,10 +2156,13 @@ public class Scanner2 implements IScanner, IScannerData { escaped = false; } } + if( bufferPos[bufferStackPos] == bufferLimit[bufferStackPos] ) + bufferPos[bufferStackPos]--; + break; case '#' : if( stopAtPound ){ - if( buffer[ bufferPos[bufferStackPos] + 1] != '#' ){ + if( bufferPos[bufferStackPos] + 1 >= limit || buffer[ bufferPos[bufferStackPos] + 1] != '#' ){ --bufferPos[bufferStackPos]; return false; } -- cgit v1.2.3