Raised This Month: $7 Target: $400
 1% 

[Question] String concatenation limit


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
gabuch2
AlliedModders Donor
Join Date: Mar 2011
Location: Chile
Old 04-16-2020 , 22:48   [Question] String concatenation limit
Reply With Quote #1

Hello.

I'm trying to concatenate a very large string buffer, however, at one point the server is crashing due to segmentation fault. If I break early the plugin won't crash.

Is there a known limit or a safer method to do it?

Example code:
Code:
format(g_sResponseMessage, charsmax(g_sResponseMessage), "%s%s", g_sResponseMessage, message);
__________________
gabuch2 is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 04-16-2020 , 23:08   Re: [Question] String concatenation limit
Reply With Quote #2

format() has to allocate memory for a copy of the destination string so you should avoid doing that for long strings if at all possible. If you're not actually formatting anything, you should use add() if possible. Try that and see if it helps. There is a way to use formatex() (which doesn't create a copy of the destination string which also means you can't use the destination string as an input to the string like you're doing here).
__________________

Last edited by fysiks; 04-16-2020 at 23:10.
fysiks is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-16-2020 , 23:23   Re: [Question] String concatenation limit
Reply With Quote #3

If you do need formatting, you should use formatex(). As fysiks mentioned you cannot use the destination string as an input, but you can handle it by setting the position in the buffer that you are setting text/formatting to:
PHP Code:
new szBuffer32 ] , iPos;
    
iPos formatexszBuffer charsmaxszBuffer ) , "h%s" "e" );
iPos += formatexszBufferiPos ] , charsmaxszBuffer ) - iPos "l%s" "l" );
iPos += formatexszBufferiPos ] , charsmaxszBuffer ) - iPos "o %s" "bud" ); 
Output: hello bud
__________________

Last edited by Bugsy; 04-17-2020 at 08:03.
Bugsy is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 04-17-2020 , 00:25   Re: [Question] String concatenation limit
Reply With Quote #4

The length passed to formatex() should be "charsmax(szBuffer) - iPos" otherwise you'll have the potential to cause formatex() to try to write past the end of the string.
__________________
fysiks is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-17-2020 , 08:03   Re: [Question] String concatenation limit
Reply With Quote #5

Correct
__________________
Bugsy 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 23:04.


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