Skip to content

Commit 9ff496e

Browse files
committed
libscript: Tweak MCScriptForeignInvocation to help Coverity
The `MCScriptForeignInvocation` implementation is confusing Coverity, which is reporting a bunch of uninitialised memory errors relating to pointers acquired via the `MCScriptForeignInvocation::Allocate()` method. There were two possible problems: - The maximum storage allocatable was stored in `MCScriptForeignInvocation::m_storage_limit`, a mutable member variable (despite being a compile-time constant). It's possible that Coverity was assuming that it could be mutated at some point to be larger than the size of the fixed-size `m_stack_storage` array, allowing `Allocate()` to return a pointer to invalid memory. This patch removes the member. - `MCScriptForeignInvocation::ReferenceArgument()` and `Argument()` were checking whether the current argument count was _equal_ to the maximum number of arguments, rather than checking that it was less than the maximum. This could allow Coverity to assume that the current argument count was greater than the maximum number of arguments, once again allowing pointers to invalid memory to be produced. This patch introduces inequalities. Coverity-Id: 137925 Coverity-Id: 137927
1 parent 9115ea4 commit 9ff496e

1 file changed

Lines changed: 4 additions & 6 deletions

File tree

libscript/src/script-execute.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ class MCScriptForeignInvocation
4141
public:
4242
MCScriptForeignInvocation(void)
4343
: m_argument_count(0),
44-
m_storage_frontier(0),
45-
m_storage_limit(sizeof(m_stack_storage))
44+
m_storage_frontier(0)
4645
{
4746
}
4847

@@ -71,7 +70,7 @@ class MCScriptForeignInvocation
7170
t_align_delta = p_align - (m_storage_frontier % p_align);
7271

7372
// If there is not enough space then throw.
74-
if (m_storage_limit - m_storage_frontier < p_amount + t_align_delta)
73+
if (sizeof(m_stack_storage) - m_storage_frontier < p_amount + t_align_delta)
7574
{
7675
return MCErrorThrowOutOfMemory();
7776
}
@@ -92,7 +91,7 @@ class MCScriptForeignInvocation
9291
bool Argument(void *p_slot_ptr,
9392
__MCScriptValueDrop p_slot_drop)
9493
{
95-
if (m_argument_count == kMaxArguments)
94+
if (m_argument_count >= kMaxArguments)
9695
{
9796
return MCErrorThrowOutOfMemory();
9897
}
@@ -114,7 +113,7 @@ class MCScriptForeignInvocation
114113
bool ReferenceArgument(void *p_slot_ptr,
115114
__MCScriptValueDrop p_slot_drop)
116115
{
117-
if (m_argument_count == kMaxArguments ||
116+
if (m_argument_count >= kMaxArguments ||
118117
!Allocate(sizeof(void *),
119118
alignof(void *),
120119
m_argument_values[m_argument_count]))
@@ -180,7 +179,6 @@ class MCScriptForeignInvocation
180179
void *m_argument_slots[kMaxArguments];
181180

182181
size_t m_storage_frontier;
183-
size_t m_storage_limit;
184182
char m_stack_storage[kMaxStorage];
185183
};
186184

0 commit comments

Comments
 (0)