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

name[MAX_NAME_LENGTH]


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
emsit
Member
Join Date: Apr 2015
Old 02-24-2018 , 14:46   name[MAX_NAME_LENGTH]
Reply With Quote #1

Hello everyone.

Does it make sense to MAX_NAME_LENGTH be defined at 32?
utf-8 characters use multiple bits. And my plugins crash if the user name contains utf-8 characters...
I give a small example.

output
Test_1: name[MAX_NAME_LENGTH]
strlen: 31, name: '🆃🅴🆂🆃_🅴🅼🆂
Test_2: name[128]
strlen: 37, name: '🆃🅴🆂🆃_🅴🅼🆂🅸🆃'
Test_3: name[MAX_NAME_LENGTH] + remove invalid UTF-8 char
strlen: 29, name: '🆃🅴🆂🆃_🅴🅼🆂'

result
Test_1: Standard use, missing apostroch, plugin crashes
Test_2: ok, no overflow protection.
Test_3: with overflow protection.

what do you think is the best choice to use?
Attached Files
File Type: sp Get Plugin or Get Source (test_4.sp - 801 views - 1.5 KB)
__________________

emsit is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 02-24-2018 , 16:31   Re: name[MAX_NAME_LENGTH]
Reply With Quote #2

As I recall, it's set to 32 bytes (well... 31 bytes plus nul terminator) because that's the limit in Source games.
__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
emsit
Member
Join Date: Apr 2015
Old 02-24-2018 , 16:50   Re: name[MAX_NAME_LENGTH]
Reply With Quote #3

But I think sourcemod works with a longer user name than 32 bytes. (I must test it.)

In any case, the string returned from GetClientName in conjunction with MAX_NAME_LENGTH returns the corrupted string.

Here is an example of a user with the name "████████████████████" (strlen = 60)

PHP Code:
char sName[MAX_NAME_LENGTH];
GetClientName(clientsNamesizeof(sName));

