Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SwiftParser crashes on string with nested interpolation #618

Closed
2 tasks done
benlangmuir opened this issue Aug 23, 2022 · 1 comment · Fixed by #637
Closed
2 tasks done

SwiftParser crashes on string with nested interpolation #618

benlangmuir opened this issue Aug 23, 2022 · 1 comment · Fixed by #637
Labels
bug Something isn't working good first issue Good for newcomers SwiftParser Bugs in the (new) Parser written in Swift

Comments

@benlangmuir
Copy link
Member

The parser is crashing on a string literal with nested interpolation. Found by running verify-round-trip on the source code for swift-argument-parser.

Reproduced with 38e3e4d.

$ cat str.swift
"text \(array.map({ "\($0)" }).joined(separator: ",")) text"

$ swift-parser-test verify-round-trip str.swift 
Swift/Collection.swift:711: Fatal error: Out of bounds: index >= endIndex
[1]    68834 trace trap  .build/debug/swift-parser-test verify-round-trip /tmp/str.swift

Checklist

  • If possible, I've reproduced the issue using the main branch of this package.
  • This issue hasn't been addressed in an existing GitHub issue.

Expected behavior

Should round-trip successfully.

Actual behavior

Crash

* thread #1, queue = 'com.apple.main-thread', stop reason = Fatal error: Out of bounds: index >= endIndex
  * frame #0: 0x00000001bc32a32c libswiftCore.dylib`::_swift_runtime_on_report(flags=1, message="Fatal error: Out of bounds: index >= endIndex", details=0x0000000000000000) at Errors.cpp:340:1 [opt]
    frame #1: 0x00000001bc32a330 libswiftCore.dylib`::_swift_reportToDebugger(flags=<unavailable>, message=<unavailable>, details=<unavailable>) at Errors.cpp:344:3 [opt] [artificial]
    frame #2: 0x00000001bc3bf8d8 libswiftCore.dylib`::_swift_stdlib_reportFatalErrorInFile(const unsigned char *, int, const unsigned char *, int, const unsigned char *, int, uint32_t, uint32_t) [inlined] logPrefixAndMessageToDebugger(prefix=<unavailable>, prefixLength=11, message="Out of bounds: index >= endIndex", messageLength=32) at Assert.cpp:39:3 [opt]
    frame #3: 0x00000001bc3bf88c libswiftCore.dylib`::_swift_stdlib_reportFatalErrorInFile(prefix=<unavailable>, prefixLength=11, message="Out of bounds: index >= endIndex", messageLength=32, file=<unavailable>, fileLength=<unavailable>, line=<unavailable>, flags=<unavailable>) at Assert.cpp:62:3 [opt]
    frame #4: 0x00000001bbfe31b0 libswiftCore.dylib`closure #1 in closure #1 in closure #1 in _assertionFailure(file=<unavailable>, prefix=<unavailable>, message=<unavailable>, line=<unavailable>, flags=<unavailable>) at AssertCommon.swift:91:9 [opt]
    frame #5: 0x00000001bbfe2ef4 libswiftCore.dylib`closure #1 in closure #1 in _assertionFailure(message=<unavailable>, file=<unavailable>, prefix=<unavailable>, line=<unavailable>, flags=1) at AssertCommon.swift:0 [opt]
    frame #6: 0x00000001bbfe2cec libswiftCore.dylib`closure #1 in _assertionFailure(prefix=<unavailable>, message=<unavailable>, file=<unavailable>, line=711, flags=1) at AssertCommon.swift:0 [opt]
    frame #7: 0x00000001bbfe284c libswiftCore.dylib`_assertionFailure(prefix=<unavailable>, message=<unavailable>, file=<unavailable>, line=711, flags=1) at AssertCommon.swift:85:10 [opt]
    frame #8: 0x00000001bc2c3e60 libswiftCore.dylib`specialized Collection._failEarlyRangeCheck(index=<unavailable>, bounds=<unavailable>) at Collection.swift:0 [opt]
    frame #9: 0x00000001002f3eb0 swift-parser-test`protocol witness for Collection._failEarlyRangeCheck(_:bounds:) in conformance SyntaxText at <compiler-generated>:0
    frame #10: 0x00000001bbfb7e34 libswiftCore.dylib`RandomAccessCollection<>.index(i=50, self=SwiftSyntax.SyntaxText @ 0x000000016fdfa878) at RandomAccessCollection.swift:296:5 [opt]
    frame #11: 0x00000001002f3f18 swift-parser-test`protocol witness for Collection.index(after:) in conformance SyntaxText at <compiler-generated>:0
    frame #12: 0x00000001bc176a08 libswiftCore.dylib`Slice.index(i=<unavailable>, self=<unavailable>) at Slice.swift:180:18 [opt]
    frame #13: 0x00000001bc034c08 libswiftCore.dylib`ClosedRange.relative<Bound>(collection=<unavailable>, self=7...50) at ClosedRange.swift:114:27 [opt]
    frame #14: 0x00000001bc0d91d8 libswiftCore.dylib`Collection.subscript.getter(r=<unavailable>, self=Swift.Slice<SwiftSyntax.SyntaxText> @ 0x000000016fdfa868) at Range.swift:934:19 [opt]
    frame #15: 0x00000001000f24dc swift-parser-test`Parser.parseStringLiteralSegments(text=Swift.Slice<SwiftSyntax.SyntaxText> @ 0x000000016fdfb010, closer=SwiftSyntax.RawTokenSyntax @ 0x000000016fdfb008, delimiter=SwiftSyntax.SyntaxText @ 0x000000016fdfaff8, self=SwiftParser.Parser @ 0x000000016fdfed50) at Expressions.swift:1167:58
    frame #16: 0x00000001000edff4 swift-parser-test`Parser.parseStringLiteral(self=SwiftParser.Parser @ 0x000000016fdfed50) at Expressions.swift:927:39
    frame #17: 0x00000001000e9154 swift-parser-test`Parser.parsePrimaryExpression(flavor=trailingClosure, self=SwiftParser.Parser @ 0x000000016fdfed50) at Expressions.swift:716:33
    frame #18: 0x00000001000e8ec8 swift-parser-test`Parser.parsePostfixExpression(flavor=trailingClosure, forDirective=false, self=SwiftParser.Parser @ 0x000000016fdfed50) at Expressions.swift:368:21
    frame #19: 0x00000001000e86e0 swift-parser-test`Parser.parseUnaryExpression(flavor=trailingClosure, forDirective=false, self=SwiftParser.Parser @ 0x000000016fdfed50) at Expressions.swift:339:19
    frame #20: 0x00000001000e7ea8 swift-parser-test`Parser.parseSequenceExpressionElement(flavor=trailingClosure, forDirective=false, self=SwiftParser.Parser @ 0x000000016fdfed50) at Expressions.swift:284:19
    frame #21: 0x00000001000e5c64 swift-parser-test`Parser.parseSequenceExpression(flavor=trailingClosure, forDirective=false, inVarOrLet=false, self=SwiftParser.Parser @ 0x000000016fdfed50) at Expressions.swift:94:24
    frame #22: 0x00000001000e59e8 swift-parser-test`Parser.parseExpression(flavor=trailingClosure, self=SwiftParser.Parser @ 0x000000016fdfed50) at Expressions.swift:40:31
    frame #23: 0x0000000100134ca0 swift-parser-test`Parser.parseItem(self=SwiftParser.Parser @ 0x000000016fdfed50) at TopLevel.swift:168:29
    frame #24: 0x0000000100133330 swift-parser-test`Parser.parseCodeBlockItem(self=SwiftParser.Parser @ 0x000000016fdfed50) at TopLevel.swift:117:21
    frame #25: 0x000000010013321c swift-parser-test`Parser.parseTopLevelCodeBlockItems(self=SwiftParser.Parser @ 0x000000016fdfed50) at TopLevel.swift:44:28
    frame #26: 0x0000000100132fe0 swift-parser-test`Parser.parseSourceFile(self=SwiftParser.Parser @ 0x000000016fdfed50) at TopLevel.swift:28:22
    frame #27: 0x000000010011e474 swift-parser-test`closure #1 in static Parser.parse(parser=SwiftParser.Parser @ 0x000000016fdfed50) at Parser.swift:45:35
    frame #28: 0x000000010011e664 swift-parser-test`partial apply for closure #1 in static Parser.parse(source:parseTransition:filenameForDiagnostics:languageVersion:enableBareSlashRegexLiteral:) at <compiler-generated>:0
    frame #29: 0x00000001bc10d8ac libswiftCore.dylib`withExtendedLifetime<T, Result>(x=<unavailable>, body=<unavailable>) at LifetimeManager.swift:27:14 [opt]
    frame #30: 0x000000010011dd14 swift-parser-test`static Parser.parse(source=60 values (0x600002600320), parseTransition=nil, filenameForDiagnostics="", languageVersion=nil, enableBareSlashRegexLiteral=nil, self=SwiftParser.Parser) at Parser.swift:44:12
    frame #31: 0x000000010011d9f0 swift-parser-test`closure #1 in static Parser.parse(buffer=60 values (0x600002600320), self=SwiftParser.Parser, parseTransition=nil, filenameForDiagnostics="", languageVersion=nil, enableBareSlashRegexLiteral=nil) at Parser.swift:26:18
    frame #32: 0x000000010011dacc swift-parser-test`partial apply for closure #1 in static Parser.parse(source:parseTransition:filenameForDiagnostics:languageVersion:enableBareSlashRegexLiteral:) at <compiler-generated>:0
    frame #33: 0x000000010011dfe0 swift-parser-test`String.withUTF8<R>(_:) at <compiler-generated>:0
    frame #34: 0x000000010011d81c swift-parser-test`static Parser.parse(source="\"text \\(array.map({ \"\\($0)\" }).joined(separator: \",\")) text\"", parseTransition=nil, filenameForDiagnostics="", languageVersion=nil, enableBareSlashRegexLiteral=nil, self=SwiftParser.Parser) at Parser.swift:25:23
    frame #35: 0x0000000100ded118 swift-parser-test`static VerifyRoundTrip.run(source="\"text \\(array.map({ \"\\($0)\" }).joined(separator: \",\")) text\"", swiftVersion=nil, enableBareSlashRegex=nil, self=swift_parser_test.VerifyRoundTrip) at swift-parser-test.swift:102:27
    frame #36: 0x0000000100decff4 swift-parser-test`VerifyRoundTrip.run(self=0x0000600002600480) at swift-parser-test.swift:98:14
    frame #37: 0x0000000100dee198 swift-parser-test`protocol witness for ParsableCommand.run() in conformance VerifyRoundTrip at <compiler-generated>:0
    frame #38: 0x000000010004166c swift-parser-test`static ParsableCommand.main(arguments=nil, self=swift_parser_test.SwiftParserTest) at ParsableCommand.swift:97:19
    frame #39: 0x0000000100041784 swift-parser-test`static ParsableCommand.main(self=swift_parser_test.SwiftParserTest) at ParsableCommand.swift:107:10
    frame #40: 0x0000000100deb344 swift-parser-test`static SwiftParserTest.$main(self=swift_parser_test.SwiftParserTest) at swift-parser-test.swift:56:1
    frame #41: 0x0000000100df8204 swift-parser-test`swift_parser_test_main at <compiler-generated>:0
    frame #42: 0x00000001020e508c dyld`start + 520
@benlangmuir benlangmuir added the bug Something isn't working label Aug 23, 2022
@ahoppen ahoppen added the SwiftParser Bugs in the (new) Parser written in Swift label Aug 23, 2022
@ahoppen
Copy link
Collaborator

ahoppen commented Aug 23, 2022

rdar://99025800

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers SwiftParser Bugs in the (new) Parser written in Swift
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants