Skip to content

fix: Harden float JSON encoding and validate battery level#7802

Open
denrase wants to merge 1 commit intomainfrom
fix/harden-float-json-encoding
Open

fix: Harden float JSON encoding and validate battery level#7802
denrase wants to merge 1 commit intomainfrom
fix/harden-float-json-encoding

Conversation

@denrase
Copy link
Copy Markdown
Collaborator

@denrase denrase commented Apr 14, 2026

📜 Description

Ports numeric encoding guards from upstream KSCrash (kstenerud/KSCrash#526 (kstenerud/KSCrash#526)):

  • Encode ±infinity as 1e999/-1e999 instead of the invalid JSON literal inf
  • Validate snprintf return before emitting data (format before beginElement so errors can't corrupt the JSON stream)

Also validates battery level in SentrySystemEventBreadcrumbs — rejects non-finite and out of-range values before they reach the crash-scope JSON encoder.

💡 Motivation and Context

Relates to #4580

💚 How did you test it?

Unit tests.

📝 Checklist

You have to check all boxes before merging:

  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • I updated the wizard if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.

Ports numeric encoding guards from upstream KSCrash (kstenerud/KSCrash#526 (kstenerud/KSCrash#526)):
 - Encode ±infinity as 1e999/-1e999 instead of the invalid JSON literal inf
 - Validate snprintf return before emitting data (format before beginElement so errors can't corrupt the JSON stream)

Also validates battery level in SentrySystemEventBreadcrumbs — rejects non-finite and out of-range values before they reach the crash-scope JSON encoder.

Relates to #4580
@github-actions
Copy link
Copy Markdown
Contributor

Semver Impact of This PR

🟢 Patch (bug fixes)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


Bug Fixes 🐛

  • Harden float JSON encoding and validate battery level by denrase in #7802
  • Detect development builds via provisioning profile entitlement by denrase in #7702

Internal Changes 🔧

Deps

  • Bump actions/upload-pages-artifact from 4.0.0 to 5.0.0 by dependabot in #7789
  • Bump actions/github-script from 8.0.0 to 9.0.0 by dependabot in #7793

🤖 This preview updates automatically when you update the PR.

@github-actions
Copy link
Copy Markdown
Contributor

Fails
🚫 Please consider adding a changelog entry for the next release.

Instructions and example for changelog

Please add an entry to CHANGELOG.md to the "Unreleased" section. Make sure the entry includes this PR's number.

Example:

## Unreleased

### Fixes

- Harden float JSON encoding and validate battery level ([#7802](https://github.com/getsentry/sentry-cocoa/pull/7802))

If none of the above apply, you can opt out of this check by adding #skip-changelog to the PR description or adding a skip-changelog label.

Generated by 🚫 dangerJS against 31ded34

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 14, 2026

Codecov Report

❌ Patch coverage is 80.00000% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.117%. Comparing base (6ea2191) to head (31ded34).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
...SentryCrash/Recording/Tools/SentryCrashJSONCodec.c 76.000% 6 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@              Coverage Diff              @@
##              main     #7802       +/-   ##
=============================================
- Coverage   85.416%   85.117%   -0.300%     
=============================================
  Files          487       487               
  Lines        29190     29208       +18     
  Branches     12625     12618        -7     
=============================================
- Hits         24933     24861       -72     
- Misses        4207      4296       +89     
- Partials        50        51        +1     
Files with missing lines Coverage Δ
...ons/Breadcrumbs/SentrySystemEventBreadcrumbs.swift 98.421% <100.000%> (+0.016%) ⬆️
...SentryCrash/Recording/Tools/SentryCrashJSONCodec.c 88.734% <76.000%> (-0.665%) ⬇️

... and 15 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6ea2191...31ded34. Read the comment docs.

@denrase denrase marked this pull request as ready for review April 14, 2026 15:44
// W3C spec says level must be null if it is unknown.
// Also guard against non-finite or out-of-range values so they never
// reach the crash-scope JSON encoder as invalid floats.
if currentState != .unknown && currentLevel.isFinite && (0...1).contains(currentLevel) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l: Does apple even report non finite values?

if (isinf(value)) {
int result = sentrycrashjson_beginElement(context, name);
unlikely_if(result != SentryCrashJSON_OK) { return result; }
return value > 0 ? addJSONData(context, "1e999", 5) : addJSONData(context, "-1e999", 6);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m: are these the expected values at Sentry or just replicating KSCrash?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants