From aa083605851c53465b401cd1490cd9a530184df1 Mon Sep 17 00:00:00 2001 From: stackotter Date: Thu, 2 Dec 2021 08:10:30 +1000 Subject: [PATCH] Fix fatal crash caused by force unwrap --- Package.resolved | 18 ++++++++++++++++ Package.swift | 2 ++ Sources/swift-bundler/Subcommands/Build.swift | 12 ++++++++--- Sources/swift-bundler/main.swift | 21 ++++++++++++++++++- install.sh | 2 +- 5 files changed, 50 insertions(+), 5 deletions(-) diff --git a/Package.resolved b/Package.resolved index 3f3ce63c..68cb4a2f 100644 --- a/Package.resolved +++ b/Package.resolved @@ -27,6 +27,24 @@ "revision": "5d66f7ba25daf4f94100e7022febf3c75e37a6c7", "version": "1.4.2" } + }, + { + "package": "swift-nio", + "repositoryURL": "https://github.com/apple/swift-nio.git", + "state": { + "branch": null, + "revision": "37e7a33de45bac894c0b08b56a2f755ebe4884e6", + "version": "2.35.0" + } + }, + { + "package": "SwiftBacktrace", + "repositoryURL": "https://github.com/norio-nomura/SwiftBacktrace", + "state": { + "branch": null, + "revision": "1b4bbe87c19d5e2e04786d8140ab36fec4b7881b", + "version": "1.0.1" + } } ] }, diff --git a/Package.swift b/Package.swift index 3b3a73f2..0707d724 100644 --- a/Package.swift +++ b/Package.swift @@ -9,6 +9,7 @@ let package = Package( dependencies: [ .package(url: "https://github.com/apple/swift-argument-parser", from: "1.0.0"), .package(name: "DeltaLogger", url: "https://github.com/stackotter/delta-logger", .branch("main")), + .package(url: "https://github.com/norio-nomura/SwiftBacktrace", from: "1.0.1"), ], targets: [ .target( @@ -16,6 +17,7 @@ let package = Package( dependencies: [ .product(name: "ArgumentParser", package: "swift-argument-parser"), "DeltaLogger", + "SwiftBacktrace" ]), ] ) diff --git a/Sources/swift-bundler/Subcommands/Build.swift b/Sources/swift-bundler/Subcommands/Build.swift index 1a37688e..f163eb83 100644 --- a/Sources/swift-bundler/Subcommands/Build.swift +++ b/Sources/swift-bundler/Subcommands/Build.swift @@ -89,8 +89,14 @@ extension Bundler { } else if line.starts(with: "[") { let parts = line.split(separator: "]") let progressParts = parts[0].dropFirst().split(separator: "/") - let progress = Double(progressParts[0])! - let total = Double(progressParts[1])! + + guard + let progress = Double(progressParts[0]), + let total = Double(progressParts[1]) + else { + return + } + let decimalProgress = progress / total updateProgress(line, 0.8 * decimalProgress + 0.1, shouldLog: false) } else if line.starts(with: "Fetching") || line.starts(with: "Resolving") || line.starts(with: "Cloning") { @@ -128,4 +134,4 @@ extension Bundler { updateProgress("Build completed", 1) } -} \ No newline at end of file +} diff --git a/Sources/swift-bundler/main.swift b/Sources/swift-bundler/main.swift index 8a5b2cee..787d99d4 100644 --- a/Sources/swift-bundler/main.swift +++ b/Sources/swift-bundler/main.swift @@ -1,4 +1,23 @@ +import Foundation +import SwiftBacktrace + +NSSetUncaughtExceptionHandler { (exception) in + let stack = exception.callStackReturnAddresses + print("Stack trace: \(stack)") +} + +func handleSignal(_ code: Int32) { + print("caught crash") +} + +signal(SIGABRT, handleSignal); +signal(SIGILL, handleSignal); +signal(SIGSEGV, handleSignal); +signal(SIGFPE, handleSignal); +signal(SIGBUS, handleSignal); +signal(SIGPIPE, handleSignal); + Bundler.main() // TODO: support sandboxing -// TODO: add proper help messages to subcommands, options and flags \ No newline at end of file +// TODO: add proper help messages to subcommands, options and flags diff --git a/install.sh b/install.sh index 1703825a..b48d491d 100755 --- a/install.sh +++ b/install.sh @@ -1,6 +1,6 @@ #!/bin/sh # Build -swift build -c release +swift build -c debug # Create directory with correct permissions sudo mkdir -p -m755 /opt/swift-bundler