SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 28/11/2019, 08:08 AM   #1
Hzgamer28
Little Clucker
 
Join Date: Apr 2019
Posts: 1
Reputation: 0
Default array index out of bounds

i have same error with command /pvlock
[15:06:28] [debug] Run time error 4: "Array index out of bounds"
[15:06:28] [debug] Attempted to read/write array element at negative index -400
[15:06:28] [debug] AMX backtrace:
[15:06:28] [debug] #0 00327a24 in public cmd_pvlock (0, 180555344) in NGRP.amx
[15:06:28] [debug] #1 native CallLocalFunction () in samp-server.exe
[15:06:28] [debug] #2 00031fec in public OnPlayerCommandText (0, 180555312) in NGRP.amx

this is command /pvlock
CMDvlock(playerid, params[])
{
new string[128];
new vehicleid = GetPlayerVehicleID(playerid);
new Float: x, Float: y, Float: z;
if(PlayerInfo[playerid][pVehicleKeysFrom] != INVALID_PLAYER_ID)
{
new ownerid = PlayerInfo[playerid][pVehicleKeysFrom];
if(IsPlayerConnected(ownerid))
{
new d = PlayerInfo[playerid][pVehicleKeys];
if(PlayerVehicleInfo[ownerid][d][pvId] != INVALID_PLAYER_VEHICLE_ID) GetVehiclePos(PlayerVehicleInfo[ownerid][d][pvId], x, y, z);
if(IsPlayerInRangeOfPoint(playerid, 3.0, x, y, z))
{
if(PlayerVehicleInfo[ownerid][d][pvLock] > 0)
{
if(PlayerVehicleInfo[ownerid][d][pvLocksLeft] <= 0) {
SendClientMessageEx(playerid, COLOR_GREY, "The lock has been damaged as result of a lock pick!");
return 1;
}
if(PlayerVehicleInfo[ownerid][d][pvBeingPickLocked]) {
SendClientMessageEx(playerid, COLOR_GREY, "This vehicle cannot be locked/unlocked right now.");
return 1;
}
if(PlayerVehicleInfo[ownerid][d][pvLocked] == 0)
{
format(string, sizeof(string), "~r~%s LOCKED", GetVehicleName(vehicleid));
GameTextForPlayer(playerid, string, 3000, 6);
PlayerPlaySound(playerid, 1145, 0.0, 0.0, 0.0);
PlayerVehicleInfo[ownerid][d][pvLocked] = 1;
LockPlayerVehicle(ownerid, PlayerVehicleInfo[ownerid][d][pvId], PlayerVehicleInfo[ownerid][d][pvLock]);
return 1;
}
else
{
format(string, sizeof(string), "~r~%s UNLOCKED", GetVehicleName(vehicleid));
GameTextForPlayer(playerid, string, 3000, 6);
PlayerVehicleInfo[ownerid][d][pvLocked] = 0;
UnLockPlayerVehicle(ownerid, PlayerVehicleInfo[ownerid][d][pvId], PlayerVehicleInfo[ownerid][d][pvLock]);
return 1;
}
}
else
{
SendClientMessageEx(playerid, COLOR_GREY, " You don't have a lock system installed on this vehicle.");
return 1;
}
}
}
}
for(new d = 0 ; d < MAX_PLAYERVEHICLES; d++)
{
if(PlayerVehicleInfo[playerid][d][pvId] != INVALID_PLAYER_VEHICLE_ID) GetVehiclePos(PlayerVehicleInfo[playerid][d][pvId], x, y, z);
if(IsPlayerInRangeOfPoint(playerid, 3.0, x, y, z))
{
if(PlayerVehicleInfo[playerid][d][pvLocksLeft] <= 0) {
SendClientMessageEx(playerid, COLOR_GREY, "The lock has been damaged as result of a lock pick, please buy a new one!");
return 1;
}
if(PlayerVehicleInfo[playerid][d][pvBeingPickLocked]) {
SendClientMessageEx(playerid, COLOR_GREY, "This vehicle cannot be locked/unlocked right now.");
return 1;
}
if(PlayerVehicleInfo[playerid][d][pvLock] > 0 && PlayerVehicleInfo[playerid][d][pvLocked] == 0)
{
format(string, sizeof(string), "~r~%s LOCKED", GetVehicleName(vehicleid));
GameTextForPlayer(playerid, string, 3000, 6);
PlayerVehicleInfo[playerid][d][pvLocked] = 1;
LockPlayerVehicle(playerid, PlayerVehicleInfo[playerid][d][pvId], PlayerVehicleInfo[playerid][d][pvLock]);
return 1;
}
else if(PlayerVehicleInfo[playerid][d][pvLock] > 0 && PlayerVehicleInfo[playerid][d][pvLocked] == 1)
{
format(string, sizeof(string), "~r~%s UNLOCKED", GetVehicleName(vehicleid));
GameTextForPlayer(playerid, string, 3000, 6);
PlayerVehicleInfo[playerid][d][pvLocked] = 0;
UnLockPlayerVehicle(playerid, PlayerVehicleInfo[playerid][d][pvId], PlayerVehicleInfo[playerid][d][pvLock]);
return 1;

}
SendClientMessageEx(playerid, COLOR_GREY, " You don't have a lock system installed on this vehicle.");
return 1;
}
}
SendClientMessageEx(playerid, COLOR_GREY, " You are not near any vehicle that you own.");
return 1;
}

Does anyone know how to fix it ( sorry my bad english )
Hzgamer28 is offline   Reply With Quote
Old 28/11/2019, 08:28 AM   #2
Pottus
High-roller
 
Pottus's Avatar
 
Join Date: Jun 2012
Posts: 4,899
Reputation: 1321
Default Re: array index out of bounds

Scripting help is for people who know how to script and have a problem, not to debug downloaded scripts that you are not qualified to use nevertheless....

There isn't enough information here but the clues point to another function call inside this function. The give away is referencing index -400 that is used when getting a vehicle modelid in order to line up with an array. My guess is the problem is going to be in this function here.

Code:
LockPlayerVehicle(ownerid, PlayerVehicleInfo[ownerid][d][pvId], PlayerVehicleInfo[ownerid][d][pvLock]);
But the problem could extend past this with lack of adequate checks resulting in undefined behavior which I am leaning towards.

-d3 when you compile.
Pottus is offline   Reply With Quote
Old 28/11/2019, 12:01 PM   #3
Calisthenics
Gangsta
 
Join Date: May 2018
Posts: 874
Reputation: 156
Default Re: array index out of bounds

Verify that player is IN vehicle otherwise `GetPlayerVehicleID` will return 0. Then `GetVehicleName` is called and subtracts 400 from vehicleid resulting in negative index -400.

Very weird that crashdetect reported code directly in the command and not in a child function.
Calisthenics is offline   Reply With Quote
Reply

Thread Tools
Display Modes

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Array index out of bounds. Kortecs Scripting Help 1 04/02/2019 09:30 PM
[Ajuda] array index out of bounds elTioFrame Português/Portuguese 5 20/12/2018 05:18 AM
Array index out of bounds Younes44 Scripting Help 6 11/08/2016 02:08 PM
array index out of bounds Pawnify Scripting Help 5 04/10/2014 07:34 PM
Array index out of bounds Snipa Help Archive 1 05/04/2011 04:41 AM


All times are GMT. The time now is 10:18 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.