(svn r17403) -Fix [Squirrel]: guard against squirrel stack overflows; if assert is enabled assert (catch possible overflow bugs in nightlies/RCs), otherwise just increase the stack's size (don't get into invalid reads/writes in releases)

This commit is contained in:
rubidium 2009-09-03 11:48:08 +00:00
parent 48e1a5a350
commit 5607a610d7
2 changed files with 14 additions and 2 deletions

View File

@ -88,7 +88,7 @@ public:
}
SQUnsignedInteger capacity() { return _allocated; }
inline T &back() const { return _vals[_size - 1]; }
inline T& operator[](SQUnsignedInteger pos) const{ return _vals[pos]; }
inline T& operator[](SQUnsignedInteger pos) const{ assert(pos < _allocated); return _vals[pos]; }
T* _vals;
private:
void _realloc(SQUnsignedInteger newsize)

View File

@ -1526,7 +1526,19 @@ void SQVM::Pop(SQInteger n) {
}
}
void SQVM::Push(const SQObjectPtr &o) { _stack[_top++] = o; }
void SQVM::Push(const SQObjectPtr &o) {
/* Normally the stack shouldn't get this full, sometimes it might. As of now
* all cases have been bugs in "our" (OpenTTD) code. Trigger an assert for
* all debug builds and for the release builds just increase the stack size.
* This way getting a false positive isn't that bad (releases work fine) and
* if there is something fishy it can be caught in RCs/nightlies. */
#ifdef NDEBUG
if (_top >= (int)_stack.capacity()) _stack.resize(2 * _stack.capacity());
#else
assert(_top < (int)_stack.capacity());
#endif
_stack[_top++] = o;
}
SQObjectPtr &SQVM::Top() { return _stack[_top-1]; }
SQObjectPtr &SQVM::PopGet() { return _stack[--_top]; }
SQObjectPtr &SQVM::GetUp(SQInteger n) { return _stack[_top+n]; }