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

Exception in -[Swift.__StringStorage substringWithRange:]: #3761

Closed
2 tasks done
BalestraPatrick opened this issue Nov 22, 2021 · 8 comments · Fixed by #3776
Closed
2 tasks done

Exception in -[Swift.__StringStorage substringWithRange:]: #3761

BalestraPatrick opened this issue Nov 22, 2021 · 8 comments · Fixed by #3776

Comments

@BalestraPatrick
Copy link
Contributor

New Issue Checklist

Describe the bug

SwiftLint crashes with the following exception:

2021-11-22 11:51:08.610 swiftlint[89491:27814178] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[Swift.__StringStorage substringWithRange:]: Range {799, 18446744073709551611} out of bounds; string length 2101'
*** First throw call stack:
(
	0   CoreFoundation                      0x00007ff813ece62b __exceptionPreprocess + 242
	1   libobjc.A.dylib                     0x00007ff813c30dce objc_exception_throw + 48
	2   Foundation                          0x00007ff814bac8f5 -[NSString substringWithRange:] + 223
	3   swiftlint                           0x00000001025e89ac $s21SourceKittenFramework10StringViewV22substringWithByteRangeySSSgAA0hI0VF + 156
	4   swiftlint                           0x000000010284572c $s18SwiftLintFramework14AttributesRuleV26attributeContainsParameter031_DE7F7E58C782210DC97A8A1A7663B2N0LL0F5Range4line4fileSb012SourceKittenC004ByteR0V_AI4LineVAA0aB4FileCtFTf4nnnd_n + 124
	5   swiftlint                           0x0000000102846ba5 $s18SwiftLintFramework14AttributesRuleV27attributesFromPreviousLines031_DE7F7E58C782210DC97A8A1A7663B2O0LL10lineNumber4fileSaySS_SbtGSi_AA0aB4FileCtKFTf4nnd_n + 5013
	6   swiftlint                           0x00000001028447dd $s18SwiftLintFramework14AttributesRuleV11isViolation031_DE7F7E58C782210DC97A8A1A7663B2M0LL10lineNumber4file27attributeShouldBeOnSameLineSbSi_AA0aB4FileCSbtF + 2109
	7   swiftlint                           0x0000000102847bbb $s18SwiftLintFramework14AttributesRuleV12validateKind031_DE7F7E58C782210DC97A8A1A7663B2M0LL4file27attributeShouldBeOnSameLine10dictionarySayAA14StyleViolationVGAA0aB4FileC_SbAA22SourceKittenDictionaryVtFTf4nnxn_n + 171
	8   swiftlint                           0x0000000102845550 $s18SwiftLintFramework7ASTRulePAAE8validate4file10dictionarySayAA14StyleViolationVGAA0aB4FileC_AA22SourceKittenDictionaryVtFAISgAMXEfU_AA14AttributesRuleV_TG5TA + 48
	9   swiftlint                           0x00000001026a7ab5 $s18SwiftLintFramework22SourceKittenDictionaryV18traverseDepthFirst33_0EBA09E17623EAE379194D9431F0C722LL20collectingValuesInto0G5BlockySayxGz_AHSgACXEtlFAA14StyleViolationV_Tg5Tf4nnx_n + 197
	10  swiftlint                           0x00000001026a7aaa $s18SwiftLintFramework22SourceKittenDictionaryV18traverseDepthFirst33_0EBA09E17623EAE379194D9431F0C722LL20collectingValuesInto0G5BlockySayxGz_AHSgACXEtlFAA14StyleViolationV_Tg5Tf4nnx_n + 186
	11  swiftlint                           0x0000000102843f1b $s18SwiftLintFramework14AttributesRuleV8validate4fileSayAA14StyleViolationVGAA0aB4FileC_tF + 123
	12  swiftlint                           0x000000010265e36b $s18SwiftLintFramework4RulePAAE4lint33_5A5F37FCDEE0B4D78BAE05A90BA8A062LL4file7regions9benchmark7storage13configuration025superfluousDisableCommandD017compilerArgumentsAA0B6ResultAELLVSgAA0aB4FileC_SayAA6RegionVGSbAA0D7StorageCAA13ConfigurationVAA011SuperfluousuvD0VSgSaySSGtFTf4nnnndnnn_n + 507
	13  swiftlint                           0x000000010265dff1 $s18SwiftLintFramework15CollectedLinterV18getStyleViolations33_5A5F37FCDEE0B4D78BAE05A90BA8A062LL5using9benchmarkSayAA0G9ViolationVG_SaySS2id_Sd4timetGtAA11RuleStorageC_SbtFAA0B6ResultAELLVSgAA0W0_pXEfU0_TA + 113
	14  swiftlint                           0x000000010265e041 $s18SwiftLintFramework4Rule_pAA0B6Result33_5A5F37FCDEE0B4D78BAE05A90BA8A062LLVSgIgno_AaB_pAFIegnr_TRTA + 33
	15  swiftlint                           0x00000001026346dd $sSa18SwiftLintFrameworkE11parallelMap9transformSayqd__Gqd__xXE_tlFADSryqd__SgGzXEfU_ySiXEfU_AA4Rule_p_AA0B6Result33_5A5F37FCDEE0B4D78BAE05A90BA8A062LLVSgTg5 + 77
	16  libswiftDispatch.dylib              0x00007ff826cb5171 $sSiIgy_SiIegy_TRTA + 17
	17  libswiftDispatch.dylib              0x00007ff826cb5194 $sSiIegy_SiIyBy_TR + 20
	18  libdispatch.dylib                   0x00007ff813bd3cfc _dispatch_client_callout2 + 8
	19  libdispatch.dylib                   0x00007ff813be6110 _dispatch_apply_serial + 160
	20  libdispatch.dylib                   0x00007ff813bd3cc9 _dispatch_client_callout + 8
	21  libdispatch.dylib                   0x00007ff813bd7fa1 _dispatch_sync_function_invoke + 54
	22  libdispatch.dylib                   0x00007ff813be5add _dispatch_apply_with_attr_f + 1334
	23  libswiftDispatch.dylib              0x00007ff826cb5232 _swift_dispatch_apply_current + 100
	24  libswiftDispatch.dylib              0x00007ff826cb50ec $sSo17OS_dispatch_queueC8DispatchE17concurrentPerform10iterations7executeySi_ySiXEtFZ + 156
	25  swiftlint                           0x000000010263450e $sSa18SwiftLintFrameworkE11parallelMap9transformSayqd__Gqd__xXE_tlFADSryqd__SgGzXEfU_AA4Rule_p_AA0B6Result33_5A5F37FCDEE0B4D78BAE05A90BA8A062LLVSgTg5 + 78
	26  swiftlint                           0x0000000102633ec6 $sSa18SwiftLintFrameworkE11parallelMap9transformSayqd__Gqd__xXE_tlFAA4Rule_p_AA0B6Result33_5A5F37FCDEE0B4D78BAE05A90BA8A062LLVSgTg5 + 150
	27  swiftlint                           0x0000000102634066 $sSa18SwiftLintFrameworkE18parallelCompactMap9transformSayqd__Gqd__SgxXE_tlFAA4Rule_p_AA0B6Result33_5A5F37FCDEE0B4D78BAE05A90BA8A062LLVTg5 + 22
	28  swiftlint                           0x0000000102659776 $s18SwiftLintFramework15CollectedLinterV18getStyleViolations33_5A5F37FCDEE0B4D78BAE05A90BA8A062LL5using9benchmarkSayAA0G9ViolationVG_SaySS2id_Sd4timetGtAA11RuleStorageC_SbtF + 854
	29  swiftlint                           0x00000001026593fd $s18SwiftLintFramework15CollectedLinterV15styleViolations5usingSayAA14StyleViolationVGAA11RuleStorageC_tF + 13
	30  swiftlint                           0x00000001029ab1bb $s9swiftlint20LintOrAnalyzeCommandV17collectViolations33_50703DD9A63FFC386F0BA0D6C49778EBLL7builders6ResultOySay05SwiftB9Framework0rB4FileCGAA0rB5ErrorOGAA0bcdQ7BuilderAELLC_tFZyAI15CollectedLinterVcfU_ + 107
	31  swiftlint                           0x000000010299e691 $s18SwiftLintFramework13ConfigurationV9swiftlintE5visit33_30E847BB8C9F32DCEAC767137A79F9B7LL7linters7visitor7storage18duplicateFileNamesSayAA0abS0CGSayAA15CollectedLinterVG_AD20LintableFilesVisitorVAA11RuleStorageCShySSGtFAlOcfU_yyXEfU0_ + 417
	32  swiftlint                           0x00000001029a2bb1 $ss5Error_pIgzo_ytsAA_pIegrzo_TRTA + 17
	33  swiftlint                           0x00000001029a3759 $ss5Error_pIgzo_ytsAA_pIegrzo_TRTA.5 + 9
	34  libswiftObjectiveC.dylib            0x00007ff828fe1e6c $s10ObjectiveC15autoreleasepool8invokingxxyKXE_tKlF + 44
	35  swiftlint                           0x000000010299e29b $s18SwiftLintFramework13ConfigurationV9swiftlintE5visit33_30E847BB8C9F32DCEAC767137A79F9B7LL7linters7visitor7storage18duplicateFileNamesSayAA0abS0CGSayAA15CollectedLinterVG_AD20LintableFilesVisitorVAA11RuleStorageCShySSGtFAlOcfU_ + 667
	36  swiftlint                           0x00000001029a2d89 $s18SwiftLintFramework15CollectedLinterVAA0aB4FileCIggo_AcEIegnr_TRTA + 57
	37  swiftlint                           0x0000000102999da9 $sSa9swiftlintE11parallelMap9transformSayqd__Gqd__xXE_tlFySryqd__Gz_SiztXEfU_ySiXEfU_18SwiftLintFramework15CollectedLinterV_AF0eF4FileCTg5 + 649
	38  libswiftDispatch.dylib              0x00007ff826cb5171 $sSiIgy_SiIegy_TRTA + 17
	39  libswiftDispatch.dylib              0x00007ff826cb5194 $sSiIegy_SiIyBy_TR + 20
	40  libdispatch.dylib                   0x00007ff813bd3cfc _dispatch_client_callout2 + 8
	41  libdispatch.dylib                   0x00007ff813be64bc _dispatch_apply_invoke_and_wait + 213
	42  libdispatch.dylib                   0x00007ff813be5a42 _dispatch_apply_with_attr_f + 1179
	43  libswiftDispatch.dylib              0x00007ff826cb5232 _swift_dispatch_apply_current + 100
	44  libswiftDispatch.dylib              0x00007ff826cb50ec $sSo17OS_dispatch_queueC8DispatchE17concurrentPerform10iterations7executeySi_ySiXEtFZ + 156
	45  swiftlint                           0x00000001029a23d2 $sSa28_unsafeUninitializedCapacity16initializingWithSayxGSi_ySryxGz_SiztKXEtKcfC18SwiftLintFramework0fG4FileC_Tg5079$sSa9swiftlintE11parallelMap9transformSayqd__Gqd__xXE_tlFySryqd__Gz_SiztXEfU_18fg34Framework15CollectedLinterV_AF0eF4I4CTg5SayAE0xY0VGxq_r0_lyAjGIsgnr_Tf1ncn_nTf4nngn_n + 194
	46  swiftlint                           0x00000001029a27f2 $s18SwiftLintFramework13ConfigurationV9swiftlintE5visit33_30E847BB8C9F32DCEAC767137A79F9B7LL7linters7visitor7storage18duplicateFileNamesSayAA0abS0CGSayAA15CollectedLinterVG_AD20LintableFilesVisitorVAA11RuleStorageCShySSGtFTf4nndnn_n + 1010
	47  swiftlint                           0x000000010299d342 $s18SwiftLintFramework13ConfigurationV9swiftlintE18visitLintableFiles4with7storages6ResultOySayAA0aB4FileCGAD0aB5ErrorOGAD0gH7VisitorV_AA11RuleStorageCtFALSayAA15CollectedLinterVG_ShySSGtXEfU4_ + 258
	48  swiftlint                           0x000000010299edbe $sSay18SwiftLintFramework15CollectedLinterVGShySSGSayAA0aB4FileCGIgggo_AD_AEtAHIegnr_TRTA + 30
	49  libswiftCore.dylib                  0x00007ff8218611f3 $ss6ResultO3mapyAByqd__q_Gqd__xXElF + 227
	50  swiftlint                           0x000000010299ab79 $s18SwiftLintFramework13ConfigurationV9swiftlintE18visitLintableFiles4with7storages6ResultOySayAA0aB4FileCGAD0aB5ErrorOGAD0gH7VisitorV_AA11RuleStorageCtF + 1017
	51  swiftlint                           0x000000010299ec72 $s18SwiftLintFramework13ConfigurationV9swiftlintE18visitLintableFiles7options5cache7storage12visitorBlocks6ResultOySayAA0aB4FileCGAD0aB5ErrorOGAD0B16OrAnalyzeOptionsV_AA11LinterCacheCSgAA11RuleStorageCyAA09CollectedT0VctFAqD0gH7VisitorVXEfU_ + 18
	52  swiftlint                           0x00000001029ad41d $s9swiftlint20LintableFilesVisitorVs6ResultOySay18SwiftLintFramework0fG4FileCGAA0fG5ErrorOGIggo_AcLIegnr_TRTA + 45
	53  swiftlint                           0x00000001029af659 $s9swiftlint20LintableFilesVisitorVs6ResultOySay18SwiftLintFramework0fG4FileCGAA0fG5ErrorOGIggo_AcLIegnr_TRTA.8 + 9
	54  libswiftCore.dylib                  0x00007ff82186145f $ss6ResultO7flatMapyAByqd__q_GADxXElF + 207
	55  swiftlint                           0x00000001029ab0c6 $s9swiftlint20LintOrAnalyzeCommandV17collectViolations33_50703DD9A63FFC386F0BA0D6C49778EBLL7builders6ResultOySay05SwiftB9Framework0rB4FileCGAA0rB5ErrorOGAA0bcdQ7BuilderAELLC_tFZ + 710
	56  swiftlint                           0x00000001029aa44f $s9swiftlint20LintOrAnalyzeCommandV3runys6ResultOyytAA05SwiftB5ErrorOGAA0bcD7OptionsVFZ + 287
	57  swiftlint                           0x000000010298f7e5 $s9swiftlint9SwiftLintV0C0V3runyyKF + 8773
	58  swiftlint                           0x000000010298fc19 $s9swiftlint9SwiftLintV0C0V14ArgumentParser15ParsableCommandAafGP3runyyKFTW + 9
	59  swiftlint                           0x0000000102536d3e $s14ArgumentParser15ParsableCommandPAAE4mainyySaySSGSgFZ + 78
	60  swiftlint                           0x000000010299871d $s9swiftlint9SwiftLintV30mainHandlingDeprecatedCommandsyySaySSGSgFZTf4nd_n + 349
	61  swiftlint                           0x0000000102984cab $s9swiftlintyycfU_ + 11
	62  swiftlint                           0x0000000102636ece $sIeg_IeyB_TR + 14
	63  libdispatch.dylib                   0x00007ff813bd2ad8 _dispatch_call_block_and_release + 12
	64  libdispatch.dylib                   0x00007ff813bd3cc9 _dispatch_client_callout + 8
	65  libdispatch.dylib                   0x00007ff813bd63bf _dispatch_queue_override_invoke + 801
	66  libdispatch.dylib                   0x00007ff813be33ba _dispatch_root_queue_drain + 343
	67  libdispatch.dylib                   0x00007ff813be3b5a _dispatch_worker_thread2 + 160
	68  libsystem_pthread.dylib             0x00007ff813d8a049 _pthread_wqthread + 256
	69  libsystem_pthread.dylib             0x00007ff813d8901b start_wqthread + 15
)
libc++abi: terminating with uncaught exception of type NSException

