Skip to content

Commit ab2b192

Browse files
committed
ArrayTable prevent block overflow
1 parent b57fcf1 commit ab2b192

3 files changed

Lines changed: 12 additions & 4 deletions

File tree

include/sta/ArrayTable.hh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "Vector.hh"
1515
#include "ObjectId.hh"
16+
#include "Error.hh"
1617

1718
namespace sta {
1819

@@ -42,8 +43,9 @@ public:
4243
size_t size() const { return size_; }
4344
void clear();
4445

45-
static constexpr ObjectId idx_bits = 10;
46-
static constexpr ObjectId block_size = (1 << idx_bits);
46+
static constexpr int idx_bits = 7;
47+
static constexpr int block_size = (1 << idx_bits);
48+
static constexpr int block_id_max = 1 << (object_id_bits - idx_bits);
4749

4850
private:
4951
ArrayBlock<TYPE> *makeBlock(uint32_t size);
@@ -129,6 +131,8 @@ void
129131
ArrayTable<TYPE>::pushBlock(ArrayBlock<TYPE> *block)
130132
{
131133
blocks_[blocks_size_++] = block;
134+
if (blocks_size_ >= block_id_max)
135+
internalError("max array table block count exceeded.");
132136
if (blocks_size_ == blocks_capacity_) {
133137
size_t new_capacity = blocks_capacity_ * 1.5;
134138
ArrayBlock<TYPE>** new_blocks = new ArrayBlock<TYPE>*[new_capacity];

include/sta/ObjectId.hh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ typedef uint32_t BlockIdx;
1919
// Object index within a block.
2020
typedef uint32_t ObjectIdx;
2121

22+
static constexpr int object_id_bits = sizeof(ObjectId) * 8;
2223
static constexpr BlockIdx block_idx_null = 0;
2324
static constexpr ObjectId object_id_null = 0;
2425
static constexpr ObjectIdx object_idx_null = 0;

include/sta/ObjectTable.hh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ public:
4343
void clear();
4444

4545
// Objects are allocated in blocks of 128.
46-
static constexpr ObjectId idx_bits = 7;
47-
static constexpr ObjectId block_object_count = (1 << idx_bits);
46+
static constexpr int idx_bits = 7;
47+
static constexpr int block_object_count = (1 << idx_bits);
48+
static constexpr int block_id_max = 1 << (object_id_bits - idx_bits);
4849

4950
private:
5051
void makeBlock();
@@ -104,6 +105,8 @@ ObjectTable<TYPE>::makeBlock()
104105
BlockIdx block_index = blocks_.size();
105106
TableBlock<TYPE> *block = new TableBlock<TYPE>(block_index, this);
106107
blocks_.push_back(block);
108+
if (blocks_.size() >= block_id_max)
109+
internalError("max object table block count exceeded.");
107110
// ObjectId zero is reserved for object_id_null.
108111
int last = (block_index > 0) ? 0 : 1;
109112
for (int i = block_object_count - 1; i >= last; i--) {

0 commit comments

Comments
 (0)