Limit access to the _bufSize, _scriptSize and _heapSize members of the Script class

svn-id: r49327
This commit is contained in:
Filippos Karapetis 2010-05-30 16:14:31 +00:00
parent 0276ec835e
commit c32e88fe0b
10 changed files with 33 additions and 28 deletions

View file

@ -1245,7 +1245,7 @@ bool Console::segmentInfo(int nr) {
case SEG_TYPE_SCRIPT: { case SEG_TYPE_SCRIPT: {
Script *scr = (Script *)mobj; Script *scr = (Script *)mobj;
DebugPrintf("script.%03d locked by %d, bufsize=%d (%x)\n", scr->_nr, scr->getLockers(), (uint)scr->_bufSize, (uint)scr->_bufSize); DebugPrintf("script.%03d locked by %d, bufsize=%d (%x)\n", scr->_nr, scr->getLockers(), (uint)scr->getBufSize(), (uint)scr->getBufSize());
if (scr->_exportTable) if (scr->_exportTable)
DebugPrintf(" Exports: %4d at %d\n", scr->_numExports, (int)(((const byte *)scr->_exportTable) - ((const byte *)scr->_buf))); DebugPrintf(" Exports: %4d at %d\n", scr->_numExports, (int)(((const byte *)scr->_exportTable) - ((const byte *)scr->_buf)));
else else

View file

@ -87,7 +87,7 @@ bool GameFeatures::autoDetectSoundType() {
opcode = extOpcode >> 1; opcode = extOpcode >> 1;
// Check for end of script // Check for end of script
if (opcode == op_ret || offset >= script->_bufSize) if (opcode == op_ret || offset >= script->getBufSize())
break; break;
// The play method of the Sound object pushes the DoSound command // The play method of the Sound object pushes the DoSound command
@ -223,7 +223,7 @@ bool GameFeatures::autoDetectLofsType(int methodNum) {
opcode = extOpcode >> 1; opcode = extOpcode >> 1;
// Check for end of script // Check for end of script
if (opcode == op_ret || offset >= script->_bufSize) if (opcode == op_ret || offset >= script->getBufSize())
break; break;
if (opcode == op_lofsa || opcode == op_lofss) { if (opcode == op_lofsa || opcode == op_lofss) {
@ -231,13 +231,13 @@ bool GameFeatures::autoDetectLofsType(int methodNum) {
uint16 lofs = opparams[0]; uint16 lofs = opparams[0];
// Check for going out of bounds when interpreting as abs/rel // Check for going out of bounds when interpreting as abs/rel
if (lofs >= script->_bufSize) if (lofs >= script->getBufSize())
_lofsType = SCI_VERSION_0_EARLY; _lofsType = SCI_VERSION_0_EARLY;
if ((signed)offset + (int16)lofs < 0) if ((signed)offset + (int16)lofs < 0)
_lofsType = SCI_VERSION_1_MIDDLE; _lofsType = SCI_VERSION_1_MIDDLE;
if ((signed)offset + (int16)lofs >= (signed)script->_bufSize) if ((signed)offset + (int16)lofs >= (signed)script->getBufSize())
_lofsType = SCI_VERSION_1_MIDDLE; _lofsType = SCI_VERSION_1_MIDDLE;
if (_lofsType != SCI_VERSION_NONE) if (_lofsType != SCI_VERSION_NONE)
@ -309,7 +309,7 @@ bool GameFeatures::autoDetectGfxFunctionsType(int methodNum) {
opcode = extOpcode >> 1; opcode = extOpcode >> 1;
// Check for end of script // Check for end of script
if (opcode == op_ret || offset >= script->_bufSize) if (opcode == op_ret || offset >= script->getBufSize())
break; break;
if (opcode == op_callk) { if (opcode == op_callk) {
@ -412,7 +412,7 @@ bool GameFeatures::autoDetectSci21KernelType() {
opcode = extOpcode >> 1; opcode = extOpcode >> 1;
// Check for end of script // Check for end of script
if (opcode == op_ret || offset >= script->_bufSize) if (opcode == op_ret || offset >= script->getBufSize())
break; break;
if (opcode == op_callk) { if (opcode == op_callk) {
@ -465,7 +465,7 @@ bool GameFeatures::autoDetectMoveCountType() {
opcode = extOpcode >> 1; opcode = extOpcode >> 1;
// Check for end of script // Check for end of script
if (opcode == op_ret || offset >= script->_bufSize) if (opcode == op_ret || offset >= script->getBufSize())
break; break;
if (opcode == op_callk) { if (opcode == op_callk) {

View file

@ -628,7 +628,7 @@ int Kernel::findRegType(reg_t reg) {
switch (mobj->getType()) { switch (mobj->getType()) {
case SEG_TYPE_SCRIPT: case SEG_TYPE_SCRIPT:
if (reg.offset <= (*(Script *)mobj)._bufSize && if (reg.offset <= (*(Script *)mobj).getBufSize() &&
reg.offset >= -SCRIPT_OBJECT_MAGIC_OFFSET && reg.offset >= -SCRIPT_OBJECT_MAGIC_OFFSET &&
RAW_IS_OBJECT((*(Script *)mobj)._buf + reg.offset)) { RAW_IS_OBJECT((*(Script *)mobj)._buf + reg.offset)) {
return ((Script *)mobj)->getObject(reg.offset) ? KSIG_OBJECT : KSIG_REF; return ((Script *)mobj)->getObject(reg.offset) ? KSIG_OBJECT : KSIG_REF;

View file

@ -208,7 +208,7 @@ reg_t kScriptID(EngineState *s, int argc, reg_t *argv) {
// Point to the heap for SCI1.1+ games // Point to the heap for SCI1.1+ games
if (getSciVersion() >= SCI_VERSION_1_1) if (getSciVersion() >= SCI_VERSION_1_1)
address += scr->_scriptSize; address += scr->getScriptSize();
return make_reg(scriptSeg, address); return make_reg(scriptSeg, address);
} }

View file

@ -758,22 +758,22 @@ static void reconstruct_stack(EngineState *retval) {
} }
static void load_script(EngineState *s, Script *scr) { static void load_script(EngineState *s, Script *scr) {
scr->_buf = (byte *)malloc(scr->_bufSize); scr->_buf = (byte *)malloc(scr->getBufSize());
assert(scr->_buf); assert(scr->_buf);
Resource *script = g_sci->getResMan()->findResource(ResourceId(kResourceTypeScript, scr->_nr), 0); Resource *script = g_sci->getResMan()->findResource(ResourceId(kResourceTypeScript, scr->_nr), 0);
assert(script != 0); assert(script != 0);
assert(scr->_bufSize >= script->size); assert(scr->getBufSize() >= script->size);
memcpy(scr->_buf, script->data, script->size); memcpy(scr->_buf, script->data, script->size);
if (getSciVersion() >= SCI_VERSION_1_1) { if (getSciVersion() >= SCI_VERSION_1_1) {
Resource *heap = g_sci->getResMan()->findResource(ResourceId(kResourceTypeHeap, scr->_nr), 0); Resource *heap = g_sci->getResMan()->findResource(ResourceId(kResourceTypeHeap, scr->_nr), 0);
assert(heap != 0); assert(heap != 0);
scr->_heapStart = scr->_buf + scr->_scriptSize; scr->_heapStart = scr->_buf + scr->getScriptSize();
assert(scr->_bufSize - scr->_scriptSize <= heap->size); assert(scr->getBufSize() - scr->getScriptSize() <= heap->size);
memcpy(scr->_heapStart, heap->data, heap->size); memcpy(scr->_heapStart, heap->data, heap->size);
} }
} }

View file

@ -175,7 +175,7 @@ void SegManager::scriptInitialiseLocals(reg_t location) {
Script *scr = getScript(location.segment); Script *scr = getScript(location.segment);
unsigned int count; unsigned int count;
VERIFY(location.offset + 1 < (uint16)scr->_bufSize, "Locals beyond end of script\n"); VERIFY(location.offset + 1 < (uint16)scr->getBufSize(), "Locals beyond end of script\n");
if (getSciVersion() >= SCI_VERSION_1_1) if (getSciVersion() >= SCI_VERSION_1_1)
count = READ_SCI11ENDIAN_UINT16(scr->_buf + location.offset - 2); count = READ_SCI11ENDIAN_UINT16(scr->_buf + location.offset - 2);
@ -185,9 +185,9 @@ void SegManager::scriptInitialiseLocals(reg_t location) {
scr->_localsOffset = location.offset; scr->_localsOffset = location.offset;
if (!(location.offset + count * 2 + 1 < scr->_bufSize)) { if (!(location.offset + count * 2 + 1 < scr->getBufSize())) {
warning("Locals extend beyond end of script: offset %04x, count %x vs size %x", location.offset, count, (uint)scr->_bufSize); warning("Locals extend beyond end of script: offset %04x, count %x vs size %x", location.offset, count, (uint)scr->getBufSize());
count = (scr->_bufSize - location.offset) >> 1; count = (scr->getBufSize() - location.offset) >> 1;
} }
LocalVariables *locals = allocLocalsSegment(scr, count); LocalVariables *locals = allocLocalsSegment(scr, count);

View file

@ -85,7 +85,7 @@ reg_t disassemble(EngineState *s, reg_t pos, int print_bw_tag, int print_bytecod
script_entity = (Script *)mobj; script_entity = (Script *)mobj;
scr = script_entity->_buf; scr = script_entity->_buf;
scr_size = script_entity->_bufSize; scr_size = script_entity->getBufSize();
if (pos.offset >= scr_size) { if (pos.offset >= scr_size) {
warning("Trying to disassemble beyond end of script"); warning("Trying to disassemble beyond end of script");
@ -303,7 +303,7 @@ void script_debug(EngineState *s) {
if (mobj) { if (mobj) {
Script *scr = (Script *)mobj; Script *scr = (Script *)mobj;
byte *code_buf = scr->_buf; byte *code_buf = scr->_buf;
int code_buf_size = scr->_bufSize; int code_buf_size = scr->getBufSize();
int opcode = scriptState.xs->addr.pc.offset >= code_buf_size ? 0 : code_buf[scriptState.xs->addr.pc.offset]; int opcode = scriptState.xs->addr.pc.offset >= code_buf_size ? 0 : code_buf[scriptState.xs->addr.pc.offset];
int op = opcode >> 1; int op = opcode >> 1;
int paramb1 = scriptState.xs->addr.pc.offset + 1 >= code_buf_size ? 0 : code_buf[scriptState.xs->addr.pc.offset + 1]; int paramb1 = scriptState.xs->addr.pc.offset + 1 >= code_buf_size ? 0 : code_buf[scriptState.xs->addr.pc.offset + 1];

View file

@ -223,7 +223,7 @@ Object *SegManager::getObject(reg_t pos) {
warning("getObject(): Trying to get an invalid object"); warning("getObject(): Trying to get an invalid object");
} else if (mobj->getType() == SEG_TYPE_SCRIPT) { } else if (mobj->getType() == SEG_TYPE_SCRIPT) {
Script *scr = (Script *)mobj; Script *scr = (Script *)mobj;
if (pos.offset <= scr->_bufSize && pos.offset >= -SCRIPT_OBJECT_MAGIC_OFFSET if (pos.offset <= scr->getBufSize() && pos.offset >= -SCRIPT_OBJECT_MAGIC_OFFSET
&& RAW_IS_OBJECT(scr->_buf + pos.offset)) { && RAW_IS_OBJECT(scr->_buf + pos.offset)) {
obj = scr->getObject(pos.offset); obj = scr->getObject(pos.offset);
} }

View file

@ -323,10 +323,6 @@ class Script : public SegmentObj {
public: public:
int _nr; /**< Script number */ int _nr; /**< Script number */
byte *_buf; /**< Static data buffer, or NULL if not used */ byte *_buf; /**< Static data buffer, or NULL if not used */
size_t _bufSize;
size_t _scriptSize;
size_t _heapSize;
byte *_heapStart; /**< Start of heap if SCI1.1, NULL otherwise */ byte *_heapStart; /**< Start of heap if SCI1.1, NULL otherwise */
const uint16 *_exportTable; /**< Abs. offset of the export table or 0 if not present */ const uint16 *_exportTable; /**< Abs. offset of the export table or 0 if not present */
@ -335,9 +331,18 @@ public:
const byte *_synonyms; /**< Synonyms block or 0 if not present*/ const byte *_synonyms; /**< Synonyms block or 0 if not present*/
int _numSynonyms; /**< Number of entries in the synonyms block */ int _numSynonyms; /**< Number of entries in the synonyms block */
uint32 getScriptSize() { return _scriptSize; }
uint32 getHeapSize() { return _heapSize; }
uint32 getBufSize() { return _bufSize; }
protected: protected:
int _lockers; /**< Number of classes and objects that require this script */ int _lockers; /**< Number of classes and objects that require this script */
private:
size_t _scriptSize;
size_t _heapSize;
size_t _bufSize;
public: public:
/** /**
* Table for objects, contains property variables. * Table for objects, contains property variables.

View file

@ -784,7 +784,7 @@ void run_vm(EngineState *s, bool restoring) {
obj = s->_segMan->getObject(scriptState.xs->objp); obj = s->_segMan->getObject(scriptState.xs->objp);
code_buf = scr->_buf; code_buf = scr->_buf;
#ifndef DISABLE_VALIDATIONS #ifndef DISABLE_VALIDATIONS
code_buf_size = scr->_bufSize; code_buf_size = scr->getBufSize();
#endif #endif
local_script = s->_segMan->getScriptIfLoaded(scriptState.xs->local_segment); local_script = s->_segMan->getScriptIfLoaded(scriptState.xs->local_segment);
if (!local_script) { if (!local_script) {
@ -1398,7 +1398,7 @@ void run_vm(EngineState *s, bool restoring) {
switch (g_sci->_features->detectLofsType()) { switch (g_sci->_features->detectLofsType()) {
case SCI_VERSION_1_1: case SCI_VERSION_1_1:
s->r_acc.offset = opparams[0] + local_script->_scriptSize; s->r_acc.offset = opparams[0] + local_script->getScriptSize();
break; break;
case SCI_VERSION_1_MIDDLE: case SCI_VERSION_1_MIDDLE:
s->r_acc.offset = opparams[0]; s->r_acc.offset = opparams[0];
@ -1420,7 +1420,7 @@ void run_vm(EngineState *s, bool restoring) {
switch (g_sci->_features->detectLofsType()) { switch (g_sci->_features->detectLofsType()) {
case SCI_VERSION_1_1: case SCI_VERSION_1_1:
r_temp.offset = opparams[0] + local_script->_scriptSize; r_temp.offset = opparams[0] + local_script->getScriptSize();
break; break;
case SCI_VERSION_1_MIDDLE: case SCI_VERSION_1_MIDDLE:
r_temp.offset = opparams[0]; r_temp.offset = opparams[0];