From d335ed450ef7b3f3f2bdb80defefc43a2789f751 Mon Sep 17 00:00:00 2001 From: Timofey Solomko Date: Sat, 31 Jul 2021 12:23:00 +0300 Subject: [PATCH] [TAR] Counters for special entries are now UInt to prevent overflowing into negatives --- Sources/TAR/TarContainer.swift | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Sources/TAR/TarContainer.swift b/Sources/TAR/TarContainer.swift index 44155655..727764d5 100644 --- a/Sources/TAR/TarContainer.swift +++ b/Sources/TAR/TarContainer.swift @@ -94,17 +94,17 @@ public class TarContainer: Container { // Every time we append something to the output we also make sure that the data is padded to 512 byte-long blocks. // In theory if the counters are big enough, the names of the special entries can become long enough to cause - // problems (truncation, etc.). In practice, the largest possible counter (Int.max) is 19 symbols long, which + // problems (truncation, etc.). In practice, the largest possible counter (UInt.max) is 20 symbols long, which // when combined with the longest used special entry name can never cause any problems, since it is still // shorter then 99 symbols available in the "name" field of the TAR header. // However, if in the distant future Int.max becomes large enough to cause any issues (e.g. 128-bit and higher // integers), the following check will catch it. - assert(String(Int.max).count < 100 - 19) // "SWC_LocalPaxHeader_".count == 19 + assert(String(UInt.max).count < 100 - 19) // "SWC_LocalPaxHeader_".count == 19 // We also use &+ when incrementing counters to prevent integer overflow crashes: we rather deal with the special - // entries having the same name, then crash the program. - var longNameCounter = 0 - var longLinkNameCounter = 0 - var localPaxHeaderCounter = 0 + // entries having repeating names, then crash the program. + var longNameCounter = 0 as UInt + var longLinkNameCounter = 0 as UInt + var localPaxHeaderCounter = 0 as UInt var out = Data() for entry in entries {