Skip to content

fix(policies): guard extractPresetEntries against null/undefined input#1042

Merged
cv merged 1 commit intoNVIDIA:mainfrom
Junior00619:fix/policy-extract-null-guard
Mar 31, 2026
Merged

fix(policies): guard extractPresetEntries against null/undefined input#1042
cv merged 1 commit intoNVIDIA:mainfrom
Junior00619:fix/policy-extract-null-guard

Conversation

@Junior00619
Copy link
Copy Markdown
Contributor

@Junior00619 Junior00619 commented Mar 28, 2026

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() 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).

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.
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 28, 2026

📝 Walkthrough

Walkthrough

A defensive improvement to extractPresetEntries() that early-returns null for falsy input before regex matching, combined with comprehensive test coverage for both extractPresetEntries() and parseCurrentPolicy() functions, validating behavior for edge cases and real preset data.

Changes

Cohort / File(s) Summary
Defensive Input Handling
bin/lib/policies.js
Added early null-check in extractPresetEntries() to short-circuit before regex matching when input is falsy.
Test Coverage Expansion
test/policies.test.js
Added 115 lines of test cases for extractPresetEntries() and parseCurrentPolicy(), covering null/undefined/empty inputs, YAML section extraction, metadata header removal, separator handling, and validation against all real presets.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A null-check hops in line,
Before the regex dares to shine,
Tests now dance in every case,
Presets parsed at proper pace! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title directly and clearly describes the main change: adding a guard against null/undefined input to extractPresetEntries, which is exactly what the one-line code change accomplishes.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands and usage tips.

@Junior00619
Copy link
Copy Markdown
Contributor Author

@cv friendly ping , this is ready for review whenever you have a moment 🙏

@wscurran wscurran added bug Something isn't working good first issue Good for newcomers fix labels Mar 30, 2026
@wscurran
Copy link
Copy Markdown
Contributor

✨ 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.

@jyaunches jyaunches self-assigned this Mar 31, 2026
@jyaunches jyaunches self-requested a review March 31, 2026 20:05
@jyaunches
Copy link
Copy Markdown
Contributor

@Junior00619 @cv This looks good to merge.

@cv cv merged commit 0afb077 into NVIDIA:main Mar 31, 2026
9 of 10 checks passed
@Junior00619
Copy link
Copy Markdown
Contributor Author

@Junior00619 @cv This looks good to merge.

Nice! Always a pleasure :))

laitingsheng pushed a commit that referenced this pull request Apr 2, 2026
#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).
lakamsani pushed a commit to lakamsani/NemoClaw that referenced this pull request Apr 4, 2026
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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working fix good first issue Good for newcomers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants