Skip to content

Commit

Permalink
fix(normalize): Treat Infinity as NaN both are non-serializable numbe…
Browse files Browse the repository at this point in the history
…rs (#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
  • Loading branch information
krystofwoldrich committed Sep 12, 2024
1 parent fa9fa5d commit 1664dc7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,10 @@ sentryTest('should set extras from multiple consecutive calls', async ({ getLoca
const eventData = await getFirstSentryEnvelopeRequest<Event>(page, url);

expect(eventData.message).toBe('consecutive_calls');
expect(eventData.extra).toMatchObject({ extra: [], Infinity: 2, null: null, obj: { foo: ['bar', 'baz', 1] } });
expect(eventData.extra).toMatchObject({
extra: [],
Infinity: 2,
null: '[Infinity]',
obj: { foo: ['bar', 'baz', 1] },
});
});
7 changes: 4 additions & 3 deletions packages/utils/src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ function visit(
// Get the simple cases out of the way first
if (
value == null || // this matches null and undefined -> eqeq not eqeqeq
(['number', 'boolean', 'string'].includes(typeof value) && !Number.isNaN(value))
['boolean', 'string'].includes(typeof value) ||
(typeof value === 'number' && Number.isFinite(value))
) {
return value as Primitive;
}
Expand Down Expand Up @@ -220,8 +221,8 @@ function stringifyValue(
return '[SyntheticEvent]';
}

if (typeof value === 'number' && value !== value) {
return '[NaN]';
if (typeof value === 'number' && !Number.isFinite(value)) {
return `[${value}]`;
}

if (typeof value === 'function') {
Expand Down
2 changes: 2 additions & 0 deletions packages/utils/test/normalize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,8 @@ describe('normalize()', () => {
describe('changes unserializeable/global values/classes to their respective string representations', () => {
test('primitive values', () => {
expect(normalize(NaN)).toEqual('[NaN]');
expect(normalize(Infinity)).toEqual('[Infinity]');
expect(normalize(-Infinity)).toEqual('[-Infinity]');
expect(normalize(Symbol('dogs'))).toEqual('[Symbol(dogs)]');
expect(normalize(BigInt(1121201212312012))).toEqual('[BigInt: 1121201212312012]');
});
Expand Down

0 comments on commit 1664dc7

Please sign in to comment.