diff --git a/src/reducers/url-state.ts b/src/reducers/url-state.ts index 8903ea34b2..d307ce0b6e 100644 --- a/src/reducers/url-state.ts +++ b/src/reducers/url-state.ts @@ -606,12 +606,21 @@ const assemblyView: Reducer = ( const { nativeSymbols, currentNativeSymbol, shouldOpenAssemblyView } = action; const shouldScroll = action.scrollToInstructionAddress !== undefined; + + // Clamp currentNativeSymbol to null if it is out of bounds, so that + // "currentNativeSymbol is always in-bounds or null" holds. + const clampedNativeSymbol = + currentNativeSymbol !== null && + currentNativeSymbol < nativeSymbols.length + ? currentNativeSymbol + : null; + return { scrollGeneration: shouldScroll ? state.scrollGeneration + 1 : state.scrollGeneration, nativeSymbols, - currentNativeSymbol, + currentNativeSymbol: clampedNativeSymbol, isOpen: state.isOpen || shouldOpenAssemblyView, scrollToInstructionAddress: action.scrollToInstructionAddress, highlightedInstruction: action.highlightedInstructionAddress, diff --git a/src/test/store/bottom-box.test.ts b/src/test/store/bottom-box.test.ts index f410ee3a3c..32a6e1c1a4 100644 --- a/src/test/store/bottom-box.test.ts +++ b/src/test/store/bottom-box.test.ts @@ -345,6 +345,29 @@ describe('bottom box', function () { ).toBeOneOf([0x40, 0x45]); }); + it('returns null for getAssemblyViewNativeSymbol when nativeSymbols is empty but initialNativeSymbol is 0', function () { + // initialNativeSymbol = 0, even when nativeSymbols is empty + // (e.g., when the frame has no native symbol). + // The selector must return null (not undefined) + // to avoid a crash when downstream code accesses .libIndex on it. + const { dispatch, getState } = setup(); + + dispatch( + updateBottomBoxContentsAndMaybeOpen('calltree', { + libIndex: null, + sourceIndex: null, + nativeSymbols: [], + initialNativeSymbol: 0, + highlightedLineNumber: null, + highlightedInstructionAddress: null, + }) + ); + + expect( + UrlStateSelectors.getAssemblyViewNativeSymbol(getState()) + ).toBeNull(); + }); + // Further ideas for tests: // // - A test with multiple threads: Open the assembly view for a symbol, switch