Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid unwrapping crash when pmt parsing has returned undefined #433

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

gurushida
Copy link

When trying to transmux or inspect the attached ts segment, mux.js crashes because the pmt parsing returns an undefined value that the caller tries to use as a non empty object. This PR adds fallbacks to use empty objects when that happens.

Here is a typescript file that reproduces the problem:

import { mp2t } from 'mux.js';
import fs from 'fs';

main(process.argv.slice(2)).catch(err => console.error(err));

async function main(args: string[]) {
  for (const file of args) {
    const buffer = fs.readFileSync(file);
    const tsInfo = mp2t.tools.inspect(buffer);
    console.log(`File ${file}: ${JSON.stringify(tsInfo, null,  2)}`);
  }
}

Without the fix, the output is as follows:

$ muxProbe ~/Desktop/pmt.ts
TypeError: Cannot convert undefined or null to object
    at Function.keys (<anonymous>)
    at parsePsi_2 (..../muxProbe.js:7423:20)
    at inspectTs_2 (..../muxProbe.js:7705:5)
    at Object.inspect2 [as inspect] (..../muxProbe.js:7737:16)
    at main (..../muxProbe.js:7774:42)
    at Object.<anonymous> (..../muxProbe.js:7770:1)
    at Module._compile (node:internal/modules/cjs/loader:1254:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Module._load (node:internal/modules/cjs/loader:958:12)

With the fix, it works as expected:

$ muxProbe pmt.ts
File pmt.ts: {
  "video": [
    {
      "pts": 183600,
      "dts": 180000,
      "type": "video",
      "dtsTime": 2,
      "ptsTime": 2.04
    },
    {
      "pts": 500400,
      "dts": 500400,
      "type": "video",
      "dtsTime": 5.56,
      "ptsTime": 5.56
    }
  ],
  "firstKeyFrame": {
    "pts": 183600,
    "dts": 180000,
    "type": "video",
    "dtsTime": 2,
    "ptsTime": 2.04
  },
  "audio": [
    {
      "pts": 180600,
      "dts": 180600,
      "type": "audio",
      "dtsTime": 2.006666666666667,
      "ptsTime": 2.006666666666667
    },
    {
      "pts": 507000,
      "dts": 507000,
      "type": "audio",
      "dtsTime": 5.633333333333334,
      "ptsTime": 5.633333333333334
    }
  ]
}

pmt.ts.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant