1
0
mirror of https://github.com/OpenTTD/OpenTTD synced 2026-01-31 16:14:29 +01:00

Fix #14278, ccd586a7: [Script] Don't set members inside operator new() (#14568)

This commit is contained in:
Loïc Guilloux
2025-09-02 12:58:31 +02:00
committed by GitHub
parent fce2748bb7
commit 7a6e04561d
10 changed files with 22 additions and 23 deletions

View File

@@ -58,7 +58,7 @@ HSQUIRRELVM sq_open(SQInteger initialstacksize)
SQVM *v;
sq_new(ss, SQSharedState);
v = (SQVM *)SQ_MALLOC(sizeof(SQVM));
new (v) SQVM(ss);
new (v, sizeof(SQVM)) SQVM(ss);
ss->_root_vm = v;
if(v->Init(nullptr, initialstacksize)) {
return v;
@@ -76,7 +76,7 @@ HSQUIRRELVM sq_newthread(HSQUIRRELVM friendvm, SQInteger initialstacksize)
ss=_ss(friendvm);
v= (SQVM *)SQ_MALLOC(sizeof(SQVM));
new (v) SQVM(ss);
new (v, sizeof(SQVM)) SQVM(ss);
if(v->Init(friendvm, initialstacksize)) {
friendvm->Push(v);

View File

@@ -13,7 +13,7 @@ private:
public:
static SQArray* Create(SQSharedState *ss,SQInteger nInitialSize){
SQArray *newarray=(SQArray*)SQ_MALLOC(sizeof(SQArray));
new (newarray) SQArray(ss,nInitialSize);
new (newarray, sizeof(SQArray)) SQArray(ss,nInitialSize);
return newarray;
}
#ifndef NO_GARBAGE_COLLECTOR

View File

@@ -34,7 +34,7 @@ struct SQClass : public CHAINABLE_OBJ
public:
static SQClass* Create(SQSharedState *ss,SQClass *base) {
SQClass *newclass = (SQClass *)SQ_MALLOC(sizeof(SQClass));
new (newclass) SQClass(ss, base);
new (newclass, sizeof(SQClass)) SQClass(ss, base);
return newclass;
}
~SQClass();
@@ -90,7 +90,7 @@ public:
SQInteger size = calcinstancesize(theclass);
SQInstance *newinst = (SQInstance *)SQ_MALLOC(size);
new (newinst) SQInstance(ss, theclass,size);
new (newinst, size) SQInstance(ss, theclass,size);
if(theclass->_udsize) {
newinst->_userpointer = ((unsigned char *)newinst) + (size - theclass->_udsize);
}
@@ -100,7 +100,7 @@ public:
{
SQInteger size = calcinstancesize(_class);
SQInstance *newinst = (SQInstance *)SQ_MALLOC(size);
new (newinst) SQInstance(ss, this,size);
new (newinst, size) SQInstance(ss, this,size);
if(_class->_udsize) {
newinst->_userpointer = ((unsigned char *)newinst) + (size - _class->_udsize);
}

View File

@@ -11,7 +11,7 @@ private:
public:
static SQClosure *Create(SQSharedState *ss,SQFunctionProto *func){
SQClosure *nc=(SQClosure*)SQ_MALLOC(sizeof(SQClosure));
new (nc) SQClosure(ss,func);
new (nc, sizeof(SQClosure)) SQClosure(ss,func);
return nc;
}
void Release() override {
@@ -49,7 +49,7 @@ private:
public:
static SQGenerator *Create(SQSharedState *ss,SQClosure *closure){
SQGenerator *nc=(SQGenerator*)SQ_MALLOC(sizeof(SQGenerator));
new (nc) SQGenerator(ss,closure);
new (nc, sizeof(SQGenerator)) SQGenerator(ss,closure);
return nc;
}
~SQGenerator()
@@ -85,7 +85,7 @@ public:
static SQNativeClosure *Create(SQSharedState *ss,SQFUNCTION func)
{
SQNativeClosure *nc=(SQNativeClosure*)SQ_MALLOC(sizeof(SQNativeClosure));
new (nc) SQNativeClosure(ss,func);
new (nc, sizeof(SQNativeClosure)) SQNativeClosure(ss,func);
return nc;
}
SQNativeClosure *Clone()

View File

@@ -99,8 +99,9 @@ public:
{
SQFunctionProto *f;
//I compact the whole class and members in a single memory allocation
f = (SQFunctionProto *)sq_vm_malloc(_FUNC_SIZE(ninstructions,nliterals,nparameters,nfunctions,noutervalues,nlineinfos,nlocalvarinfos,ndefaultparams));
new (f) SQFunctionProto(ninstructions, nliterals, nparameters, nfunctions, noutervalues, nlineinfos, nlocalvarinfos, ndefaultparams);
SQInteger size = _FUNC_SIZE(ninstructions, nliterals, nparameters, nfunctions, noutervalues, nlineinfos, nlocalvarinfos, ndefaultparams);
f = (SQFunctionProto *)sq_vm_malloc(size);
new (f, size) SQFunctionProto(ninstructions, nliterals, nparameters, nfunctions, noutervalues, nlineinfos, nlocalvarinfos, ndefaultparams);
return f;
}
void Release() override {

View File

@@ -91,7 +91,8 @@ SQUnsignedInteger TranslateIndex(const SQObjectPtr &idx)
SQWeakRef *SQRefCounted::GetWeakRef(SQObjectType type)
{
if(!_weakref) {
sq_new(_weakref,SQWeakRef);
_weakref = (SQWeakRef *)sq_vm_malloc(sizeof(SQWeakRef));
new (_weakref, sizeof(SQWeakRef)) SQWeakRef();
_weakref->_obj._type = type;
_weakref->_obj._unVal.pRefCounted = this;
}

View File

@@ -64,22 +64,19 @@ struct SQRefCounted
virtual void Release()=0;
/* Placement new/delete to prevent memory leaks if constructor throws an exception. */
inline void *operator new(size_t size, SQRefCounted *place)
inline void *operator new([[maybe_unused]] size_t size, void *ptr, [[maybe_unused]] size_t real_size)
{
place->size = size;
return place;
assert(size <= real_size);
return ptr;
}
inline void operator delete(void *ptr, SQRefCounted *place)
inline void operator delete(void *ptr, void *, size_t real_size)
{
SQ_FREE(ptr, place->size);
SQ_FREE(ptr, real_size);
}
/* Never used but required. */
inline void operator delete(void *) { NOT_REACHED(); }
private:
size_t size = 0;
};
struct SQWeakRef : SQRefCounted

View File

@@ -553,7 +553,7 @@ SQString *SQStringTable::Add(std::string_view new_string)
}
SQString *t=(SQString *)SQ_MALLOC(len+sizeof(SQString));
new (t) SQString(new_string);
new (t, len+sizeof(SQString)) SQString(new_string);
t->_next = _strings[slot];
_strings[slot] = t;
_slotused++;

View File

@@ -46,7 +46,7 @@ public:
static SQTable* Create(SQSharedState *ss,SQInteger nInitialSize)
{
SQTable *newtable = (SQTable*)SQ_MALLOC(sizeof(SQTable));
new (newtable) SQTable(ss, nInitialSize);
new (newtable, sizeof(SQTable)) SQTable(ss, nInitialSize);
newtable->_delegate = nullptr;
return newtable;
}

View File

@@ -14,7 +14,7 @@ struct SQUserData : SQDelegable
static SQUserData* Create(SQSharedState *ss, SQInteger size)
{
SQUserData* ud = (SQUserData*)SQ_MALLOC(sizeof(SQUserData)+(size-1));
new (ud) SQUserData(ss, size);
new (ud, sizeof(SQUserData)+(size-1)) SQUserData(ss, size);
return ud;
}
#ifndef NO_GARBAGE_COLLECTOR