Skip to content

Out of bounds error on incomple function default parameters #520

@the-horo

Description

@the-horo

Trying to parse the code void foo(int s = is causing an out-of-bounds error:

core.exception.ArrayIndexError@/home/happy/.dub/packages/libdparse/~master/libdparse/src/dparse/parser.d(9104): index [6] is out of bounds for array of length 6
----------------
??:? onArrayIndexError [0x7f0e1e4139be]
??:? _d_arraybounds_indexp [0x7f0e1e413fff]
/home/happy/.dub/packages/libdparse/~master/libdparse/src/dparse/parser.d:9104 const pure nothrow @property @nogc @safe std.experimental.lexer.TokenStructure!(ubyte, "import dparse.lexer:TokenTriviaFields,TriviaToken; mixin TokenTriviaFields;").TokenStructure dparse.parser.Parser.current() [0x565523b92951]
/home/happy/.dub/packages/libdparse/~master/libdparse/src/dparse/parser.d:7325 dparse.ast.TemplateValueParameterDefault dparse.parser.Parser.parseTemplateValueParameterDefault() [0x565523b8d125]
/home/happy/.dub/packages/libdparse/~master/libdparse/src/dparse/parser.d:7295 dparse.ast.TemplateValueParameter dparse.parser.Parser.parseTemplateValueParameter() [0x565523b8d026]
/home/happy/.dub/packages/libdparse/~master/libdparse/src/dparse/parser.d:7112 dparse.ast.TemplateParameter dparse.parser.Parser.parseTemplateParameter() [0x565523b8c56c]
/home/happy/.dub/packages/libdparse/~master/libdparse/src/dparse/parser.d:8821 dparse.ast.TemplateParameterList dparse.parser.Parser.parseCommaSeparatedRule!(dparse.ast.TemplateParameterList, dparse.ast.TemplateParameter, false).parseCommaSeparatedRule(bool) [0x565523b9afb3]
/home/happy/.dub/packages/libdparse/~master/libdparse/src/dparse/parser.d:7132 dparse.ast.TemplateParameterList dparse.parser.Parser.parseTemplateParameterList() [0x565523b8c61d]
/home/happy/.dub/packages/libdparse/~master/libdparse/src/dparse/parser.d:7148 dparse.ast.TemplateParameters dparse.parser.Parser.parseTemplateParameters() [0x565523b8c686]
/home/happy/.dub/packages/libdparse/~master/libdparse/src/dparse/parser.d:2669 dparse.ast.Declarator dparse.parser.Parser.parseDeclarator() [0x565523b7d7bc]
/home/happy/.dub/packages/libdparse/~master/libdparse/src/dparse/parser.d:8107 dparse.ast.VariableDeclaration dparse.parser.Parser.parseVariableDeclaration(dparse.ast.Type, bool) [0x565523b8fb17]
/home/happy/.dub/packages/libdparse/~master/libdparse/src/dparse/parser.d:2492 dparse.ast.Declaration dparse.parser.Parser.parseDeclaration(bool, bool, bool) [0x565523b7ca7e]
/home/happy/.dub/packages/libdparse/~master/libdparse/src/dparse/parser.d:5046 dparse.ast.Module dparse.parser.Parser.parseModule() [0x565523b85a51]
/home/happy/.dub/packages/libdparse/~master/libdparse/src/dparse/parser.d:71 dparse.ast.Module dparse.parser.parseModule!().parseModule(dparse.parser.ParserConfig) [0x565523b031c8]
/home/happy/.dub/packages/libdparse/~master/libdparse/src/dparse/parser.d:100 dparse.ast.Module dparse.parser.parseModule!().parseModule(const(std.experimental.lexer.TokenStructure!(ubyte, "import dparse.lexer:TokenTriviaFields,TriviaToken; mixin TokenTriviaFields;").TokenStructure)[], immutable(char)[], dparse.rollback_allocator.RollbackAllocator*, , uint*, uint*) [0x565523b0314d]
./a.d:23 _Dmain [0x565523aff767]
Error Program exited with code 1

The above stack trace is from the modified README example below:

/+dub.sdl:
dependency "libdparse" version="~master"
+/
import dparse.ast;
import std.stdio, std.range;

class TestVisitor : ASTVisitor {}

void main()
{
    import dparse.lexer;
    import dparse.parser : parseModule;
    import dparse.rollback_allocator : RollbackAllocator;

    auto sourceCode = q{
        void foo(int s =
    };
    LexerConfig config;
    auto cache = StringCache(StringCache.defaultBucketCount);
    auto tokens = getTokensForParser(sourceCode, config, &cache);

    RollbackAllocator rba;
    auto m = parseModule(tokens, "test.d", &rba);
    auto visitor = new TestVisitor();
    visitor.visit(m);
}

I hit this in serve-d when it crashed after I saved an incomplete buffer that contained the snippet.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions