fix(plugin-mongodb): prevent crash on NaN or infinite document values (#1418)#1423
Merged
Conversation
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
Opening a MongoDB collection crashes the app (SIGABRT) when a document contains a non-finite floating-point value (
NaN,+Infinity,-Infinity). BSONdoubleis IEEE-754, so these values are legal in stored documents. Reported in #1418 with a crash report from 0.44.0.The crash trace:
+[NSJSONSerialization dataWithJSONObject:options:error:]->_writeJSONArray->_writeJSONNumber->objc_exception_throw->abort().Root cause
BsonDocumentFlattener.sanitizeForJsonwas not total: itsdefaultbranch returned any unrecognized value unchanged, so a non-finiteDouble/NSNumberreachedJSONSerialization.data(withJSONObject:). That method raises an Objective-CNSInvalidArgumentExceptionfor non-finite numbers (and for unsupported types). The surroundingdo { try ... } catch {}was dead code: Swift cannot catch a raisedNSException, only bridgedNSError, so the exception unwound toabort().MongoDBPluginDriver.prettyJson(collection validators, index key specs) had the identical uncatchabletry?flaw.Fix
sanitizeForJsonis now total: every value resolves to a guaranteed-JSON-safe form. Non-finite doubles become the tokensNaN/Infinity/-Infinity; any unsupported type becomesString(describing:)so nothing invalid can reach the writer.serializeToJsongates onJSONSerialization.isValidJSONObjectwith an early-return guard, replacing the deadcatch.stringValuerenders top-level non-finite doubles as the same tokens instead of Swift's lowercasenan/inf.prettyJsonsanitizes and validates before serializing.Token spellings match MongoDB Extended JSON / mongosh. Nested values render quoted (
{"v":"NaN"}, valid JSON, consistent with how Data/Date are already coerced); top-level cells render bare (NaN).Tests
Added 9 tests to
BsonDocumentFlattenerTests: nested NaN/+Inf/-Inf in dicts and arrays, the flatten-level crash scenario, top-level token rendering, and an unsupported nested type that now stringifies instead of crashing.Notes
plugin-mongodb-v*release after merge.