SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 28/06/2016, 04:47 PM   #1
Lorenc_
High-roller
 
Lorenc_'s Avatar
 
Join Date: Jan 2010
Location: Australia
Posts: 3,793
Reputation: 1179
Default Server changing variables randomly (memory bug?)

Hello, it's been a while!

My server is experiencing an issue where after a period of time, variables are just randomly being changed/bugged. I believe it is a memory issue?

I will offer a reward (money if I am allowed) for anyone that helps me nail down this issue.


Anyway, for example:

pawn Code:
new iWeather = 0;

// a timer every 24 minutes
iWeather = randomEx( 10, 12 )

// another timer every second
SetPlayerWeather( playerid, iWeather );

Now randomly, out of nowhere. This weather variable is being changed to some negative value and bugging out the server.

ANOTHER EXAMPLE:
pawn Code:
/* ** Random Messages ** */
static const
    g_randomMessages[ 45 ] [ 137 ] =
    {
        { "{8ADE47}Stephanie:"COL_WHITE" You can buy ropes at Supa Save or a 24/7 store to tie people up!" },
        { "{8ADE47}Stephanie:"COL_WHITE" Save us on your favourites so you don't miss out on the action!" },
        // 43 others ...
    }
;

public timer( )
{
    if( ( g_iTime = gettime( ) ) > g_randomMessageTick ) {
        SendClientMessageToAll( -1, g_randomMessages[ random( sizeof( g_randomMessages ) ) ] );
        g_randomMessageTick = g_iTime + 30;
    }
}

This constant array only just dumps random messages in my server periodically. However, it sometimes only just prints NOTHING. Like literally, the constant value is changed by itself during runtime.

-----

Advanced

according to a stack trace, seems that last line before my server crashes is here
https://github.com/compuphase/pawn/b...amx/amx.c#L582

-----

This issue occurs to many variables and consequentially, the server crashes.

Crashdetect provides me some information such as:
Code:
[10:42:56] [debug] AMX backtrace:
[10:42:56] [debug] #0 native memcpy () from samp03svr
But when I comment the code, other things get even more buggy. I've rolled back to an older version of my server and this still persists oddly.

If you know any potential reasons for this to cause (or fixes), please tell me.

I'll reward contributors that help me to fix the issues.


Ask me if you need more information, and what kind of information.
__________________
Join the best Cops And Robbers in SA-MP, today. svr.sfcnr.com:7777

Lorenc_ is offline   Reply With Quote
Old 28/06/2016, 05:09 PM   #2
SyS
High-roller
 
SyS's Avatar
 
Join Date: Oct 2015
Posts: 1,939
Reputation: 504
Default Re: Server changing variables randomly (memory bug?)

