Raised This Month: $51 Target: $400
 12% 

Index out of bounds


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Shadows Adi
AlliedModders Donor
Join Date: Aug 2019
Location: Romania
Old 03-03-2020 , 14:05   Index out of bounds
Reply With Quote #1

Hello,
I have a crosshair menu plugin, sometimes it causes Segmentation Fault on my Cs 1.6 dedicated server.

Error Log:
PHP Code:
03/03/2020 20:55:20: [AMXXDisplaying debug trace (plugin "tinta.amxx"version "1.1")
03/03/2020 20:55:20: [AMXXRun time error 4index out of bounds
03
/03/2020 20:55:20: [AMXX] [0tinta.sma::Event_CurWeapon (line 229)
FATA ERROR (shutting down): PF_MessageBegin_I: New message started when msg '78' has not been sent yet
Segmentation fault 
(core dumped
Code:
PHP Code:
#include <amxmodx>
#include <engine>
#include <cstrike>
#include <fakemeta>

new const SilahBilgi[25][3] = {
    {
9,52,1},
    {
12,1,4},
    {
5,32,5},
    {
14,1,6},
    {
6,100,7},
    {
4,90,8},
    {
13,1,9},
    {
10,120,10},
    {
7,100,11},
    {
6,100,12},
    {
4,90,14},
    {
4,90,15},
    {
6,100,16},
    {
10,120,17},
    {
10,120,19},
    {
3,200,20},
    {
5,32,21},
    {
4,90,22},
    {
10,120,23},
    {
11,2,25},
    {
8,35,26},
    {
4,90,27},
    {
2,90,28},
    {-
1,-1,29},
    {
7,100,30}
}
new 
scope[33];
new const 
sprites[6][4][64] = {
    {
"Tinta Normala","Txt Yolu","Sprite Yolu","Txt Adi"},
    {
"Tinta CS:GO","sprites/custom_cross2_fix.txt","sprites/csgo_crosshair_32.spr","custom_cross2_fix"},
    {
"Tinta Fixa","sprites/custom_cross_fix_yeni3.txt","sprites/csgo_nisan_yeni3.spr","custom_cross_fix_yeni3"},
    {
"Tinta Flusha ","sprites/custom_cross3_fix.txt","sprites/artinisan.spr","custom_cross3_fix"},
    {
"Tinta KennyS ","sprites/custom_cross5_fix4.txt","sprites/dahakucuk4.spr","custom_cross5_fix4"},
    {
"Tinta S1mple ","sprites/custom_cross_baslangic.txt","sprites/csgo_baslangic.spr","custom_cross_baslangic"}
}
new 
cross[33];
public 
plugin_init()
{
    
register_plugin("CS:GO Crosshair","1.1","KriTo");
    
register_event("CurWeapon","Event_CurWeapon","be","1=1");
    
register_clcmd("buyammo1","guncelle1");
    
register_clcmd("buyammo2","guncelle1");
    
register_clcmd("custom_cross2_fix","FakeSwitch");
    
register_clcmd("say /crosshair","crossmenu");
    
register_clcmd("say /tinta","crossmenu");
}
public 
client_disconnected(id){
    
cross[id] = 1;
}
public 
client_putinserver(id){
    
cross[id] = 1;
}
public 
client_PreThink(id)
{
    new 
button pev(idpev_oldbuttons);
    new 
oldbutton pev(idpev_button);
    if(
oldbutton IN_ATTACK2)
    {
        if(
button IN_ATTACK2)
            return 
PLUGIN_CONTINUE;

        new 
weapon get_user_weapon(id);
        if(
weapon == CSW_AUG || weapon == CSW_SG552)
        {
            if(
scope[id]==0)
                
scope[id]=1;
            else
                
scope[id]=0;
            
Guncelle(id);
        } else if(
weapon != CSW_AUG || weapon != CSW_SG552)
        {
            
scope[id]=0;
        }
    }
    return 
PLUGIN_CONTINUE;
}
public 
crossmenu(id)
{
    new 
Menu=menu_create("\yGO.LEAGUECS.RO -> \rCS:GO^n","crossdevam");
    
menu_additem(Menu,"Tinta Normala","1");
    for(new 
i=1;i<sizeof(sprites);i++)
    {
        new 
Item[128],text[12];
        
formatex(Item,127,"%s",sprites[i][0]);
        
num_to_str(i+1,text,11);
        
menu_additem(Menu,Item,text);
    }
    
menu_setprop(MenuMPROP_EXITMEXIT_ALL);
    
menu_display(id,Menu,0);
}
public 
crossdevam(idmenuitem)
{

    if( 
item == MENU_EXIT )
    {
        
menu_destroy(menu);
        return 
PLUGIN_HANDLED;
    }

    new 
data[6], iName[64];
    new 
accesscallback;
    
menu_item_getinfo(menuitemaccessdata,5iName63callback);
    new 
key str_to_num(data);
    
cross[id] = key-1;
    if(
cross[id] == 0)
    {
        
message_begin(MSG_ONEget_user_msgid("HideWeapon"),  .player=id);
        
write_byte(1<<7);
        
message_end();
    }
    
client_print_color(idid"^3Ai ales ^4%s ",sprites[cross[id]][0])
    
Guncelle(id);
    return 
PLUGIN_HANDLED;
}
public 
guncelle1(id)
{
    
set_task(0.1,"Guncelle",id);
    return 
PLUGIN_CONTINUE;
}
public 
Guncelle(id)
{
    if(!
is_user_alive(id) || cross[id] == 0)
        return 
PLUGIN_HANDLED;
    new 
weapon get_user_weapon(id);
    if( 
weapon == CSW_AWP || weapon == CSW_SCOUT || weapon == CSW_SG550 || weapon == CSW_G3SG1)
        return 
PLUGIN_CONTINUE

    
new i;
    for(
i=0;i<sizeof(SilahBilgi);i++)
    {
        new 
yeniweap SilahBilgi[i][2];
        if(
weapon == yeniweap)
        {
            break;
        } else if(
i==sizeof(SilahBilgi)-1)
        {
            return 
PLUGIN_CONTINUE;
        }
    }
    
message_begin(MSG_ONEget_user_msgid("HideWeapon"),  .player=id);
    
write_byte(1<<6);
    
message_end();

    
message_begin(MSG_ONE,get_user_msgid("WeaponList"),   .player=id);
    
write_string(sprites[cross[id]][3]);
    
write_byte(SilahBilgi[i-1][0]);
    
write_byte(SilahBilgi[i-1][1]);
    
write_byte(-1);
    
write_byte(-1);
    
write_byte(6);
    
write_byte(0);
    
write_byte(2);
    
write_byte(0);
    
message_end();

    
message_begin(MSG_ONE,get_user_msgid("SetFOV"),        .player=id);
    if(
scope[id] == 1)
        
write_byte(55);
    else
        
write_byte(89);
    
message_end();

    new 
ammo,bpammo;
    
get_user_ammo(id,weapon,ammo,bpammo);

    
message_begin(MSG_ONE,get_user_msgid("CurWeapon"),    .player=id);
    
write_byte(1);
    
write_byte(2);
    
write_byte(ammo);
    
message_end();
    if(
weapon != CSW_KNIFE) {
        
message_begin(MSG_ONE,get_user_msgid("AmmoX"),      .player=id);
        
write_byte(SilahBilgi[i-1][0]);
        
write_byte(bpammo);
        
message_end();
    }
    if(
scope[id] == 0) {
        
message_begin(MSG_ONE,get_user_msgid("SetFOV"),        .player=id);
        
write_byte(90);
        
message_end();
    }

    return 
PLUGIN_HANDLED;
}
public 
FakeSwitch(const client)engclient_cmd(client,"weapon_shield");
public 
plugin_precache()
{
    for(new 
i=1;i<sizeof(sprites);i++)
    {
        
precache_generic(sprites[i][1]);
        
precache_generic(sprites[i][2]);
    }
}

public 
Event_CurWeapon(id)
{
    if(!
is_user_alive(id) || cross[id] == 0)
        return 
PLUGIN_HANDLED;
    new 
weapon read_data(2);
    if( 
weapon == CSW_AWP || weapon == CSW_SCOUT || weapon == CSW_SG550 || weapon == CSW_G3SG1)
        return 
PLUGIN_CONTINUE
    
new i;
    for(
i=0;i<sizeof(SilahBilgi);i++)
    {
        new 
yeniweap SilahBilgi[i][2];
        if(
weapon == yeniweap)
        {
            break;
        } else if(
i==sizeof(SilahBilgi)-1)
        {
            
message_begin(MSG_ONE,get_user_msgid("SetFOV"),        .player=id);
            
write_byte(90);
            
message_end();
            return 
PLUGIN_CONTINUE;
        }
    }
    
message_begin(MSG_ONEget_user_msgid("HideWeapon"),  .player=id);
    
write_byte(1<<6);
    
message_end();

    
message_begin(MSG_ONE,get_user_msgid("WeaponList"),   .player=id);
    
write_string(sprites[cross[id]][3]);
    
write_byte(SilahBilgi[i-1][0]);
    
write_byte(SilahBilgi[i-1][1]);
    
write_byte(-1);
    
write_byte(-1);
    
write_byte(6);
    
write_byte(0);
    
write_byte(2);
    
write_byte(0);
    
message_end();

    
message_begin(MSG_ONE,get_user_msgid("SetFOV"),        .player=id);
    if(
scope[id] == 1)
        
write_byte(55);
    else
        
write_byte(89);
    
message_end();

    new 
ammo,bpammo;
    
get_user_ammo(id,weapon,ammo,bpammo);

    
message_begin(MSG_ONE,get_user_msgid("CurWeapon"),    .player=id);
    
write_byte(1);
    
write_byte(2);
    
write_byte(ammo);
    
message_end();

    if(
weapon != CSW_KNIFE) {
        
message_begin(MSG_ONE,get_user_msgid("AmmoX"),      .player=id);
        
write_byte(SilahBilgi[i-1][0]);
        
write_byte(bpammo);
        
message_end();
    }
    if(
scope[id] == 0) {
        
message_begin(MSG_ONE,get_user_msgid("SetFOV"),        .player=id);
        
write_byte(90);
        
message_end();
    }


    return 
PLUGIN_HANDLED;

__________________


Accepting Paid Requests, contact PM.

MVP Of The Round View project on GITHUB / AlliedModders
CSGO REMAKE ~ CSGO MOD [STABLE + SOURCE CODE]
Shadows Adi is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 03-03-2020 , 14:24   Re: Index out of bounds
Reply With Quote #2

You can use MSG_ONE_UNRELIABLE instead of MSG_ONE to prevent such crashes.
__________________
OciXCrom is offline
Send a message via Skype™ to OciXCrom
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 03-03-2020 , 18:22   Re: Index out of bounds
Reply With Quote #3

Index out of bounds will occur when i == 0 and weapon == yeniweap. This is because when i == 0 and you reach the write_byte() code, you are trying to pass -1 as an array index.

Code:
    new i;     for(i=0;i<sizeof(SilahBilgi);i++)     {         new yeniweap = SilahBilgi[i][2];         if(weapon == yeniweap)         {
            //If i == 0 then i will hold 0 when exiting the loop.
            break;         } else if(i==sizeof(SilahBilgi)-1)         {             message_begin(MSG_ONE,get_user_msgid("SetFOV"),        .player=id);             write_byte(90);             message_end();             return PLUGIN_CONTINUE;         }     }     message_begin(MSG_ONE, get_user_msgid("HideWeapon"),  .player=id);     write_byte(1<<6);     message_end();     message_begin(MSG_ONE,get_user_msgid("WeaponList"),   .player=id);     write_string(sprites[cross[id]][3]);
    // SilahBilgi[ -1 ][ 0 ]  == index out of bounds
    write_byte(SilahBilgi[i-1][0]);
__________________

Last edited by Bugsy; 03-03-2020 at 18:23.
Bugsy is offline
Shadows Adi
AlliedModders Donor
Join Date: Aug 2019
Location: Romania
Old 03-04-2020 , 10:08   Re: Index out of bounds
Reply With Quote #4

PHP Code:
   // SilahBilgi[ -1 ][ 0 ]  == index out of bounds
    
write_byte(SilahBilgi[i-1][0]); <<-- 
Is this an index out of bounds?
__________________


Accepting Paid Requests, contact PM.

MVP Of The Round View project on GITHUB / AlliedModders
CSGO REMAKE ~ CSGO MOD [STABLE + SOURCE CODE]

Last edited by Shadows Adi; 03-04-2020 at 10:09. Reason: grammar
Shadows Adi is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 03-04-2020 , 18:32   Re: Index out of bounds
Reply With Quote #5

Yes, when the i variable holds a 0 value.
__________________
Bugsy is offline
Shadows Adi
AlliedModders Donor
Join Date: Aug 2019
Location: Romania
Old 03-05-2020 , 09:49   Re: Index out of bounds
Reply With Quote #6

Is this correct format?
PHP Code:
  write_byte(SilahBilgi[i-1][0]); -->> write_byte(SilahBilgi[i-1][1]); 
ant this should be deleted?
PHP Code:
 // SilahBilgi[ -1 ][ 0 ]  == index out of bounds 
__________________


Accepting Paid Requests, contact PM.

MVP Of The Round View project on GITHUB / AlliedModders
CSGO REMAKE ~ CSGO MOD [STABLE + SOURCE CODE]
Shadows Adi is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 03-05-2020 , 17:45   Re: Index out of bounds
Reply With Quote #7

Quote:
Originally Posted by Shadows Adi View Post
Is this correct format?
PHP Code:
  write_byte(SilahBilgi[i-1][0]); -->> write_byte(SilahBilgi[i-1][1]); 
ant this should be deleted?
PHP Code:
 // SilahBilgi[ -1 ][ 0 ]  == index out of bounds 
It's not a format issue, its bad logic which I already explained above. The error happens when holding CSW_P228 when Guncelle(id)is called.

Code:
new const SilahBilgi[25][3] = {
    {9,521  },
    {12,1,4},

Code:
    new i;     for(i=0;i<sizeof(SilahBilgi);i++)     {         new yeniweap = SilahBilgi[i][2];         if(weapon == yeniweap)         {
            //When player is holding P228 the loop will break
            //SilahBilgi[ 0 ][ 2 ] == 1 == CSW_P228
            break;         } else if(i==sizeof(SilahBilgi)-1)         {             message_begin(MSG_ONE,get_user_msgid("SetFOV"),        .player=id);             write_byte(90);             message_end();             return PLUGIN_CONTINUE;         }     }     message_begin(MSG_ONE, get_user_msgid("HideWeapon"),  .player=id);     write_byte(1<<6);     message_end();     message_begin(MSG_ONE,get_user_msgid("WeaponList"),   .player=id);     write_string(sprites[cross[id]][3]);
    // When the code reaches here SilahBilgi[i-1][0] results in SilahBilgi[ 0 - 1 ][0]
    // Which results in
    // SilahBilgi[ -1 ][ 0 ]  == index out of bounds
    write_byte(SilahBilgi[i-1][0]);

Try replacing
PHP Code:
    write_byte(SilahBilgi[i-1][0]);
    
write_byte(SilahBilgi[i-1][1]); 
with
PHP Code:
    write_byte(SilahBilgi[i][0]);
    
write_byte(SilahBilgi[i][1]); 
__________________

Last edited by Bugsy; 03-05-2020 at 21:14.
Bugsy is offline
Shadows Adi
AlliedModders Donor
Join Date: Aug 2019
Location: Romania
Old 03-07-2020 , 04:51   Re: Index out of bounds
Reply With Quote #8

Oh, thanks for explaination. I undestand now
__________________


Accepting Paid Requests, contact PM.

MVP Of The Round View project on GITHUB / AlliedModders
CSGO REMAKE ~ CSGO MOD [STABLE + SOURCE CODE]
Shadows Adi is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 03-07-2020 , 11:15   Re: Index out of bounds
Reply With Quote #9

Quote:
Originally Posted by Shadows Adi View Post
Oh, thanks for explaination. I undestand now
No problem. Maybe it would have helped if I explained what index out of bounds means.

When you define an array you declare it with a size:

new TheArray[ 10 ]

This creates an array with 10 cells. You can access these 10 cells using an index of 0 to 9. If you attempt to pass a value outside of that range, you get an index out of bounds error. In your case, you are trying to pass -1 which is below 0. You would get the same error if you pass any value greater than 9.
__________________
Bugsy is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 18:47.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode