SIGTRAP with .sup file

Open
#47 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Quiet
Tech stack
macos, swift
Domain
backend

Research direction

The crash is an force-unwrap of iterator.next() in RLEData.decodePGS() at Sources/macSubtitleOCR/Subtitles/RLE/RLEData.swift:46, reached through ODS.decodeRLEData() and PGS.parseNextSubtitle(). Start by reading the RLE decoding loop and the PGS parser, then reproduce with the attached sigtrap.sup and truncated.sup files. Done means the malformed input no longer traps and the truncated case is handled or reported cleanly.

Written by the indexing model from the issue text.

Description

With this sup file:

sigtrap.sup.gz

using current HEAD:

macSubtitleOCR/RLEData.swift:46: Fatal error: Unexpectedly found nil while unwrapping an Optional value

💣 Program crashed: System trap at 0x000000019362cc24

Thread 1 crashed:

 0 0x000000019362cc24 closure #1 in closure #1 in closure #1 in _assertionFailure(_:_:file:line:flags:) + 240 in libswiftCore.dylib
 1 0x000000019362caec closure #1 in closure #1 in _assertionFailure(_:_:file:line:flags:) + 340 in libswiftCore.dylib
 2 0x000000019362c494 _assertionFailure(_:_:file:line:flags:) + 188 in libswiftCore.dylib
 3 RLEData.decodePGS() + 912 in macSubtitleOCR at /Users/t/Downloads/macSubtitleOCR/Sources/macSubtitleOCR/Subtitles/RLE/RLEData.swift:46:44

    44│ 
    45│             if color == 0x00 {
    46│                 let flags = iterator.next()!                                                                                            
      │                                           ▲
    47│                 run = Int(flags & 0x3F)
    48│                 if flags & 0x40 != 0 {

 4 ODS.decodeRLEData() + 136 in macSubtitleOCR at /Users/t/Downloads/macSubtitleOCR/Sources/macSubtitleOCR/Subtitles/PGS/Parsers/ODS.swift:37:33

    35│     func decodeRLEData() throws -> Data {
    36│         let rleImageData = RLEData(data: encodedImageData, width: objectWidth, height: objectHeight)
    37│         return try rleImageData.decodePGS()                                                                                             
      │                                 ▲
    38│     }
    39│

 5 PGS.parseNextSubtitle(_:_:) + 3440 in macSubtitleOCR at /Users/t/Downloads/macSubtitleOCR/Sources/macSubtitleOCR/Subtitles/PGS/PGS.swift:119:32

   117│                 imageWidth: ods.objectWidth,
   118│                 imageHeight: ods.objectHeight,
   119│                 imageData: ods.decodeRLEData(),                                                                                         
      │                                ▲
   120│                 imagePalette: pds.palette,
   121│                 numberOfColors: 256)

 6 PGS.parseData(_:) + 2732 in macSubtitleOCR at /Users/t/Downloads/macSubtitleOCR/Sources/macSubtitleOCR/Subtitles/PGS/PGS.swift:42:38

    40│         while offset + pgsHeaderLength < buffer.count {
    41│             logger.debug("Parsing subtitle at offset: \(offset)")
    42│             guard let subtitle = try parseNextSubtitle(buffer, &offset)                                                                 
      │                                      ▲
    43│             else {
    44│                 if offset + pgsHeaderLength > buffer.count { break }

 7 closure #1 in PGS.init(_:) + 52 in macSubtitleOCR at /Users/t/Downloads/macSubtitleOCR/Sources/macSubtitleOCR/Subtitles/PGS/PGS.swift:24:17

    22│         let data = try Data(contentsOf: url)
    23│         try data.withUnsafeBytes { (buffer: UnsafeRawBufferPointer) in
    24│             try parseData(buffer)                                                                                                       
      │                 ▲
    25│         }
    26│     }

 8 0x000000018395fcfc __DataStorage.withUnsafeBytes<A>(in:apply:) + 88 in Foundation
 9 0x0000000183966320 Data._Representation.withUnsafeBytes<A>(_:) + 344 in Foundation
10 0x000000018395e940 Data.withUnsafeBytes<A>(_:) + 16 in Foundation
11 macSubtitleOCR.processInternalDecoder() + 8684 in macSubtitleOCR at /Users/t/Downloads/macSubtitleOCR/Sources/macSubtitleOCR/macSubtitleOCR.swift:112:27

   110│         } else if input.hasSuffix(".sup") {
   111│             // Open the PGS data stream
   112│             let PGS = try PGS(URL(fileURLWithPath: input))                                                                              
      │                           ▲
   113│             let result = try await processSubtitle(PGS.subtitles, trackNumber: 0)
   114│             results.append(result)

12 macSubtitleOCR.processInput() in macSubtitleOCR at /Users/t/Downloads/macSubtitleOCR/Sources/macSubtitleOCR/macSubtitleOCR.swift:63

    61│         }
    62│         #else
    63│         try await processInternalDecoder()                                                                                              
      │         ▲
    64│         #endif
    65│     }

13 macSubtitleOCR.run() in macSubtitleOCR at /Users/timj/Downloads/macSubtitleOCR/Sources/macSubtitleOCR/macSubtitleOCR.swift:42

    40│         do {
    41│             let fileHandler = try macSubtitleOCRFileHandler(outputDirectory: outputDirectory)
    42│             let results = try await processInput()                                                                                      
      │             ▲
    43│             try await saveResults(fileHandler: fileHandler, results: results)
    44│         } catch let macSubtitleOCRError.fileReadError(string), let macSubtitleOCRError.invalidInputFile(string),

14 static AsyncParsableCommand.main(_:) in macSubtitleOCR at /Users/t/Downloads/macSubtitleOCR/.build/checkouts/swift-argument-parser/Sources/ArgumentParser/Parsable Types/AsyncParsableCommand.swift:41

    39│       var command = try parseAsRoot(arguments)
    40│       if var asyncCommand = command as? AsyncParsableCommand {
    41│         try await asyncCommand.run()                                                                                                    
      │         ▲
    42│       } else {
    43│         try command.run()

15 static AsyncParsableCommand.main() in macSubtitleOCR at /Users/t/Downloads/macSubtitleOCR/.build/checkouts/swift-argument-parser/Sources/ArgumentParser/Parsable Types/AsyncParsableCommand.swift:61

    59│   /// if necessary.
    60│   public static func main() async {
    61│     await self.main(nil)                                                                                                                
      │     ▲
    62│   }
    63│ }

Backtrace took 1.22s

zsh: trace trap

with this sup from the same disc:

truncated.sup.gz

I get:

Found invalid image for track: 0, index: 1, creating an empty placeholder!

and it only gives half of the first subtitle.

Dominant language
Swift
Stars
42
Forks
9
Avg merge
9h 30m
Merged PRs (30d)
4

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ecdye/macSubtitleOCR

All issues in ecdye/macSubtitleOCR

Similar issues

More Swift issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.