AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   get user name (https://forums.alliedmods.net/showthread.php?t=298631)

D3XT3R 06-18-2017 10:16

get user name
 
Hi, i have some bugs with this script so can tell me what is wroung?

Script to get user name, red line is the problem in logs
PHP Code:

stock get_user_name_ex(id)
{
    new 
szName[33];
    
get_user_name(idszNamecharsmax(szName));

    
replace_all(szNamecharsmax(szName), "'""\'");
    
replace_all(szNamecharsmax(szName), "^"", "\^"");
    
replace_all(szNamecharsmax(szName), ".""");
    
    return 
szName;


Line Error in Log (1842)
Quote:

replace_all(szName, charsmax(szName), "'", "\'");
Log:
Quote:

L 06/18/2017 - 11:33:03: replace() buffer not big enough (12>=11)
L 06/18/2017 - 11:33:03: [AMXX] Displaying debug trace (plugin "gunxpmod.amxx", version "4.3.5")
L 06/18/2017 - 11:33:03: [AMXX] Run time error 10: native error (native "replace")
L 06/18/2017 - 11:33:03: [AMXX] [0] string.inc::replace_all (line 340)
L 06/18/2017 - 11:33:03: [AMXX] [1] gunxpmod.sma::get_user_name_ex (line 1842)
L 06/18/2017 - 11:33:03: [AMXX] [2] gunxpmod.sma::check_level (line 881)
L 06/18/2017 - 11:33:03: [AMXX] [3] gunxpmod.sma::native_set_user_xp (line 1757)
L 06/18/2017 - 11:33:03: Unhandled dynamic native error

aron9forever 06-18-2017 10:23

Re: get user name
 
Problem is that sometimes you are replacing one character with two characters, and let's say a player has a name of length 32 which is max, but 5 of the chars are ', then you need length 37 to store the replaced version.

easy fix is to change szName[33] (which should be 32 anyways) to szName[64] but this will sometimes clip the player's name if you try to set his name to the newly generated name, since it's too long for the game


also, you must never return a string in AMXX
you must pass the array via reference which will edit the original array

PHP Code:


public using_the_stock(id) {
    new 
theName[64];
    
get_user_name_ex(idtheNamecharsmax(theName));
}

stock get_user_name_ex(idszName[], len)
{
    
get_user_name(idszNamelen);

    
replace_all(szNamelen"'""\'");
    
replace_all(szNamelen"^"", "\^"");
    
replace_all(szNamelen".""");



D3XT3R 06-18-2017 10:31

Re: get user name
 
best option is i remove replacing characters or i change chars to 64

aron9forever 06-18-2017 10:41

Re: get user name
 
Quote:

Originally Posted by D3XT3R (Post 2529611)
best option is i remove replacing characters or i change chars to 64

see my final edited answer, I've edited it quite a lot maybe you missed some stuff

may I ask why you're doing this filtering?

D3XT3R 06-18-2017 10:52

Re: get user name
 
Quote:

Originally Posted by aron9forever (Post 2529614)
see my final edited answer, I've edited it quite a lot maybe you missed some stuff

may I ask why you're doing this filtering?

using a save for gunxpmod for ip / nick / steamid for save thier xp and filtering for illegal names whitch has ² ... whitch mysql system not allowded them...
ps: i didnt understand any thing from yours answer and why no return in AMXX?

D3XT3R 06-18-2017 10:53

Re: get user name
 
PHP Code:

stock get_user_name_ex(id)
{
    new 
szName[33];
    
get_user_name(idszNamecharsmax(szName));

    
replace_all(szNamecharsmax(szName), "'""\'");
    
replace_all(szNamecharsmax(szName), "^"", "\^"");
    
replace_all(szNamecharsmax(szName), ".""");
    
    return 
szName;
}

stock get_user_ip_ex(id)
{
    new 
szIP[33];
    
get_user_ip(idszIPcharsmax(szIP), 1);
    
    return 
szIP;
}

stock get_user_authid_ex(id)
{
    new 
szAuthid[33];
    
get_user_authid(idszAuthid32);
    
    return 
szAuthid;



fysiks 06-18-2017 13:36

Re: get user name
 
Quote:

Originally Posted by D3XT3R (Post 2529617)
ps: i didnt understand any thing from yours answer and why no return in AMXX?

There is a reason that get_user_name() requires the string to be passed as an argument with a length argument. You should do that exact same thing with your custom name function.

PRoSToTeM@ 06-18-2017 18:02

Re: get user name
 
Quote:

Originally Posted by D3XT3R (Post 2529618)
PHP Code:

replace_all(szNamecharsmax(szName), "."""); 


This is very bad, because it makes "Pla.yer" and "Player" nicknames the same, so there will be a conflict.

aron9forever 06-19-2017 08:22

Re: get user name
 
Here's a stock for preventing mysql injection
It prevents all problems but requires a buffer size x3 the original size

here's how you use it

PHP Code:

    new nume[96];
    
get_user_name(idnumecharsmax(nume));
    
mysql_escape_string(numecharsmax(nume));
    
//nume is now safe to use in a query

stock mysql_escape_string(dest[], len) {
    
replace_all(destlen"\\""\\\\");
    
replace_all(destlen"\0""\\0");
    
replace_all(destlen"\n""\\n");
    
replace_all(destlen"\r""\\r");
    
replace_all(destlen"\x1a""\Z");
    
replace_all(destlen"'""\'");
    
replace_all(destlen"^"", "\^"");


theres no need to replace players names to be SQL safe, just filter every user input before running queries
I'll say it one more time; returning strings the way you're trying to is not supported in pawn
I doubt a full explanation of why would do you any good(and I can't give it to you because I don't know) but the fact that you can't find a single function that returns a string in the entirety of amxmodx should be a hint.

D3XT3R 06-19-2017 10:20

Re: get user name
 
You are right!


All times are GMT -4. The time now is 22:44.

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