graphitemaster / gmqcc

An Improved Quake C Compiler
Other
163 stars 29 forks source link

Fix number of bytes to be zeroed when spawn entity #201

Closed zus-dev closed 1 year ago

zus-dev commented 1 year ago

In qcvm when zeroing memory for new entitydata wrong number of bytes used. This leads to crash with the error:malloc(): invalid size (unsorted).

Use prog->entityfields * sizeof(qcint_t) instead of sz * sizeof(qcint_t).

sz is the size of the entitydata vector which is increased every time new entity spawned. prog->entityfields is the number of entity fields and the size of the increment of the entitydata vector.

The issue can be reproduced with a simple QC program:

void   (string str, ...)          print     = #1;
string (float val)                ftos      = #2;
entity ()                         spawn     = #3;

.float f1, f2, f3;

void() main = {
    local float ent_num;
    local entity ent;
    ent_num = 0;
    while (ent_num < 10) {
        ent_num = ent_num + 1;
        ent = spawn();
        print("spawned entity: ", ftos(ent_num), "\n");
    }
};

The program above is compiled with gmqcc and executed with qcvm.

When program progs.dat is executed without the change the output is:

spawned entity: 1
spawned entity: 2
spawned entity: 3
malloc(): invalid size (unsorted)
Aborted (core dumped)

When the same progs.dat is executed with the change in the PR the output is:

spawned entity: 1
spawned entity: 2
spawned entity: 3
spawned entity: 4
spawned entity: 5
spawned entity: 6
spawned entity: 7
spawned entity: 8
spawned entity: 9
spawned entity: 10
zus-dev commented 1 year ago

@graphitemaster @Blub Please take a look.

Blub commented 1 year ago

I guess none of the tests ever spawned more than 3 entities. Nice catch, thanks.