From 838a8c081d1ba06483a0b13df029544f3f9a5b68 Mon Sep 17 00:00:00 2001 From: Daniel Duan Date: Wed, 8 Jul 2026 22:58:09 -0700 Subject: [PATCH] Fix sign-only integer unpacking --- Sources/TOMLDecoder/Parsing/Parser.swift | 3 +++ Tests/TOMLDecoderTests/TOMLTableKeyMembershipTests.swift | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/Sources/TOMLDecoder/Parsing/Parser.swift b/Sources/TOMLDecoder/Parsing/Parser.swift index 3375973..8b0d71a 100644 --- a/Sources/TOMLDecoder/Parsing/Parser.swift +++ b/Sources/TOMLDecoder/Parsing/Parser.swift @@ -1308,6 +1308,9 @@ extension Token { resultCodeUnits.append(bytes[index]) index = text.index(after: index) } + guard index < text.upperBound else { + throw TOMLError(.invalidInteger(context: context, lineNumber: lineNumber, reason: "expected digit after sign")) + } if bytes[index] == CodeUnits.underscore { throw TOMLError(.invalidInteger(context: context, lineNumber: lineNumber, reason: "cannot start with a '_'")) diff --git a/Tests/TOMLDecoderTests/TOMLTableKeyMembershipTests.swift b/Tests/TOMLDecoderTests/TOMLTableKeyMembershipTests.swift index d6464b3..7100229 100644 --- a/Tests/TOMLDecoderTests/TOMLTableKeyMembershipTests.swift +++ b/Tests/TOMLDecoderTests/TOMLTableKeyMembershipTests.swift @@ -45,4 +45,11 @@ struct TOMLTableKeyMembershipTests { try inlineTable.string(forKey: "x") } } + + @Test + func signOnlyIntegerThrowsWithoutTrapping() throws { + #expect(throws: TOMLError.self) { + _ = try Dictionary(TOMLTable(source: "x = +\n")) + } + } }