alliedmodders / amxmodx

AMX Mod X - Half-Life 1 Scripting and Administration
http://www.amxmodx.org/
497 stars 201 forks source link

compiler bug #927

Open Giferns opened 3 years ago

Giferns commented 3 years ago

Problematic code:

#include <amxmodx>

enum _:CATEGORIES_ENUM {
    CATEGORY__FREE,
    CATEGORY__VIP,
    CATEGORY__PREMIUM,
    CATEGORY__GIRL,
    CATEGORY__HOLIDAY,
    CATEGORY__END
}

#define INVALID_CATEGORY_POS -1

new g_iFirstPos[CATEGORIES_ENUM] = { INVALID_CATEGORY_POS, ... }
new g_iFirstPos2[ 5 ] = { INVALID_CATEGORY_POS, ... }

public plugin_init() {
    for(new i; i < CATEGORIES_ENUM; i++) {
        log_to_file("ArrayDebug.log", "g_iFirstPos[%i]: %i", i, g_iFirstPos[i])
    }

    for(new i; i < CATEGORIES_ENUM; i++) {
        log_to_file("ArrayDebug.log", "g_iFirstPos2[%i]: %i", i, g_iFirstPos2[i])
    }
}

Compiled, then decompiled:

new g_iFirstPos[5] =
{
    -1, 0, 0, 0, 0
};
new g_iFirstPos2[5] =
{
    -1, ...
};
public plugin_init()
{
    new i;
    while (i < 5)
    {
        log_to_file("ArrayDebug.log", "g_iFirstPos[%i]: %i", i, g_iFirstPos[i]);
        i++;
    }
    new i;
    while (i < 5)
    {
        log_to_file("ArrayDebug.log", "g_iFirstPos2[%i]: %i", i, g_iFirstPos2[i]);
        i++;
    }
    return 0;
}

As you can see, only first cell of g_iFirstPos was set to -1 Bug exists only if CATEGORIES_ENUM is used as a size for g_iFirstPos If i replace CATEGORIES_ENUM to 5 all is ok (all cells got -1) If i use below construction, all is also ok

enum _:CATEGORIES_ENUM {
    CATEGORY__FREE,
    CATEGORY__VIP,
    CATEGORY__PREMIUM,
    CATEGORY__GIRL,
    CATEGORY__HOLIDAY,
    CATEGORY__END
}

new g_iFirstPos[CATEGORY__END] = { INVALID_CATEGORY_POS, ... }
afwn90cj93201nixr2e1re commented 3 years ago

There are a lot of bugs with enums, you must use const Variable = CATEGORY__END; to pretend beeing a constant variable, we need to update a pawn core as well to fix them, in some cases it is breakable.