Skip to content

Commit a340ab2

Browse files
committed
Target: Add range checks
1 parent 11fb517 commit a340ab2

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/scratch/target.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ int Target::addVariable(std::shared_ptr<Variable> variable)
4848
/*! Returns the variable at index. */
4949
std::shared_ptr<Variable> Target::variableAt(int index) const
5050
{
51+
if (index < 0 || index >= impl->variables.size())
52+
return nullptr;
53+
5154
return impl->variables[index];
5255
}
5356

@@ -96,6 +99,9 @@ int Target::addList(std::shared_ptr<List> list)
9699
/*! Returns the list at index. */
97100
std::shared_ptr<List> Target::listAt(int index) const
98101
{
102+
if (index < 0 || index >= impl->lists.size())
103+
return nullptr;
104+
99105
return impl->lists[index];
100106
}
101107

@@ -144,6 +150,9 @@ int Target::addBlock(std::shared_ptr<Block> block)
144150
/*! Returns the block at index. */
145151
std::shared_ptr<Block> Target::blockAt(int index) const
146152
{
153+
if (index < 0 || index >= impl->blocks.size())
154+
return nullptr;
155+
147156
return impl->blocks[index];
148157
}
149158

@@ -214,6 +223,7 @@ int Target::addCostume(const Costume &costume)
214223
/*! Returns the costume at index. */
215224
Costume Target::costumeAt(int index) const
216225
{
226+
// TODO: Add range check
217227
return impl->costumes[index];
218228
}
219229

@@ -259,6 +269,7 @@ int Target::addSound(const Sound &sound)
259269
/*! Returns the sound at index. */
260270
Sound Target::soundAt(int index) const
261271
{
272+
// TODO: Add range check
262273
return impl->sounds[index];
263274
}
264275

0 commit comments

Comments
 (0)