Skip to content

Commit 7a400d2

Browse files
authored
Improve CHECK-failure when passing a negative id to a ValueStore #6370 (#6392)
Otherwise the value fails in confusing ways while untagging: CHECK failure at ./toolchain/base/value_store.h:71: index >= initial_reserved_ids_: When removing tagging bits, found an index that shouldn't've been tagged in the first place. With this change: CHECK failure at ./toolchain/base/fixed_size_value_store.h:112: id.index >= 0: instFFFFFFFFFFFFFFFD
1 parent f220359 commit 7a400d2

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

toolchain/base/fixed_size_value_store.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,22 +102,22 @@ class FixedSizeValueStore {
102102

103103
// Sets the value for an ID.
104104
auto Set(IdT id, ValueType value) -> void {
105+
CARBON_DCHECK(id.index >= 0, "{0}", id);
105106
auto index = tag_.Remove(id.index);
106-
CARBON_DCHECK(index >= 0, "{0}", id);
107107
values_[index] = value;
108108
}
109109

110110
// Returns a mutable value for an ID.
111111
auto Get(IdT id) -> RefType {
112+
CARBON_DCHECK(id.index >= 0, "{0}", id);
112113
auto index = tag_.Remove(id.index);
113-
CARBON_DCHECK(index >= 0, "{0}", id);
114114
return values_[index];
115115
}
116116

117117
// Returns the value for an ID.
118118
auto Get(IdT id) const -> ConstRefType {
119+
CARBON_DCHECK(id.index >= 0, "{0}", id);
119120
auto index = tag_.Remove(id.index);
120-
CARBON_DCHECK(index >= 0, "{0}", id);
121121
return values_[index];
122122
}
123123

toolchain/base/relational_value_store.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ class RelationalValueStore {
7272

7373
// Returns a value for an ID.
7474
auto Get(IdT id) const -> ConstRefType {
75+
CARBON_DCHECK(id.index >= 0, "{0}", id);
7576
auto index = related_store_->GetIdTag().Remove(id.index);
76-
CARBON_DCHECK(index >= 0, "{0}", id);
7777
return *values_[index];
7878
}
7979

toolchain/base/value_store.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,18 @@ struct IdTag {
5151
}
5252

5353
auto Apply(int32_t index) const -> int32_t {
54+
CARBON_DCHECK(index >= 0, "{0}", index);
5455
if (index < initial_reserved_ids_) {
5556
return index;
5657
}
5758
// TODO: Assert that id_tag_ doesn't have the second highest bit set.
58-
return index ^ id_tag_;
59+
auto tagged_index = index ^ id_tag_;
60+
CARBON_DCHECK(tagged_index >= 0, "{0}", tagged_index);
61+
return tagged_index;
5962
}
6063

6164
auto Remove(int32_t tagged_index) const -> int32_t {
65+
CARBON_DCHECK(tagged_index >= 0, "{0}", tagged_index);
6266
if (!HasTag(tagged_index)) {
6367
CARBON_DCHECK(tagged_index < initial_reserved_ids_,
6468
"This untagged index is outside the initial reserved ids "
@@ -226,11 +230,11 @@ class ValueStore
226230
auto GetWithDefault(IdType id, //
227231
ConstRefType default_value [[clang::lifetimebound]]) const
228232
-> ConstRefType {
233+
CARBON_DCHECK(id.index >= 0, "{0}", id);
229234
auto index = tag_.Remove(id.index);
230235
if (index >= size_) {
231236
return default_value;
232237
}
233-
CARBON_DCHECK(index >= 0, "{0}", id);
234238
auto [chunk_index, pos] = RawIndexToChunkIndices(index);
235239
return chunks_[chunk_index].Get(pos);
236240
}
@@ -325,8 +329,8 @@ class ValueStore
325329

326330
auto GetIdTag() const -> IdTag { return tag_; }
327331
auto GetRawIndex(IdT id) const -> int32_t {
332+
CARBON_DCHECK(id.index >= 0, "{0}", index);
328333
auto index = tag_.Remove(id.index);
329-
CARBON_DCHECK(index >= 0, "{0}", index);
330334
#ifndef NDEBUG
331335
if (index >= size_) {
332336
// Attempt to decompose id.index to include extra detail in the check

0 commit comments

Comments
 (0)