Skip to content

Commit 35576ea

Browse files
Fix StaticCache::getData() returning true on empty cache
StaticCache::getData() unconditionally returned true even when no data had been inserted, causing callers to read uninitialized storage. Add a populated_ flag that tracks whether insertData() has been called. getData() now returns false with an appropriate error when the cache is empty. Also fix clearList() to reset the flag and getListLength() to return 0 when empty. Fixes #769 Signed-off-by: Pavel Guzenfeld <me@pavelguzenfeld.com>
1 parent 923656d commit 35576ea

2 files changed

Lines changed: 15 additions & 5 deletions

File tree

tf2/include/tf2/time_cache.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ class StaticCache : public TimeCacheInterface
191191

192192
private:
193193
TransformStorage storage_;
194+
bool populated_{false};
194195
};
195196
} // namespace tf2
196197
#endif // TF2__TIME_CACHE_HPP_

tf2/src/static_cache.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,16 @@ bool tf2::StaticCache::getData(
4040
tf2::TimePoint time,
4141
tf2::TransformStorage & data_out, std::string * error_str, TF2Error * error_code)
4242
{
43-
(void)error_code;
44-
(void)error_str;
43+
(void)time;
44+
if (!populated_) {
45+
if (error_str) {
46+
*error_str = "Static cache is empty";
47+
}
48+
if (error_code) {
49+
*error_code = TF2Error::TF2_LOOKUP_ERROR;
50+
}
51+
return false;
52+
}
4553
data_out = storage_;
4654
data_out.stamp_ = time;
4755
return true;
@@ -50,12 +58,13 @@ bool tf2::StaticCache::getData(
5058
bool tf2::StaticCache::insertData(const tf2::TransformStorage & new_data)
5159
{
5260
storage_ = new_data;
61+
populated_ = true;
5362
return true;
5463
}
5564

56-
void tf2::StaticCache::clearList() {}
65+
void tf2::StaticCache::clearList() {populated_ = false;}
5766

58-
unsigned tf2::StaticCache::getListLength() {return 1;}
67+
unsigned tf2::StaticCache::getListLength() {return populated_ ? 1 : 0;}
5968

6069
tf2::CompactFrameID tf2::StaticCache::getParent(
6170
tf2::TimePoint time, std::string * error_str,
@@ -64,7 +73,7 @@ tf2::CompactFrameID tf2::StaticCache::getParent(
6473
(void)time;
6574
(void)error_code;
6675
(void)error_str;
67-
return storage_.frame_id_;
76+
return populated_ ? storage_.frame_id_ : 0;
6877
}
6978

7079
tf2::P_TimeAndFrameID tf2::StaticCache::getLatestTimeAndParent()

0 commit comments

Comments
 (0)