Environment

  • SwiftLint version (run swiftlint version to be sure)? 0.45.0
  • Installation method used (Homebrew, CocoaPods, building from source, etc)? binary
  • Paste your configuration file:
    I can reproduce it on our codebase with this:
opt_in_rules:
  - attributes
  • Are you using nested configurations?
    No
  • Which Xcode version are you using (check xcodebuild -version)? Xcode 13.0
  • Do you have a sample that shows the issue? Run echo "[string here]" | swiftlint lint --no-cache --use-stdin --enable-all-rules
    to quickly test if your example is really demonstrating the issue. If your example is more
    complex, you can use swiftlint lint --path [file here] --no-cache --enable-all-rules. No, not sure how to exactly narrow this down. Is there a debug mode that shows exactly on which string swiftlint is crashing?
@BalestraPatrick
Copy link
Contributor Author

BalestraPatrick commented Nov 22, 2021

So I've noticed that on our CI running macOS 11.4 doesn't have this issue, only these logs:

2021-11-22 10:39:30.694 swiftlint[8242:783027] *** -[__NSCFString substringWithRange:]: Range {799, 18446744073709551611} out of bounds; string length 2101. This will become an exception for apps linked after 10.10 and iOS 8. Warning shown once per app execution.
2021-11-22 10:39:32.744 swiftlint[8241:782850] *** -[__NSCFString substringWithRange:]: Range {2356, 18446744073709551592} out of bounds; string length 5246. This will become an exception for apps linked after 10.10 and iOS 8. Warning shown once per app execution.
2021-11-22 10:39:41.292 swiftlint[8293:783270] *** -[__NSCFString substringWithRange:]: Range {1896, 18446744073709551611} out of bounds; string length 4288. This will become an exception for apps linked after 10.10 and iOS 8. Warning shown once per app execution.