char escaped_name[MAX_NAME_LENGTH 2];
hDatabase.Escape(nameescaped_namesizeof(escaped_name));
FormatEx(querysizeof(query), "INSERT INTO test (server, steamid, name) VALUES ('%s', '%s', '%s');"ServerIPsteamidescaped_name); 
Missing apostrophe at the end!
Code:
L 02/22/2018 - 09:00:58: Query: INSERT INTO test (server, steamid, name) VALUES ('5.196.99.41:27015', '[U:1:XXXXXXXX]', '██████████?);
L 02/22/2018 - 09:00:58: Query failed! Incorrect string value: '\xE2' for column 'name' at row 1
__________________


Last edited by emsit; 02-26-2018 at 02:44.
emsit is offline
cravenge
Veteran Member
Join Date: Nov 2015
Location: Chocolate Factory
Old 02-24-2018 , 19:51   Re: name[MAX_NAME_LENGTH]
Reply With Quote #4

I'd go with the second as I use this whenever I collect others' names.
Code:
char sPlayerName[128];

Last edited by cravenge; 02-24-2018 at 19:52.
cravenge is offline
psychonic

BAFFLED
Join Date: May 2008
Old 02-25-2018 , 10:17   Re: name[MAX_NAME_LENGTH]
Reply With Quote #5

What game are you testing it?

In CS:GO, MAX_PLAYER_NAME_LENGTH in the engine is 128 bytes. However, in all other supported games, it's 32 bytes, which can even causes truncation in the engine itself, before SourceMod gets the name. (It looks like your test plugin is for L4D2).

That said, I don't remember if there's any good reason why we didn't increase SM's MAX_NAME_LENGTH to 128, to match CS:GO.
psychonic is offline
emsit
Member
Join Date: Apr 2015
Old 02-25-2018 , 14:05   Re: name[MAX_NAME_LENGTH]
Reply With Quote #6

Hi, my plugin is for L4D2.
I tested it, when I changed my name through "SetClientInfo (client, "name", name);" the name was shortened to 32bytes. So setting the variable to 128bytes will not solve my problem.
The only solution is to check the player name and remove the last invalid UTF-8 char. It's a shame that it sourcemod does not do it automatically.
__________________


Last edited by emsit; 02-25-2018 at 14:05.
emsit is offline
psychonic

BAFFLED
Join Date: May 2008
Old 02-25-2018 , 15:06   Re: name[MAX_NAME_LENGTH]
Reply With Quote #7

I'm still not fully understanding the issue.

What I do know:
  • A user's name in Steam can be 32 characters, including multi-byte characters.
  • A user's name in L4D2 gets cut off at 32 bytes internally in the engine. (m_Name on CGameClient).
  • This same behavior occurs if you manually set their name.
  • If the above happens, the cut off can be in the middle of a character, causing the corruption you mention.
  • So far, none of the above has anything to do with SourceMod. The engine can have a corrupted name.
When SourceMod first gets the name and when it caches updates to the name, it does attempt to strip off any invalid characters at the end of the name. Using MAX_NAME_LENGTH (32) should never cause further corrupt because the engine already only holds 32 bytes of the name. Regardless, GetClientName should not be able to return a name with a trailing invalid character, unless you are on an old version (SM 1.7 or lower).

Last edited by psychonic; 02-25-2018 at 16:20.
psychonic is offline
emsit
Member
Join Date: Apr 2015
Old 02-26-2018 , 02:38   Re: name[MAX_NAME_LENGTH]
Reply With Quote #8

Quote:
Originally Posted by psychonic View Post
When SourceMod first gets the name and when it caches updates to the name, it does attempt to strip off any invalid characters at the end of the name.
I suspect that in my case did not remove invalid characters. (https://forums.alliedmods.net/showpo...68&postcount=3)

Code:
sm version
 SourceMod Version Information:
    SourceMod Version: 1.8.0.6040
    SourcePawn Engine: SourcePawn 1.8, jit-x86 (build 1.8.0.6040)
    SourcePawn API: v1 = 4, v2 = 11
    Compiled on: Dec 19 2017 21:30:41
    Built from: https://github.com/alliedmodders/sourcemod/commit/b43ab42
    Build ID: 6040:b43ab42
    http://www.sourcemod.net/
I'm create a simple plugin to test my theory today. I will write you later.
__________________

emsit is offline
emsit
Member
Join Date: Apr 2015
Old 02-26-2018 , 11:43   Re: name[MAX_NAME_LENGTH]
Reply With Quote #9

Steam does not allow me to change my name to something more than 32 bytes.
So I can not test it..

But this problem still exists but is very rare.

Specifically, my bug:
Code:
L 02/22/2018 - 09:00:58: Query: INSERT INTO test (server, steamid, name) VALUES ('5.196.99.41:27015', '[U:1:XXXXXXXX]', '██████████?);
L 02/22/2018 - 09:00:58: Query failed! Incorrect string value: '\xE2' for column 'name' at row 1
was generated by this player who managed to change the username (maybe via api?) to more than 32bytes.
His profile: (name: ████████████████████)
https://steamidfinder.com/lookup/76561198013648641/
http://steamcommunity.com/profiles/76561198013648641
__________________

emsit is offline
backwards
AlliedModders Donor
Join Date: Feb 2014
Location: USA
Old 02-26-2018 , 11:49   Re: name[MAX_NAME_LENGTH]
Reply With Quote #10

Quote:
Originally Posted by emsit View Post
Steam does not allow me to change my name to something more than 32 bytes.
So I can not test it..

But this problem still exists but is very rare.

Specifically, my bug:
Code:
L 02/22/2018 - 09:00:58: Query: INSERT INTO test (server, steamid, name) VALUES ('5.196.99.41:27015', '[U:1:XXXXXXXX]', '██████████?);
L 02/22/2018 - 09:00:58: Query failed! Incorrect string value: '\xE2' for column 'name' at row 1
was generated by this player who managed to change the username (maybe via api?) to more than 32bytes.
His profile: (name: ████████████████████)
https://steamidfinder.com/lookup/76561198013648641/
http://steamcommunity.com/profiles/76561198013648641

Just a heads up, if they are changing their name like that to something you can't do in steam or console then they most likely are cheating to be able to set their name within the game engine. I'd say that's suspicious enough just to perm ban them. Most people using a cheat/exploit like this aren't only stopping at changing their name and are most likely taking advantage of other stuff hacking related.
__________________
I highly recommend joining the SourceMod Discord Server for real time support.
backwards 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 10:45.


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