1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-24 01:15:29 +02:00

Fix for bug 126136 - Out of memory error on recursive macro

This commit is contained in:
John Camelon 2006-06-12 01:08:32 +00:00
parent 52d99fabef
commit 077fbb67ae
2 changed files with 49 additions and 8 deletions

View file

@ -355,7 +355,7 @@ public class Scanner2Test extends BaseScanner2Test
"#else\n" + //$NON-NLS-1$
"bar\n" + //$NON-NLS-1$
"#endif\n" //$NON-NLS-1$
); //$NON-NLS-1$
);
validateIdentifier("foo"); //$NON-NLS-1$
validateEOF();
initializeScanner(
@ -364,7 +364,7 @@ public class Scanner2Test extends BaseScanner2Test
"#else\n" + //$NON-NLS-1$
"bar\n" + //$NON-NLS-1$
"#endif\n" //$NON-NLS-1$
); //$NON-NLS-1$
);
validateIdentifier("foo"); //$NON-NLS-1$
validateEOF();
}
@ -2380,7 +2380,7 @@ public class Scanner2Test extends BaseScanner2Test
validateToken( IToken.tRPAREN );
validateToken( IToken.tLBRACE );
validateToken( IToken.t_return );
validateInteger( "0" );
validateInteger( "0" ); //$NON-NLS-1$
validateToken( IToken.tSEMI );
validateToken( IToken.tRBRACE );
validateEOF();
@ -2400,10 +2400,18 @@ public class Scanner2Test extends BaseScanner2Test
validateToken( IToken.tRPAREN );
validateToken( IToken.tLBRACE );
validateToken( IToken.t_return );
validateInteger( "0" );
validateInteger( "0" ); //$NON-NLS-1$
validateToken( IToken.tSEMI );
validateToken( IToken.tRBRACE );
validateEOF();
}
public void testBug126136() throws Exception {
StringBuffer buffer = new StringBuffer("#define C C\n"); //$NON-NLS-1$
buffer.append("#if !C\n"); //$NON-NLS-1$
buffer.append("true\n"); //$NON-NLS-1$
buffer.append("#endif\n"); //$NON-NLS-1$
initializeScanner(buffer.toString(), ParserLanguage.CPP);
fullyTokenize();
}
}

View file

@ -660,8 +660,24 @@ abstract class BaseScanner implements IScanner {
} else if (expObject instanceof ObjectStyleMacro) {
ObjectStyleMacro expMacro = (ObjectStyleMacro) expObject;
char[] expText = expMacro.getExpansion();
if (expText.length > 0)
pushContext(expText, expMacro);
if (expText.length > 0 )
{
if (shouldExpandMacro(expMacro, bufferStackPos, bufferData, -1, bufferPos, bufferStack ))
pushContext(expText, new MacroData(start, start + len, expMacro));
else
{
if (len == 1) { // is a character
tokenType = tCHAR;
return;
}
// undefined macro, assume 0
tokenValue = 0;
tokenType = tNUMBER;
return;
}
}
} else if (expObject instanceof char[]) {
char[] expText = (char[]) expObject;
if (expText.length > 0)
@ -2135,9 +2151,14 @@ abstract class BaseScanner implements IScanner {
* @return
*/
protected boolean shouldExpandMacro(IMacro macro) {
return shouldExpandMacro(macro, bufferStackPos, bufferData, offsetBoundary, bufferPos, bufferStack);
}
protected static boolean shouldExpandMacro(IMacro macro, int bufferStackPos, Object [] bufferData, int offsetBoundary, int [] bufferPos, char [][]bufferStack )
{
// but not if it has been expanded on the stack already
// i.e. recursion avoidance
if (macro != null && !isLimitReached())
if (macro != null && !isLimitReached(offsetBoundary, bufferStackPos, bufferPos, bufferStack ))
for (int stackPos = bufferStackPos; stackPos >= 0; --stackPos)
if (bufferData[stackPos] != null
&& bufferData[stackPos] instanceof MacroData
@ -2146,13 +2167,24 @@ abstract class BaseScanner implements IScanner {
.getName())) {
return false;
}
return true;
return true;
}
/**
* @return
*/
protected final boolean isLimitReached() {
return isLimitReached(offsetBoundary, bufferStackPos, bufferPos, bufferStack);
}
/**
* @param offsetBoundary
* @param bufferStackPos
* @param bufferPos
* @param bufferStack
* @return
*/
protected final static boolean isLimitReached(int offsetBoundary, int bufferStackPos, int [] bufferPos, char [][]bufferStack ) {
if (offsetBoundary == -1 || bufferStackPos != 0)
return false;
if (bufferPos[bufferStackPos] == offsetBoundary - 1)
@ -2165,6 +2197,7 @@ abstract class BaseScanner implements IScanner {
return false;
}
protected IToken scanString() {
char[] buffer = bufferStack[bufferStackPos];