Skip to content

Fix null map handling in toMap method#80

Open
ericraio wants to merge 1 commit intoapple:mainfrom
ericraio:ericraio/map-collection-fix
Open

Fix null map handling in toMap method#80
ericraio wants to merge 1 commit intoapple:mainfrom
ericraio:ericraio/map-collection-fix

Conversation

@ericraio
Copy link
Contributor

Add null check before iterating map values to prevent crashes when handling null map columns.

Motivation:

When handling Cassandra query results containing null map columns, the toMap method would crash because it attempted to create an iterator from a null pointer without first checking if the value was null. This is the same issue that was addressed for arrays in PR #77, but the map handling code path was missed in that fix.

Modifications:

  • Added null check using cass_value_is_null before creating the map iterator in Data+Maps.swift:272
  • Added test case testMapWithNullIteratorHandling that verifies null maps, empty maps, and valid maps are all handled correctly

Result:

Map columns containing null values now return nil instead of crashing when accessed. This provides consistent null-handling behavior across all collection types (arrays, sets, and maps) and prevents runtime crashes when querying tables with nullable map columns.

Add null check before iterating map values to prevent crashes when
handling null map columns. This matches the pattern used for array
null handling in the previous fix.
@yifan-c yifan-c added the 🔨 semver/patch No public API change. label Feb 2, 2026
Copy link
Collaborator

@yifan-c yifan-c left a comment

Choose a reason for hiding this comment

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

Thanks for the fix!

let row = result.first!

XCTAssertNil(row.column("nullable_map")?.int32StringMap)
XCTAssertNil(row.column("empty_map")?.int32StringMap)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should it assert on returning an empty map, instead of nil? The semantics seems wrong.
It is not a behavior changed in the patch, although.


var map: [K: V] = [:]

let iterator = cass_iterator_from_map(self.rawPointer)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should null case be handled in this method to create iterator? I am referring to #77

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

Labels

🔨 semver/patch No public API change.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants