Raised This Month: $ Target: $400
 0% 

Stack error with hook_cvar_change


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
luxor
Member
Join Date: Jan 2014
Old 12-26-2015 , 18:15   Stack error with hook_cvar_change
Reply With Quote #1

hello, i receive stack error when i'm trying to set any value for rcon_password.
what i was doing wrong ?

Code:
#include <amxmisc>

#if AMXX_VERSION_NUM < 183 
    #assert AMX Mod X v1.8.3 or later library required!
#endif 

public plugin_init()
{
	register_plugin
	(
		.plugin_name = "Block changing rcon_password",
		.version     = "1.0",
		.author      = "lüxor"
	);
	
	hook_cvar_change(get_cvar_pointer("rcon_password"), "setDefaultValue");
}

public setDefaultValue(PointerCvar, const OldValue[], const NewValue[])
{
	set_pcvar_string(PointerCvar, OldValue);
}

Console :
Code:
] restart
Server logging data to file logs\L1227013.log
L 12/27/2015 - 01:11:05: -------- Mapchange to de_dust2 --------
[AMXX] Loaded 1 admin from file
Executing AMX Mod X Configuration File 
Scrolling message displaying frequency: 10:00 minutes
] rcon_password
"rcon_password" is "123"
] rcon_password meow
L 12/27/2015 - 01:12:02: [AMXX] Displaying debug trace (plugin "block_changing_rcon_pass.amxx", version "1.0")
L 12/27/2015 - 01:12:02: [AMXX] Run time error 3: stack error
luxor is offline
addicted2sex
Senior Member
Join Date: May 2009
Location: localhost
Old 12-26-2015 , 20:17   Re: Stack error with hook_cvar_change
Reply With Quote #2

Quote:
Originally Posted by luxor View Post
hello, i receive stack error when i'm trying to set any value for rcon_password.
what i was doing wrong ?

Code:
#include <amxmisc>

#if AMXX_VERSION_NUM < 183 
    #assert AMX Mod X v1.8.3 or later library required!
#endif 

public plugin_init()
{
	register_plugin
	(
		.plugin_name = "Block changing rcon_password",
		.version     = "1.0",
		.author      = "lüxor"
	);
	
	hook_cvar_change(get_cvar_pointer("rcon_password"), "setDefaultValue");
}

public setDefaultValue(PointerCvar, const OldValue[], const NewValue[])
{
	set_pcvar_string(PointerCvar, OldValue);
}

Console :
Code:
] restart
Server logging data to file logs\L1227013.log
L 12/27/2015 - 01:11:05: -------- Mapchange to de_dust2 --------
[AMXX] Loaded 1 admin from file
Executing AMX Mod X Configuration File 
Scrolling message displaying frequency: 10:00 minutes
] rcon_password
"rcon_password" is "123"
] rcon_password meow
L 12/27/2015 - 01:12:02: [AMXX] Displaying debug trace (plugin "block_changing_rcon_pass.amxx", version "1.0")
L 12/27/2015 - 01:12:02: [AMXX] Run time error 3: stack error
Because you're changing the cvar itself when it is changed. The situation is the following:
  • "rcon_password" is changed to "meow"
  • setDefaultValue() is called
  • setDefaultValue is setting "rcon_password" to the old value and because of the change setDefaultValue() is called again

because of that an "infinite recursion" appears and the stack overflows
__________________
Let 7he gr0ovE r3Lease y0ur m!nd

Last edited by addicted2sex; 12-26-2015 at 20:32.
addicted2sex is offline
luxor
Member
Join Date: Jan 2014
Old 12-26-2015 , 20:27   Re: Stack error with hook_cvar_change
Reply With Quote #3

oh... i see, that explains all, thx
luxor is offline
PartialCloning
Senior Member
Join Date: Dec 2015
Old 12-26-2015 , 20:58   Re: Stack error with hook_cvar_change
Reply With Quote #4

Code:
#include <amxmisc>

#if AMXX_VERSION_NUM < 183 
    #assert AMX Mod X v1.8.3 or later library required!
#endif 

new cvarhook:rconhandle;
public plugin_init()
{
  	register_plugin
  	(
  	  	.plugin_name = "Block changing rcon_password",
  	  	.version     = "1.0",
  	  	.author      = "lüxor"
  	);

  	rconhandle = hook_cvar_change(get_cvar_pointer("rcon_password"), "setDefaultValue");
}

public setDefaultValue(PointerCvar, const OldValue[], const NewValue[])
{
  	 if(!equal(NewValue, OldValue))
  	{
  	  	disable_cvar_hook(rconhandle);
  	  	set_pcvar_string(PointerCvar, OldValue);
  	  	enable_cvar_hook(rconhandle);
  	}
}

Last edited by PartialCloning; 12-27-2015 at 09:26.
PartialCloning is offline
luxor
Member
Join Date: Jan 2014
Old 12-27-2015 , 05:28   Re: Stack error with hook_cvar_change
Reply With Quote #5

thanks for try to help, but your change won't affect the problem.
anyway i solved it, thanks for explaining addicted2sex
luxor is offline
PartialCloning
Senior Member
Join Date: Dec 2015
Old 12-27-2015 , 09:25   Re: Stack error with hook_cvar_change
Reply With Quote #6

I didn't expect it to not work with PLUGIN_HANDLED; I edited it now, it should work. I know you already found your own solution, but:

1. The code above might be simpler if you used a different method.
2. You did not share your solution, meaning if someone looks up a similar problem they won't know how to fix it, now they know.

Last edited by PartialCloning; 12-27-2015 at 09:26.
PartialCloning is offline
luxor
Member
Join Date: Jan 2014
Old 12-27-2015 , 19:51   Re: Stack error with hook_cvar_change
Reply With Quote #7

Quote:
Originally Posted by PartialCloning View Post
I didn't expect it to not work with PLUGIN_HANDLED; I edited it now, it should work. I know you already found your own solution, but:

1. The code above might be simpler if you used a different method.
2. You did not share your solution, meaning if someone looks up a similar problem they won't know how to fix it, now they know.
Code:
#include <amxmisc>

#if AMXX_VERSION_NUM < 183 
    #assert AMX Mod X v1.8.3 or later library required!
#endif

new RconPasswordValue[64];

public plugin_init()
{
	register_plugin
	(
		.plugin_name = "Block changing rcon_password",
		.version     = "1.0",
		.author      = "lüxor"
	);
	new RconPasswordCvarPointer = get_cvar_pointer("rcon_password");
	get_pcvar_string(RconPasswordCvarPointer, RconPasswordValue, charsmax(RconPasswordValue));
	hook_cvar_change(RconPasswordCvarPointer, "setDefaultValue");
}

public setDefaultValue(PointerCvar, const OldValue[], const NewValue[])
{
	if ( !equal(NewValue, RconPasswordValue, charsmax(RconPasswordValue)) )
	{
		set_pcvar_string(PointerCvar, OldValue);
	}
}
luxor is offline
WildCard65
Veteran Member
Join Date: Aug 2013
Location: Canada
Old 12-27-2015 , 20:52   Re: Stack error with hook_cvar_change
Reply With Quote #8

Quote:
Originally Posted by luxor View Post
Code:
#include <amxmisc>

#if AMXX_VERSION_NUM < 183 
    #assert AMX Mod X v1.8.3 or later library required!
#endif

new RconPasswordValue[64];

public plugin_init()
{
	register_plugin
	(
		.plugin_name = "Block changing rcon_password",
		.version     = "1.0",
		.author      = "lüxor"
	);
	new RconPasswordCvarPointer = get_cvar_pointer("rcon_password");
	get_pcvar_string(RconPasswordCvarPointer, RconPasswordValue, charsmax(RconPasswordValue));
	hook_cvar_change(RconPasswordCvarPointer, "setDefaultValue");
}

public setDefaultValue(PointerCvar, const OldValue[], const NewValue[])
{
	if ( !equal(NewValue, RconPasswordValue, charsmax(RconPasswordValue)) )
	{
		set_pcvar_string(PointerCvar, OldValue);
	}
}
That is a sane solution in my opinion.

Last edited by WildCard65; 12-27-2015 at 20:53.
WildCard65 is offline
luxor
Member
Join Date: Jan 2014
Old 12-28-2015 , 04:47   Re: Stack error with hook_cvar_change
Reply With Quote #9

ik, but i told him that i solved it

Last edited by luxor; 12-28-2015 at 04:47.
luxor is offline
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

Forum Jump


All times are GMT -4. The time now is 17:52.


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