Skip to content

fix(DICOMMetadata): prevent crash when PixelMeasuresSequence is empty#6016

Open
vishal112222 wants to merge 1 commit into
OHIF:masterfrom
vishal112222:fix/segmentation-drag-and-drop-issue
Open

fix(DICOMMetadata): prevent crash when PixelMeasuresSequence is empty#6016
vishal112222 wants to merge 1 commit into
OHIF:masterfrom
vishal112222:fix/segmentation-drag-and-drop-issue

Conversation

@vishal112222
Copy link
Copy Markdown

@vishal112222 vishal112222 commented May 13, 2026

Context

Fixes a runtime error that occurs when dragging and dropping segmentation data onto the viewport for studies containing incomplete or empty PixelMeasuresSequence metadata.

The previous implementation assumed that:

PixelMeasuresSequence[0]

always returned a valid object when PixelMeasuresSequence was an array.

However, some DICOM studies contain:

PixelMeasuresSequence = []

which caused the following runtime exception during segmentation loading:

TypeError: Cannot destructure property 'SpacingBetweenSlices'
of 'PixelMeasures' as it is undefined.

This resulted in the viewer crashing when a segmentation was dragged and dropped onto the viewport.


Changes & Results

  • Added safe fallback handling for empty PixelMeasuresSequence arrays
  • Prevented destructuring from undefined
  • Improved robustness for segmentation metadata parsing
  • No behavioral changes for valid DICOM studies

Before

Dragging and dropping segmentation data onto the viewport could crash the viewer when PixelMeasuresSequence existed but was empty.

After

The viewer now safely handles empty or missing PixelMeasuresSequence values and continues loading segmentation data without crashing.


Testing

  1. Load a study containing segmentation data

  2. Drag and drop segmentation onto the viewport

  3. Verify:

    • No runtime exception occurs
    • Viewer remains stable
    • Segmentation loads correctly

Additional cases tested:

  • PixelMeasuresSequence = []
  • PixelMeasuresSequence = undefined
  • Valid studies with proper metadata

Checklist

PR

  • My Pull Request title is descriptive, accurate and follows the semantic-release format and guidelines.

Suggested PR title:

fix(segmentation): prevent crash when PixelMeasuresSequence is empty

Code

  • My code has been well-documented (function documentation, inline comments, etc.)

Before
const PixelMeasures = Array.isArray(PixelMeasuresSequence)
? PixelMeasuresSequence[0]
: PixelMeasuresSequence;

After

const PixelMeasures = Array.isArray(PixelMeasuresSequence)
? PixelMeasuresSequence[0]
: PixelMeasuresSequence || {};

Public Documentation Updates

  • The documentation page has been updated as necessary for any public API additions or removals. (No public API changes)

Tested Environment

  • OS: Windows 11
  • Node version: 22.13.1
  • Browser: Chrome

Greptile Summary

This PR attempts to prevent a TypeError crash when destructuring PixelMeasures from an empty or missing PixelMeasuresSequence. However, the || {} fallback is placed only on the non-array branch of the ternary, leaving the reported crash path — an empty array [] — still unguarded.

  • The described crash happens when PixelMeasuresSequence = []: Array.isArray([]) is true, so PixelMeasuresSequence[0] returns undefined, and the destructure on line 299 still throws. The || {} must also be applied to the array branch: PixelMeasuresSequence[0] || {}.
  • The parallel pattern for SharedFunctionalGroupsSequence on line 289\u2013291 has the same empty-array vulnerability but is not addressed by this PR.

Confidence Score: 3/5

The fix does not cover the crash scenario it was written to address; merging as-is leaves the empty-array path broken.

The || {} guard is applied to the non-array branch only. The exact case described in the PR — PixelMeasuresSequence = [] — still takes the array branch, returns undefined from index 0, and crashes on destructuring.

extensions/cornerstone-dicom-seg/src/viewports/OHIFCornerstoneSEGViewport.tsx — the ternary on lines 295–297 still leaves the empty-array case unguarded.

Important Files Changed

Filename Overview
extensions/cornerstone-dicom-seg/src/viewports/OHIFCornerstoneSEGViewport.tsx Guard added to the non-array branch of the PixelMeasuresSequence ternary, but the actual crash (empty array []) goes through the array branch where the guard is still missing.
Prompt To Fix All With AI
Fix the following 1 code review issue. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 1
extensions/cornerstone-dicom-seg/src/viewports/OHIFCornerstoneSEGViewport.tsx:295-297
The `|| {}` fallback is applied to the **non-array** branch, but the crash described in the PR occurs when `PixelMeasuresSequence` **is** an array — specifically an empty one. When `PixelMeasuresSequence = []`, `Array.isArray([])` is `true`, so the true branch is taken: `PixelMeasuresSequence[0]` evaluates to `undefined`, and then `const { SpacingBetweenSlices, SliceThickness } = undefined` still throws a `TypeError`. The fix must guard the true (array) branch as well.

```suggestion
  const PixelMeasures = Array.isArray(PixelMeasuresSequence)
    ? PixelMeasuresSequence[0] || {}
    : PixelMeasuresSequence || {};
```

Reviews (1): Last reviewed commit: "crash when PixelMeasuresSequence item is..." | Re-trigger Greptile

Greptile also left 1 inline comment on this PR.

@netlify
Copy link
Copy Markdown

netlify Bot commented May 13, 2026

Deploy Preview for ohif-dev ready!

Name Link
🔨 Latest commit c7f1ac6
🔍 Latest deploy log https://app.netlify.com/projects/ohif-dev/deploys/6a044f7fe4b9250008fa1de3
😎 Deploy Preview https://deploy-preview-6016--ohif-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

Comment on lines 295 to +297
const PixelMeasures = Array.isArray(PixelMeasuresSequence)
? PixelMeasuresSequence[0]
: PixelMeasuresSequence;
: PixelMeasuresSequence || {};
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 The || {} fallback is applied to the non-array branch, but the crash described in the PR occurs when PixelMeasuresSequence is an array — specifically an empty one. When PixelMeasuresSequence = [], Array.isArray([]) is true, so the true branch is taken: PixelMeasuresSequence[0] evaluates to undefined, and then const { SpacingBetweenSlices, SliceThickness } = undefined still throws a TypeError. The fix must guard the true (array) branch as well.

Suggested change
const PixelMeasures = Array.isArray(PixelMeasuresSequence)
? PixelMeasuresSequence[0]
: PixelMeasuresSequence;
: PixelMeasuresSequence || {};
const PixelMeasures = Array.isArray(PixelMeasuresSequence)
? PixelMeasuresSequence[0] || {}
: PixelMeasuresSequence || {};
Prompt To Fix With AI
This is a comment left during a code review.
Path: extensions/cornerstone-dicom-seg/src/viewports/OHIFCornerstoneSEGViewport.tsx
Line: 295-297

Comment:
The `|| {}` fallback is applied to the **non-array** branch, but the crash described in the PR occurs when `PixelMeasuresSequence` **is** an array — specifically an empty one. When `PixelMeasuresSequence = []`, `Array.isArray([])` is `true`, so the true branch is taken: `PixelMeasuresSequence[0]` evaluates to `undefined`, and then `const { SpacingBetweenSlices, SliceThickness } = undefined` still throws a `TypeError`. The fix must guard the true (array) branch as well.

```suggestion
  const PixelMeasures = Array.isArray(PixelMeasuresSequence)
    ? PixelMeasuresSequence[0] || {}
    : PixelMeasuresSequence || {};
```

How can I resolve this? If you propose a fix, please make it concise.

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.

2 participants