When running the same command on macOS 12.0 the following happens:

2021-11-22 15:13:55.900 swiftlint[80984:29048713] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[Swift.__StringStorage substringWithRange:]: Range {799, 18446744073709551611} out of bounds; string length 2101'
...

followed by the stack trace in the original issue.

Is this a Foundation bug (or assertion) added in macOS 12.0?

@jpsim
Copy link
Collaborator

jpsim commented Nov 29, 2021

Can you share the source file that can reproduce this crash? I've found that SwiftLint crashes for specific source files are fairly easy to reduce too if you're concerned about sharing proprietary code.

@BalestraPatrick
Copy link
Contributor Author

@jpsim Is there way to print the exact file where this is crashing? Otherwise I'm not sure how I can track it down in a pretty big codebase.

@jpsim
Copy link
Collaborator

jpsim commented Nov 30, 2021

The easiest way is to run SwiftLint with Xcode: https://github.com/realm/SwiftLint/blob/master/CONTRIBUTING.md#building-and-running-locally

then you’ll get a breakpoint when the exception is thrown. You’ll be able to see what file was being processed when the crash occurred.

alternatively you could run SwiftLint on a subset of paths, “bisecting” as you go along.

@BalestraPatrick
Copy link
Contributor Author

@jpsim I found about 25 files that crash SwiftLint in our codebase. As I said in the original message of the issue, the attributes rule seems to be at fault.

