From 1df4cf7d3af44309ebc85fa2de2f34a700143a14 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Tue, 1 Sep 2026 11:41:53 -0700 Subject: [PATCH] Parse Reader API hex item IDs as unsigned so high-bit IDs no longer overflow to an unsendable ID. --- .../Account/ReaderAPI/ReaderAPIEntry.swift | 8 ++- .../ReaderAPI/ReaderAPIEntryTests.swift | 57 +++++++++++++++++++ 2 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 Modules/Account/Tests/AccountTests/ReaderAPI/ReaderAPIEntryTests.swift diff --git a/Modules/Account/Sources/Account/ReaderAPI/ReaderAPIEntry.swift b/Modules/Account/Sources/Account/ReaderAPI/ReaderAPIEntry.swift index db8baeb4e..c7d423653 100644 --- a/Modules/Account/Sources/Account/ReaderAPI/ReaderAPIEntry.swift +++ b/Modules/Account/Sources/Account/ReaderAPI/ReaderAPIEntry.swift @@ -92,12 +92,14 @@ struct ReaderAPIEntry: Codable, Sendable { return idPart } - // Convert hex representation back to integer and then a string representation - guard let idNumber = Int(idPart, radix: 16) else { + // Convert hex representation back to integer and then a string representation. + // Parse unsigned so a high-bit ID doesn't overflow, then reinterpret the bit + // pattern as signed — the two's-complement form itemIDParameter re-encodes. + guard let idNumber = UInt64(idPart, radix: 16) else { return articleID } - return String(idNumber, radix: 10, uppercase: false) + return String(Int64(bitPattern: idNumber)) } } diff --git a/Modules/Account/Tests/AccountTests/ReaderAPI/ReaderAPIEntryTests.swift b/Modules/Account/Tests/AccountTests/ReaderAPI/ReaderAPIEntryTests.swift new file mode 100644 index 000000000..19a98a44c --- /dev/null +++ b/Modules/Account/Tests/AccountTests/ReaderAPI/ReaderAPIEntryTests.swift @@ -0,0 +1,57 @@ +// +// ReaderAPIEntryTests.swift +// AccountTests +// +// Created by Brent Simmons on 9/1/26. +// + +import Foundation +import Testing +@testable import Account + +struct ReaderAPIEntryTests { + + @Test func lowIDConvertsToDecimal() throws { + let entry = try makeEntry(articleID: "tag:google.com,2005:reader/item/00058b10ce338909") + #expect(entry.uniqueID(variant: .freshRSS) == "1560279178774793") + } + + @Test func highBitIDDoesNotOverflow() throws { + // The signed Int(_, radix: 16) returned nil here, so the whole tag string leaked + // through as the ID and the status was classed unsendable and dropped. + let entry = try makeEntry(articleID: "tag:google.com,2005:reader/item/ffffffffffffcdef") + #expect(entry.uniqueID(variant: .freshRSS) == "-12817") + } + + @Test func zeroID() throws { + let entry = try makeEntry(articleID: "tag:google.com,2005:reader/item/0000000000000000") + #expect(entry.uniqueID(variant: .freshRSS) == "0") + } + + @Test func nonHexIDReturnsArticleIDUnchanged() throws { + let articleID = "tag:google.com,2005:reader/item/notavalidhexvalue" + let entry = try makeEntry(articleID: articleID) + #expect(entry.uniqueID(variant: .freshRSS) == articleID) + } + + @Test func theOldReaderReturnsRawIDPart() throws { + let entry = try makeEntry(articleID: "tag:google.com,2005:reader/item/00058b10ce338909") + #expect(entry.uniqueID(variant: .theOldReader) == "00058b10ce338909") + } + + @Test func decimalRoundTripsToOriginalHex() throws { + let hexIDs = ["00058b10ce338909", "ffffffffffffcdef", "0000000000000000"] + for hex in hexIDs { + let entry = try makeEntry(articleID: "tag:google.com,2005:reader/item/\(hex)") + let decimal = entry.uniqueID(variant: .freshRSS) + // Mirror itemIDParameter's inverse — the decimal must re-encode to the original hex. + let value = try #require(Int(decimal)) + #expect(String(format: "%.16llx", value) == hex) + } + } + + private func makeEntry(articleID: String) throws -> ReaderAPIEntry { + let json = "{\"id\":\"\(articleID)\",\"summary\":{},\"categories\":[],\"origin\":{}}" + return try JSONDecoder().decode(ReaderAPIEntry.self, from: Data(json.utf8)) + } +}