Skip to content

Fix unsafe std::vector::data() nullptr assumption for empty vectors#95

Draft
Copilot wants to merge 1 commit into
masterfrom
copilot/fix-unsafe-usage-of-vector-data
Draft

Fix unsafe std::vector::data() nullptr assumption for empty vectors#95
Copilot wants to merge 1 commit into
masterfrom
copilot/fix-unsafe-usage-of-vector-data

Conversation

Copilot AI commented May 1, 2026

Copy link
Copy Markdown

std::vector::data() is not guaranteed to return nullptr when empty. The PointCloud base class uses raw pointer nullness to signal attribute absence (has_times(), has_points(), etc.), but PointCloudCPU assigns storage.data() unconditionally — so empty vectors can produce non-null pointers, causing has_*() to return true with no actual data.

Fix: Replace all ptr = storage.data() with ptr = storage.empty() ? nullptr : storage.data().

  • point_cloud_cpu.cppadd_times/points/normals/covs/intensities, clone(), load(), aux_attributes
  • point_cloud_cpu_funcs.cppsample(), voxelgrid_sampling(), aux_attributes
  • gaussian_voxelmap_cpu_funcs.cppmerge_frames()
  • point_cloud_gpu.cudownload_points()
  • point_cloud_cpu.hppadd_aux_attribute() template
  • intensity_gradients.cpp — inline normal/cov estimation
  • incremental_voxelmap_impl.hpp — points assignment
// Before: data() may return non-null for empty vector
this->times = times_storage.data();

// After: explicitly null when empty
this->times = times_storage.empty() ? nullptr : times_storage.data();

…ectors

When std::vector is empty, data() is not guaranteed to return nullptr.
This caused has_times()/has_points()/etc. to incorrectly return true
for empty point clouds, since those methods check pointer nullness.

Fix by using `vec.empty() ? nullptr : vec.data()` pattern everywhere
a storage vector's data pointer is assigned to a raw pointer field.

Agent-Logs-Url: https://github.com/koide3/gtsam_points/sessions/12ae22c6-f3d9-4719-8f59-c70496ba892d

Co-authored-by: koide3 <31344317+koide3@users.noreply.github.com>
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