fix(policies): guard extractPresetEntries against null/undefined input#1042
Open
Junior00619 wants to merge 1 commit intoNVIDIA:mainfrom
Open
fix(policies): guard extractPresetEntries against null/undefined input#1042Junior00619 wants to merge 1 commit intoNVIDIA:mainfrom
Junior00619 wants to merge 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.
Contributor
📝 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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).