Skip to content

Commit

Permalink
🐛 Make mapToConstant a bit more resilient to poisoning (#5516)
Browse files Browse the repository at this point in the history
**Description**

<!-- Please provide a short description and potentially linked issues
justifying the need for this PR -->

Cf title.

<!-- * Your PR is fixing a bug or regression? Check for existing issues
related to this bug and link them -->
<!-- * Your PR is adding a new feature? Make sure there is a related
issue or discussion attached to it -->

<!-- You can provide any additional context to help into understanding
what's this PR is attempting to solve: reproduction of a bug, code
snippets... -->

**Checklist** — _Don't delete this checklist and make sure you do the
following before opening the PR_

- [x] The name of my PR follows [gitmoji](https://gitmoji.dev/)
specification
- [x] My PR references one of several related issues (if any)
- [x] New features or breaking changes must come with an associated
Issue or Discussion
- [x] My PR does not add any new dependency without an associated Issue
or Discussion
- [x] My PR includes bumps details, please run `yarn bump` and flag the
impacts properly
- [x] My PR adds relevant tests and they would have failed without my PR
(when applicable)

<!-- More about contributing at
https://github.com/dubzzz/fast-check/blob/main/CONTRIBUTING.md -->

**Advanced**

<!-- How to fill the advanced section is detailed below! -->

- [x] Category: 🐛 Fix a bug
- [x] Impacts: None expected, should be more resilient to poisoning

<!-- [Category] Please use one of the categories below, it will help us
into better understanding the urgency of the PR -->
<!-- * ✨ Introduce new features -->
<!-- * 📝 Add or update documentation -->
<!-- * ✅ Add or update tests -->
<!-- * 🐛 Fix a bug -->
<!-- * 🏷️ Add or update types -->
<!-- * ⚡️ Improve performance -->
<!-- * _Other(s):_ ... -->

<!-- [Impacts] Please provide a comma separated list of the potential
impacts that might be introduced by this change -->
<!-- * Generated values: Can your change impact any of the existing
generators in terms of generated values, if so which ones? when? -->
<!-- * Shrink values: Can your change impact any of the existing
generators in terms of shrink values, if so which ones? when? -->
<!-- * Performance: Can it require some typings changes on user side?
Please give more details -->
<!-- * Typings: Is there a potential performance impact? In which cases?
-->
  • Loading branch information
dubzzz authored Dec 12, 2024
1 parent 19fe7c6 commit 7b52827
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilled-items-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"fast-check": patch
---

🐛 Make `mapToConstant` a bit more resilient to poisoning
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Error } from '../../../utils/globals';
import { Error, Number, Map, safeMapGet, safeMapSet } from '../../../utils/globals';

/** @internal */
const safeObjectIs = Object.is;
Expand Down Expand Up @@ -68,7 +68,7 @@ function buildReverseMapping(entries: { num: number; build: (idInGroup: number)
if (value === 0 && 1 / value === Number.NEGATIVE_INFINITY) {
reverseMapping.negativeZeroIndex = choiceIndex;
} else {
reverseMapping.mapping.set(value, choiceIndex);
safeMapSet(reverseMapping.mapping, value, choiceIndex);
}
++choiceIndex;
}
Expand All @@ -85,7 +85,9 @@ export function indexToMappedConstantUnmapperFor<T>(
if (reverseMapping === null) {
reverseMapping = buildReverseMapping(entries);
}
const choiceIndex = safeObjectIs(value, -0) ? reverseMapping.negativeZeroIndex : reverseMapping.mapping.get(value);
const choiceIndex = safeObjectIs(value, -0)
? reverseMapping.negativeZeroIndex
: safeMapGet(reverseMapping.mapping, value);
if (choiceIndex === undefined) {
throw new Error('Unknown value encountered cannot be built using this mapToConstant');
}
Expand Down

0 comments on commit 7b52827

Please sign in to comment.