fix(policies): guard extractPresetEntries against null/undefined input#1042
fix(policies): guard extractPresetEntries against null/undefined input#1042cv merged 1 commit intoNVIDIA:mainfrom
Conversation
extractPresetEntries() crashes with TypeError when called with null or undefined because it calls .match() on the input without a falsy check. While applyPreset() guards against this via loadPreset() returning null first, the function is exported and callable directly — any caller passing an unexpected falsy value hits an unhandled crash. Add an early return for falsy input, consistent with the existing parseCurrentPolicy() pattern which already guards against null/empty. Add 15 regression tests covering both extractPresetEntries (8 tests) and parseCurrentPolicy (7 tests), which previously had zero direct test coverage despite being exported pure functions.
📝 WalkthroughWalkthroughA defensive improvement to Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
|
@cv friendly ping , this is ready for review whenever you have a moment 🙏 |
|
✨ Thanks for submitting this fix with a detailed summary, it addresses a bug in the extractPresetEntries function, which could improve the stability and reliability of NemoClaw. |
|
@Junior00619 @cv This looks good to merge. |
Nice! Always a pleasure :)) |
#1042) ## Problem `extractPresetEntries()` throws an unhandled `TypeError: Cannot read properties of null (reading 'match')` when called with `null` or `undefined`. The function is exported as part of the public module API but lacks the same defensive guard that its sibling `parseCurrentPolicy()` already implements. The current call site in `applyPreset()` is protected indirectly — `loadPreset()` returns `null` and the caller bails before reaching `extractPresetEntries()` — but direct consumers of the exported function hit the crash: const { extractPresetEntries } = require("./policies"); extractPresetEntries(null); // TypeError Fix Add an early-return null guard consistent with the existing [parseCurrentPolicy()](vscode-file://vscode-app/usr/share/code/resources/app/out/vs/code/electron-browser/workbench/workbench.html) pattern (line 79). One line, zero behavioral change for the happy path. Tests Added 15 regression tests for two exported pure functions that previously had zero direct test coverage: [extractPresetEntries](vscode-file://vscode-app/usr/share/code/resources/app/out/vs/code/electron-browser/workbench/workbench.html) (8 tests): null, undefined, empty string, missing section, extraction correctness, trailing whitespace stripping, real preset integration, metadata exclusion [parseCurrentPolicy](vscode-file://vscode-app/usr/share/code/resources/app/out/vs/code/electron-browser/workbench/workbench.html) (7 tests): null, undefined, empty string, metadata stripping, no-separator passthrough, whitespace trimming, edge-case separator position All 37 policy tests pass. Full suite: 615 pass, 2 pre-existing failures in install-preflight.test.js (unrelated).
NVIDIA#1042) ## Problem `extractPresetEntries()` throws an unhandled `TypeError: Cannot read properties of null (reading 'match')` when called with `null` or `undefined`. The function is exported as part of the public module API but lacks the same defensive guard that its sibling `parseCurrentPolicy()` already implements. The current call site in `applyPreset()` is protected indirectly — `loadPreset()` returns `null` and the caller bails before reaching `extractPresetEntries()` — but direct consumers of the exported function hit the crash: const { extractPresetEntries } = require("./policies"); extractPresetEntries(null); // TypeError Fix Add an early-return null guard consistent with the existing [parseCurrentPolicy()](vscode-file://vscode-app/usr/share/code/resources/app/out/vs/code/electron-browser/workbench/workbench.html) pattern (line 79). One line, zero behavioral change for the happy path. Tests Added 15 regression tests for two exported pure functions that previously had zero direct test coverage: [extractPresetEntries](vscode-file://vscode-app/usr/share/code/resources/app/out/vs/code/electron-browser/workbench/workbench.html) (8 tests): null, undefined, empty string, missing section, extraction correctness, trailing whitespace stripping, real preset integration, metadata exclusion [parseCurrentPolicy](vscode-file://vscode-app/usr/share/code/resources/app/out/vs/code/electron-browser/workbench/workbench.html) (7 tests): null, undefined, empty string, metadata stripping, no-separator passthrough, whitespace trimming, edge-case separator position All 37 policy tests pass. Full suite: 615 pass, 2 pre-existing failures in install-preflight.test.js (unrelated).
Problem
extractPresetEntries()throws an unhandledTypeError: Cannot read properties of null (reading 'match')when called withnullorundefined. The function is exported as part of the public module API but lacks the same defensive guard that its siblingparseCurrentPolicy()already implements.The current call site in
applyPreset()is protected indirectly —loadPreset()returnsnulland the caller bails before reachingextractPresetEntries()— but direct consumers of the exported function hit the crash:const { extractPresetEntries } = require("./policies");
extractPresetEntries(null); // TypeError
Fix
Add an early-return null guard consistent with the existing parseCurrentPolicy() pattern (line 79). One line, zero behavioral change for the happy path.
Tests
Added 15 regression tests for two exported pure functions that previously had zero direct test coverage:
extractPresetEntries (8 tests): null, undefined, empty string, missing section, extraction correctness, trailing whitespace stripping, real preset integration, metadata exclusion
parseCurrentPolicy (7 tests): null, undefined, empty string, metadata stripping, no-separator passthrough, whitespace trimming, edge-case separator position
All 37 policy tests pass. Full suite: 615 pass, 2 pre-existing failures in install-preflight.test.js (unrelated).