Skip to content

Commit

Permalink
Fix fatal crash caused by force unwrap
Browse files Browse the repository at this point in the history
  • Loading branch information
stackotter committed Dec 1, 2021
1 parent 349f14d commit aa08360
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 5 deletions.
18 changes: 18 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
]
},
Expand Down
2 changes: 2 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ 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(
name: "swift-bundler",
dependencies: [
.product(name: "ArgumentParser", package: "swift-argument-parser"),
"DeltaLogger",
"SwiftBacktrace"
]),
]
)
12 changes: 9 additions & 3 deletions Sources/swift-bundler/Subcommands/Build.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down Expand Up @@ -128,4 +134,4 @@ extension Bundler {

updateProgress("Build completed", 1)
}
}
}
21 changes: 20 additions & 1 deletion Sources/swift-bundler/main.swift
Original file line number Diff line number Diff line change
@@ -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
// TODO: add proper help messages to subcommands, options and flags
2 changes: 1 addition & 1 deletion install.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit aa08360

Please sign in to comment.