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

Add validation for invalid memory references #131

Merged
merged 2 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/webview/components/options-widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { MemoryOptions, ReadMemoryArguments, SessionContext } from '../../common
import { tryToNumber } from '../../common/typescript';
import { CONFIG_BYTES_PER_MAU_CHOICES, CONFIG_GROUPS_PER_ROW_CHOICES, CONFIG_MAUS_PER_GROUP_CHOICES } from '../../plugin/manifest';
import { TableRenderOptions } from '../columns/column-contribution-service';
import { DEFAULT_READ_ARGUMENTS } from '../memory-webview-view';
import { AddressPaddingOptions, MemoryState, SerializedTableRenderOptions } from '../utils/view-types';
import { createSectionVscodeContext } from '../utils/vscode-contexts';
import { MultiSelectWithLabel } from './multi-select';
Expand Down Expand Up @@ -120,11 +121,14 @@ export class OptionsWidget extends React.Component<OptionsWidgetProps, OptionsWi
return errors;
};

componentDidUpdate(_: Readonly<OptionsWidgetProps>, prevState: Readonly<OptionsWidgetState>): void {
componentDidUpdate(prevProps: Readonly<OptionsWidgetProps>, prevState: Readonly<OptionsWidgetState>): void {
if (!prevState.isTitleEditing && this.state.isTitleEditing) {
this.labelEditInput.current?.focus();
this.labelEditInput.current?.select();
}
if (prevProps.activeReadArguments === DEFAULT_READ_ARGUMENTS) {
this.formConfig.initialErrors = this.validate(this.optionsFormValues);
}
}

override render(): React.ReactNode {
Expand Down
10 changes: 8 additions & 2 deletions src/webview/memory-webview-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const MEMORY_DISPLAY_CONFIGURATION_DEFAULTS: MemoryDisplayConfiguration = {
showRadixPrefix: true,
};

const DEFAULT_READ_ARGUMENTS: Required<ReadMemoryArguments> = {
export const DEFAULT_READ_ARGUMENTS: Required<ReadMemoryArguments> = {
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it make sense to move this into a more common place? Like ../common/messaging or ../common/memory? Looks like ReadMemoryArguments is defined in ../common/messaging.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

IMO I don't see any benefit in doing that.
The default constants defined in the memory-webview-view (MEMORY_DISPLAY_CONFIGURATION_DEFAULTS,DEFAULT_SESSION_CONTEXT etc.) only serve the purpose of constructing the initial state for the App component. They are an implementation detail of ( and scoped to) the webview/react app and should not leak into other components.

Copy link
Contributor

Choose a reason for hiding this comment

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

OK, not a too strong opinion about this. The main reason for raising this was to avoid a direct dependency between the memory-webview-view and the options-widget.

Choose a reason for hiding this comment

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

I agree with @jreineckearm that it would be better to move this to a constants file (e.g. view-types.ts) to avoid circular dependencies.

memoryReference: '',
offset: 0,
count: 256,
Expand Down Expand Up @@ -249,13 +249,19 @@ class App extends React.Component<{}, MemoryAppState> {
if (this.state.isFrozen) {
return;
}
this.setState(prev => ({ ...prev, isMemoryFetching: true }));
const completeOptions = {
memoryReference: partialOptions?.memoryReference || this.state.activeReadArguments.memoryReference,
offset: partialOptions?.offset ?? this.state.activeReadArguments.offset,
count: partialOptions?.count ?? this.state.activeReadArguments.count
};

// Don't fetch memory if we have an incomplete memory reference
if (completeOptions.memoryReference === '') {
return;
}

this.setState(prev => ({ ...prev, isMemoryFetching: true }));

try {
const response = await messenger.sendRequest(readMemoryType, HOST_EXTENSION, completeOptions);
await Promise.all(Array.from(
Expand Down
Loading