Skip to content

Commit 40c3adb

Browse files
authored
Merge pull request #192 from at-wat/fix-stl-assertion-on-recent-libstdc++
Fix STL assertion on recent libstdc++ when handling empty PointCloud2
1 parent 20a833b commit 40c3adb

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

sensor_msgs/include/sensor_msgs/impl/point_cloud2_iterator.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,9 @@ PointCloud2IteratorBase<T, TT, U, C, V>::PointCloud2IteratorBase(C &cloud_msg, c
249249
{
250250
int offset = set_field(cloud_msg, field_name);
251251

252-
data_char_ = &(cloud_msg.data.front()) + offset;
252+
data_char_ = cloud_msg.data.data() + offset;
253253
data_ = reinterpret_cast<TT*>(data_char_);
254-
data_end_ = reinterpret_cast<TT*>(&(cloud_msg.data.back()) + 1 + offset);
254+
data_end_ = reinterpret_cast<TT*>(data_char_ + cloud_msg.data.size());
255255
}
256256

257257
/** Assignment operator

sensor_msgs/test/main.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,24 @@
3434

3535
#include <gtest/gtest.h>
3636

37+
#define _GLIBCXX_DEBUG // Enable assertions in STL
38+
3739
#include <sensor_msgs/point_cloud2_iterator.h>
3840

41+
TEST(sensor_msgs, PointCloud2Iterator_Empty)
42+
{
43+
EXPECT_EXIT({
44+
sensor_msgs::PointCloud2 cloud_msg;
45+
cloud_msg.width = 1;
46+
sensor_msgs::PointCloud2Modifier modifier(cloud_msg);
47+
modifier.setPointCloud2FieldsByString(1, "xyz");
48+
49+
sensor_msgs::PointCloud2Iterator<float> iter_x(cloud_msg, "x");
50+
51+
exit(0);
52+
}, ::testing::ExitedWithCode(0), "");
53+
}
54+
3955
TEST(sensor_msgs, PointCloud2Iterator)
4056
{
4157
// Create a dummy PointCloud2
@@ -111,6 +127,17 @@ TEST(sensor_msgs, PointCloud2Iterator)
111127
iter_const_2_g += 1;
112128
iter_const_2_b = iter_const_2_b + 1;
113129
}
130+
EXPECT_FALSE(iter_const_1_y != iter_const_1_y.end());
131+
EXPECT_FALSE(iter_const_1_z != iter_const_1_z.end());
132+
EXPECT_FALSE(iter_const_1_r != iter_const_1_r.end());
133+
EXPECT_FALSE(iter_const_1_g != iter_const_1_g.end());
134+
EXPECT_FALSE(iter_const_1_b != iter_const_1_b.end());
135+
EXPECT_FALSE(iter_const_2_x != iter_const_2_x.end());
136+
EXPECT_FALSE(iter_const_2_y != iter_const_2_y.end());
137+
EXPECT_FALSE(iter_const_2_z != iter_const_2_z.end());
138+
EXPECT_FALSE(iter_const_2_r != iter_const_2_r.end());
139+
EXPECT_FALSE(iter_const_2_g != iter_const_2_g.end());
140+
EXPECT_FALSE(iter_const_2_b != iter_const_2_b.end());
114141
EXPECT_EQ(i, n_points);
115142
}
116143

0 commit comments

Comments
 (0)