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

Seriously dumb question related to strings...


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Durzel
Member
Join Date: Aug 2008
Old 01-04-2009 , 13:01   Seriously dumb question related to strings...
Reply With Quote #1

Is it safe to do something like this?

example:

decl String:buffer[100];
buffer = "This is a string";

I realise there are functions like Format, etc that take the sizeof() as a parameter, but when you don't need formatting options what are you supposed to use? sprintf/snprintf don't seem to be valid functions to use in SM?

I only ask because I've written a mod which does the above and whilst it appears to work I am getting a load of spurious packet output to the console that looks like the product of corruption somewhere, and it seems that it crashes the server after a round ends.
Durzel is offline
pRED*
Join Date: Dec 2006
Old 01-04-2009 , 15:34   Re: Seriously dumb question related to strings...
Reply With Quote #2

No.

Format *is* snprintf. Otherwise you can use strcpy.
pRED* is offline
BAILOPAN
Join Date: Jan 2004
Old 01-04-2009 , 20:06   Re: Seriously dumb question related to strings...
Reply With Quote #3

If you don't need formatting options you can do passthru, i.e. Format(x, sizeof(x), "%s", string) -- or strcpy as pRED said.
__________________
egg
BAILOPAN is offline
Kigen
BANNED
Join Date: Feb 2008
Old 01-05-2009 , 02:01   Re: Seriously dumb question related to strings...
Reply With Quote #4

new const String:blah[] = "Stuff in buffer.";

If you have to change whats in blah you should use strcpy or Format and also define a size. However, if blah is going to be static its best to use the above code.
Kigen is offline
SirLamer
Senior Member
Join Date: Oct 2008
Old 01-05-2009 , 18:28   Re: Seriously dumb question related to strings...
Reply With Quote #5

Consider your code here:

Code:
decl String:buffer[100];
buffer = "This is a string";
In SourcePawn, the second line changes the array pointer address to whatever "this is a string" ends up becoming, but does not actually change the string in memory. Basically, your array will point at a new, meaningless location in memory, which your script will have no problem reading from, and will be printed out as gibberish.

The following are acceptable:

Code:
new String:buffer[100] = "This is a string"; // Assignment like this during creation is acceptable

new String:buffer_2[] = "This is also a string"; // buffer_2's length will be auto-sized

decl String:buffer_3[100];
strcopy(buffer_3, "This is another string");
Similarly, you have to use the function "StrEqual(string_1, string_2)" to do logical comparisons of strings.

Languages that accept the straight-on assignment like you propose , such as PHP, are actually interpreting your intent in the compiler and sort of correct the logic to do what you would intuitively expect - assign the string into the array, and not change the memory address. SourcePawn doesn't do that for you.

The reason you get a LOT of gibberish output is because the script is actually reading from memory until it encounters the end-of-string character , '/0'. This character is automatically appended to all strings in memory, and this why a 2-character string like "at" actually takes 3 bytes. This system trusts that the provided memory address is valid, however, and it has no defense against random selection of a new memory address other than to keep reading until it happens across another end-of-string character from another string in memory.

Though I don't know the source code, I would expect strcopy(dest, source) to be faster than a pass-through with Format and is preferred unless your new string requires complex combination of several sources.

Last edited by SirLamer; 01-05-2009 at 18:34.
SirLamer is offline
Kigen
BANNED
Join Date: Feb 2008
Old 01-06-2009 , 01:07   Re: Seriously dumb question related to strings...
Reply With Quote #6

All SourcePawn functions relating to strings use buffer overflow protection (i.e., you have to specify a size of the dest buffer using sizeof() or a int).
Kigen is offline
BAILOPAN
Join Date: Jan 2004
Old 01-06-2009 , 03:19   Re: Seriously dumb question related to strings...
Reply With Quote #7

SirLamer: You're right that strcopy would be faster. But:

Quote:
Originally Posted by SirLamer
In SourcePawn, the second line changes the array pointer address to whatever "this is a string" ends up becoming, but does not actually change the string in memory. Basically, your array will point at a new, meaningless location in memory, which your script will have no problem reading from, and will be printed out as gibberish.
Pawn does not expose references - arrays are copied by-value, so string assignment there should be valid as long as the rvalue string can fit in the lvalue buffer.
__________________
egg
BAILOPAN is offline
Durzel
Member
Join Date: Aug 2008
Old 01-06-2009 , 06:27   Re: Seriously dumb question related to strings...
Reply With Quote #8

Ok thanks for all the help, I did think that it didn't seem unsafe but it's a bit odd writing in something that is "sortof C, but not quite". Or maybe my C is just a bit ropey...

Either way, changing the instances in my code where I was assigning a string to strcopy() didn't fix the problem I was having.

Basically while my plugin is running I am occasionally seeing this happen in the console on the client (not the server). Any ideas what could be causing this?

Last edited by Durzel; 01-06-2009 at 18:49.
Durzel is offline
SirLamer
Senior Member
Join Date: Oct 2008
Old 01-06-2009 , 11:08   Re: Seriously dumb question related to strings...
Reply With Quote #9

Quote:
Originally Posted by BAILOPAN View Post
Pawn does not expose references - arrays are copied by-value, so string assignment there should be valid as long as the rvalue string can fit in the lvalue buffer.
Oh excuse me, thanks for correcting me. I had similar weird issues when transferring my PHP experience to Pawn and thought that it was replacing the address pointer. I guess this means that one could implement their own string copy function that checks for the string length.

Quote:
Ok thanks for all the help, I did think that it didn't seem unsafe but it's a bit odd writing in something that is "sortof C, but not quite".
It's not C, it's Pawn. ;)
SirLamer is offline
Kigen
BANNED
Join Date: Feb 2008
Old 01-07-2009 , 08:45   Re: Seriously dumb question related to strings...
Reply With Quote #10

Looks like corruption.

It would be better if you posted all of your code as is so we may better understand what is all going on.

Explains a bit more on Strings in SourcePawn: http://wiki.alliedmods.net/Introduct...cePawn#Strings

Last edited by Kigen; 01-07-2009 at 09:13.
Kigen is offline
Reply



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 01:33.


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