Description
Environment
- Xcode version: 12.5
- Firebase SDK version: 8.0.0
- Installation method:
Swift Package Manager
- Firebase Component: Firestore
The Problem
Currently, attempting a Firestore document load (of any nature) with an empty ID causes a runtime exception which results in a crash. By "empty ID" I am referring to a string whose character count is zero, but where the object is non-nil. Literally, an empty string.
While Firestore handles a number of errors and returns the failure results gracefully in many cases, this is not one of those. It would be nice if the SDK could catch this and return an error
Steps to reproduce:
- Start in a Swift environment
- Create a Firestore document or collection request
- In the document ID parameter:
.document(ID)
, supply a non-nil but empty string such as""
. - Firestore throws a fatal error and causes the application to crash.
- Crash logs are difficult to parse, as the throw occurs deep inside the Firestore core framework.
Relevant Code
It appears, from crash logs that this occurs deep in the Firestore framework (core/src/api/collection_reference.cc
):
return DocumentReference(std::move(path), firestore());
There seems to be an attempt to handle the issue and fail gracefully in Source/API/FIRCollectionReference.mm
):
- (FIRDocumentReference *)documentWithPath:(NSString *)documentPath { if (!documentPath) { ThrowInvalidArgument("Document path cannot be nil."); } DocumentReference child = self.reference.Document(util::MakeString(documentPath)); return [[FIRDocumentReference alloc] initWithReference:std::move(child)]; }
Unfortunately, if(!documentPath)
does not handle cases where strings may be non-nil, yet still empty (something I think may have been made possible in a recent Swift release 🥴?).