Instead of using randomex try this raw method(im not sure if it will work but you can try)
PHP Code:
srandom(a,b)
{
if(
a>b)
{
return 
random(a-b+1)+b;
}

return 
random(b-a+1)+a;

SyS is offline   Reply With Quote
Old 28/06/2016, 05:21 PM   #3
Vince
Spam Machine
 
Vince's Avatar
 
Join Date: Sep 2007
Location: Belgium
Posts: 10,067
Reputation: 2660
Default Re: Server changing variables randomly (memory bug?)

Variables being overwritten is nearly always caused by a stack or buffer overflow; putting more things on the stack than it has room to store, or somehow storing a larger string in a variable than its capacity allows.
__________________
Vince is offline   Reply With Quote
Old 28/06/2016, 11:44 PM   #4
PrO.GameR
Gangsta
 
PrO.GameR's Avatar
 
Join Date: Oct 2012
Posts: 694
Reputation: 121
Default Re: Server changing variables randomly (memory bug?)

Well something is leaking for sure, but it's a needle in the haystack, you yourself should know better when it started happening and what you've added back then, trace recursive functions and such. An observation I made was that it's very easy to ruin data with memcpy, check for memcpy's with incorrect sizes, as those can easily overwrite your data if you give them incorrect numbers.
__________________
Blueberry Prison Roleplay will be back soon!
Follow the forums for more information about opening day.

Forums
PrO.GameR is offline   Reply With Quote
Old 29/06/2016, 12:54 AM   #5
Lorenc_
High-roller
 
Lorenc_'s Avatar
 
Join Date: Jan 2010
Location: Australia
Posts: 3,793
Reputation: 1179
Default Re: Server changing variables randomly (memory bug?)

Thank you for the replies. Hope y'all don't mind me asking, it's imperative I nail it to the last detail.

Stack overflow?

What's the most common type of stack overflow? Doesn't the compiler often suggest a stack overflow after compilation?

Buffer overflow?

Are there more examples of a buffer overflow than just this?

pawn Code:
new
    szString[ 8 ];

format( szString, 16, "%s", "the lazy dog jumps over lorenc's fence" );
__________________
Join the best Cops And Robbers in SA-MP, today. svr.sfcnr.com:7777

Lorenc_ is offline   Reply With Quote
Old 29/06/2016, 02:27 AM   #6
Golimad
Huge Clucker
 
Golimad's Avatar
 
Join Date: Jan 2014
Location: Morocco
Posts: 251
Reputation: 36
Default Re: Server changing variables randomly (memory bug?)

Did you run your server on localhost to check if it does the same thing?
Golimad is offline   Reply With Quote
Old 29/06/2016, 05:49 AM   #7
SecretBoss
Gangsta
 
SecretBoss's Avatar
 
Join Date: Dec 2014
Location: Greece
Posts: 807
Reputation: 106
Default Re: Server changing variables randomly (memory bug?)

Buffer overflow means that your string has more cells than it can hold for example

Code:
CMD:test(playerid)
{	
	new message[100];
	if(sscanf(params, "s[144]", message)) return 0;
	
	new string[100];
	format(string, sizeof(string), "%s from id %i", message, playerid);
	SendClientMessage(playerid, -1, string);
	return 1;
}
I remember facing such problems but the server never crashed

I hope my info helped you out
__________________
SecretBoss is offline   Reply With Quote
Old 29/06/2016, 06:27 AM   #8
iKarim
Banned
 
Join Date: Oct 2015
Posts: 579
Reputation: 142
Default Re: Server changing variables randomly (memory bug?)

I got a problem similar to this once, I defined a global string and after some time the string changes to random characters but never fixed it, had to re-create the whole thing I made..
iKarim is offline   Reply With Quote
Old 29/06/2016, 03:09 PM   #9
Lorenc_
High-roller
 
Lorenc_'s Avatar
 
Join Date: Jan 2010
Location: Australia
Posts: 3,793
Reputation: 1179
Default Re: Server changing variables randomly (memory bug?)

Quote:
Originally Posted by SecretBoss View Post
Buffer overflow means that your string has more cells than it can hold for example

Code:
CMD:test(playerid)
{	
	new message[100];
	if(sscanf(params, "s[144]", message)) return 0;
	
	new string[100];
	format(string, sizeof(string), "%s from id %i", message, playerid);
	SendClientMessage(playerid, -1, string);
	return 1;
}
I remember facing such problems but the server never crashed

I hope my info helped you out
Sscanf automatically discards the excess hence the warning (afaik)? I don't think we're even meant to have only "s[144]", am I wrong?

Quote:
Originally Posted by PawnHunter View Post
I got a problem similar to this once, I defined a global string and after some time the string changes to random characters but never fixed it, had to re-create the whole thing I made..
Very similar, if not, the same issue.

Quote:
Originally Posted by Golimad View Post
Did you run your server on localhost to check if it does the same thing?
Not yet, because it happens after a few hours.
__________________
Join the best Cops And Robbers in SA-MP, today. svr.sfcnr.com:7777

Lorenc_ is offline   Reply With Quote
Old 30/06/2016, 08:05 AM   #10
SecretBoss
Gangsta
 
SecretBoss's Avatar
 
Join Date: Dec 2014
Location: Greece
Posts: 807
Reputation: 106
Default Re: Server changing variables randomly (memory bug?)

I run some tests because I wanted to make a function to continue messages to a new line if they are very long and I received such messages when the string was too long

Logs:

Code:
[19:43:30] [join] SecretBoss has joined the server (0:192.168.2.2)
[19:43:50] sscanf warning: String buffer overflow.
[19:43:53] sscanf warning: String buffer overflow.
[19:43:54] [part] SecretBoss has left the server (0:2)
[19:44:18] [connection] 192.168.2.2:55307 requests connection cookie.
[19:44:19] [connection] incoming connection: 192.168.2.2:55307 id: 0
[19:44:19] [join] SecretBoss has joined the server (0:192.168.2.2)
[19:44:33] sscanf warning: String buffer overflow.
[19:44:38] sscanf warning: String buffer overflow.
[19:49:32] sscanf warning: String buffer overflow.
[19:49:34] [part] SecretBoss has left the server (0:2)
__________________
SecretBoss 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
Finding right variables and randomly choosing one of them Aa12 Scripting Help 4 25/03/2016 07:42 AM
Can't create big string variables? Running out of memory? Shetch Scripting Help 2 19/06/2015 03:47 AM
Variables are lagging or taking memory? PaulDinam Scripting Help 1 05/03/2013 05:52 PM
Varibles Changing Randomly. Kyle Help Archive 2 18/02/2011 06:43 PM
Memory using in variables, booleans and enums Jochemd Help Archive 0 05/06/2010 04:52 PM


All times are GMT. The time now is 10:17 AM.


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