I can see one example where this attribute reproduces the issue:

@objc(
      componentModelWithIdentifier:
      componentNamespace:
      componentName:
      topicName:
      imageURL:
      tappedCommand:
      deleteButtonTappedCommand:
)
public static func objc_componentModel(
      identifier: String,
      componentNamespace: String,
      componentName: String,
      topicName: String,
      imageURL: URL?,
      tappedCommand: Command,
      deleteButtonTappedCommand: Command
) -> Model {
      ...
}

Another similar example:

@objc(
    provideAssetLoaderWithHTTPMaximumConnectionsPerHost:
    timeoutIntervalForRequest:
    timeoutIntervalForResource:
)
func provideAssetLoader(
    httpMaximumConnectionsPerHost: Int,
    timeoutIntervalForRequest: TimeInterval,
    timeoutIntervalForResource: TimeInterval
) -> URLAssetLoader

I hope this helps!

@jpsim
Copy link
Collaborator

jpsim commented Dec 2, 2021

Thanks for sharing those examples, here's a reduced repro case:

$ cat repro.swift
import Foundation

class MyClass: NSObject {
  @objc(
    first:
  )
  static func foo(first: String) {}
}
$ swiftlint --enable-all-rules     
Linting Swift files in current working directory
Linting 'repro.swift' (1/1)
2021-12-02 15:53:55.997 swiftlint[46828:5399350] *** -[__NSCFString substringWithRange:]: Range {68, 18446744073709551613} out of bounds; string length 107. This will become an exception for apps linked after 10.10 and iOS 8. Warning shown once per app execution.

@jpsim
Copy link
Collaborator

jpsim commented Dec 6, 2021

Potential fix here: #3776

@BalestraPatrick could you please try out that branch and let me know if you're still encountering issues?

@BalestraPatrick
Copy link
Contributor Author

Thanks a lot @jpsim! That fixes it 🥳

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants