From f6f4ebfa890ceda870f62ecdb22f583a7a3dda5a Mon Sep 17 00:00:00 2001 From: Daniel Resnick Date: Wed, 16 Apr 2025 18:10:56 -0600 Subject: [PATCH] Fix crashes due to forced unwrapping --- Sources/SwiftMath/MathRender/MTMathListBuilder.swift | 9 ++++++--- Tests/SwiftMathTests/MTMathListBuilderTests.swift | 4 +++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/Sources/SwiftMath/MathRender/MTMathListBuilder.swift b/Sources/SwiftMath/MathRender/MTMathListBuilder.swift index 3b146c9..26e713d 100755 --- a/Sources/SwiftMath/MathRender/MTMathListBuilder.swift +++ b/Sources/SwiftMath/MathRender/MTMathListBuilder.swift @@ -289,8 +289,11 @@ public struct MTMathListBuilder { return list } else { // Create a new table with the current list and a default env - let table = self.buildTable(env: nil, firstList: list, isRow: false) - return MTMathList(atom: table!) + if let table = self.buildTable(env: nil, firstList: list, isRow: false) { + return MTMathList(atom: table) + } else { + return nil + } } } else if spacesAllowed && char == " " { // If spaces are allowed then spaces do not need escaping with a \ before being used. @@ -720,7 +723,7 @@ public struct MTMathListBuilder { return nil } if env! != currentEnv!.envName { - let errorMessage = "Begin environment name \(currentEnv!.envName!) does not match end name: \(env!)" + let errorMessage = "Begin environment name \(currentEnv!.envName ?? "(none)") does not match end name: \(env!)" self.setError(.invalidEnv, message:errorMessage) return nil } diff --git a/Tests/SwiftMathTests/MTMathListBuilderTests.swift b/Tests/SwiftMathTests/MTMathListBuilderTests.swift index 5a9e3fb..f1453cb 100755 --- a/Tests/SwiftMathTests/MTMathListBuilderTests.swift +++ b/Tests/SwiftMathTests/MTMathListBuilderTests.swift @@ -187,7 +187,9 @@ final class MTMathListBuilderTests: XCTestCase { ("\\begin{displaylines} x & y \\end{displaylines}", .invalidNumColumns), ("\\begin{eqalign} x \\end{eqalign}", .invalidNumColumns), ("\\nolimits", .invalidLimits), - ("\\frac\\limits{1}{2}", .invalidLimits) + ("\\frac\\limits{1}{2}", .invalidLimits), + ("&\\begin", .characterNotFound), + ("x & y \\\\ z & w \\end{matrix}", .invalidEnv) ] }