Skip to content

Commit 1664dc7

Browse files
fix(normalize): Treat Infinity as NaN both are non-serializable numbers (#13406)
RN SDK uses the normalize function before passing data over the RN Bridge, which only accepts serializable data. Infinity causes -> getsentry/sentry-react-native#4024
1 parent fa9fa5d commit 1664dc7

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

dev-packages/browser-integration-tests/suites/public-api/setExtras/consecutive_calls/test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,10 @@ sentryTest('should set extras from multiple consecutive calls', async ({ getLoca
1010
const eventData = await getFirstSentryEnvelopeRequest<Event>(page, url);
1111

1212
expect(eventData.message).toBe('consecutive_calls');
13-
expect(eventData.extra).toMatchObject({ extra: [], Infinity: 2, null: null, obj: { foo: ['bar', 'baz', 1] } });
13+
expect(eventData.extra).toMatchObject({
14+
extra: [],
15+
Infinity: 2,
16+
null: '[Infinity]',
17+
obj: { foo: ['bar', 'baz', 1] },
18+
});
1419
});

packages/utils/src/normalize.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ function visit(
8181
// Get the simple cases out of the way first
8282
if (
8383
value == null || // this matches null and undefined -> eqeq not eqeqeq
84-
(['number', 'boolean', 'string'].includes(typeof value) && !Number.isNaN(value))
84+
['boolean', 'string'].includes(typeof value) ||
85+
(typeof value === 'number' && Number.isFinite(value))
8586
) {
8687
return value as Primitive;
8788
}
@@ -220,8 +221,8 @@ function stringifyValue(
220221
return '[SyntheticEvent]';
221222
}
222223

223-
if (typeof value === 'number' && value !== value) {
224-
return '[NaN]';
224+
if (typeof value === 'number' && !Number.isFinite(value)) {
225+
return `[${value}]`;
225226
}
226227

227228
if (typeof value === 'function') {

packages/utils/test/normalize.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,8 @@ describe('normalize()', () => {
403403
describe('changes unserializeable/global values/classes to their respective string representations', () => {
404404
test('primitive values', () => {
405405
expect(normalize(NaN)).toEqual('[NaN]');
406+
expect(normalize(Infinity)).toEqual('[Infinity]');
407+
expect(normalize(-Infinity)).toEqual('[-Infinity]');
406408
expect(normalize(Symbol('dogs'))).toEqual('[Symbol(dogs)]');
407409
expect(normalize(BigInt(1121201212312012))).toEqual('[BigInt: 1121201212312012]');
408410
});

0 commit comments

Comments
 (0)