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

Is this a StrCat bug? (solved)


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
step
Senior Member
Join Date: May 2010
Old 07-16-2010 , 19:59   Is this a StrCat bug? (solved)
Reply With Quote #1

Hi, need a little help to understand a problem I'm having with this code:

PHP Code:
public Action:Command_Test(clientargs)
{
    
decl String:name[128];
    
decl String:temp[768];
    
    
name "initialized";
    
StrCat(tempsizeof(temp), name);
    
    
ChangeString(tempsizeof(temp));
    
PrintToConsole(client"%s"temp);
    
    return 
Plugin_Handled;
}

ChangeString(String:buffer[], maxlength)
{
    
decl String:str[32];
    
decl String:temp[128];
    
    if (
strlen(str) == 0)
    {
        
GetGameFolderName(strsizeof(str));
    }
    
    
BuildPath(Path_SMtempsizeof(temp), "logs\\%s.txt"str);
    
    
strcopy(buffermaxlengthstr);

The first response to the command will be:
Quote:
left4dead2
But the second one will be:
Quote:
left<04>
Why does this happen?


Another funny thing is that if I add
PHP Code:
decl String:whatever[32]; 
before the 'str' declaration, it will work properly.

Thanks

Last edited by step; 07-17-2010 at 17:24.
step is offline
p3tsin
Senior Member
Join Date: Sep 2005
Location: Finland
Old 07-16-2010 , 20:20   Re: Is this a StrCat bug?
Reply With Quote #2

I would only expect that to produce garbage data. Why are you concatenating uninitialized strings in the first place, anyway? What are you trying to do?
__________________
plop
p3tsin is offline
step
Senior Member
Join Date: May 2010
Old 07-16-2010 , 20:31   Re: Is this a StrCat bug?
Reply With Quote #3

Quote:
Originally Posted by p3tsin View Post
I would only expect that to produce garbage data. Why are you concatenating uninitialized strings in the first place, anyway? What are you trying to do?
The only one that wasn't initialized was name, but even if it is, this will happen.
The actual code is bigger, but this replicates the problem. I don't want to repeat some of the instructions in ChangeString() if they are already found.

Last edited by step; 07-16-2010 at 20:36.
step is offline
Seta00
The Seta00 user has crashed.
Join Date: Jan 2010
Location: Berlin
Old 07-16-2010 , 20:57   Re: Is this a StrCat bug?
Reply With Quote #4

In first place, what are you trying to do?
Seta00 is offline
p3tsin
Senior Member
Join Date: Sep 2005
Location: Finland
Old 07-16-2010 , 21:11   Re: Is this a StrCat bug?
Reply With Quote #5

Quote:
Originally Posted by step View Post
The only one that wasn't initialized was name, but even if it is, this will happen.
Dude, none of them were initialized. Let me break this down for you:

PHP Code:
public Action:Command_Test(clientargs)
{
    
decl String:name[128];    //declare variable name, contents: garbage
    
decl String:temp[768];    //declare variable temp, contents: garbage
    
    
name "initialized";        //assign a value to name
    
StrCat(tempsizeof(temp), name);    //concatenate name to temp; temp's contents are now something like this: nvgj9782hoinitialized

    
ChangeString(tempsizeof(temp));    //pass the mess to the other function, which overwrites the previous contents
    //after the function call temp may now contain the gamedir, or it may contain just more garbage
    
PrintToConsole(client"%s"temp);
    
    return 
Plugin_Handled;
}

ChangeString(String:buffer[], maxlength)
{
    
decl String:str[32];        //declare variable str, contents: garbage
    
decl String:temp[128];    //declare variable temp, contents: garbage
    
    
if (strlen(str) == 0)        //is str empty? (random result depends on the garbage content)
    
{
        
GetGameFolderName(strsizeof(str));
    }
    
    
BuildPath(Path_SMtempsizeof(temp), "logs\\%s.txt"str);    //build path with a potentially random string as the filename
    
    
strcopy(buffermaxlengthstr);    //copy the potentially random string to buffer

Can you please just explain what you're trying to achieve here, because that code isn't making much sense.
__________________
plop
p3tsin is offline
step
Senior Member
Join Date: May 2010
Old 07-16-2010 , 21:51   Re: Is this a StrCat bug?
Reply With Quote #6

I just didn't want to initialize 'str', so I could know if the function ChangeString was being run by the first time.
Thanks for the help. I thought using 'decl' instead of 'new' was the best way, since strings are arrays. StrCat was the first function that didn't like it that way.

Anyway, it's fixed now. The real function is:

PHP Code:
CreateFilePath(String:buffer[], maxlength)
{
    
decl String:game[32] = "";
    
decl String:version[32] = "";
    
decl String:date[32];
    
decl String:temp[128];
    new 
ranbefore;
    
    if (
ranbefore == 0)
    {
        
// game name
        
GetGameFolderName(gamesizeof(game));
        
        
// game version
        
new Handle:file OpenFile("steam.inf""r");
        
ReadFileLine(filetempsizeof(temp));
        
CloseHandle(file);
        
TrimString(temp);
        new 
int;
        
int FindCharInString(temp'='false);
        
StrCat(versionsizeof(version), temp[int]);
        
ReplaceString(versionsizeof(version), "=""v");
        
ReplaceString(versionsizeof(version), ".""");
        
        
ranbefore 1;
    }
    
    
// date & time
    
FormatTime(datesizeof(date), NULL_STRING);
    
ReplaceString(datesizeof(date), "/""_");
    
ReplaceString(datesizeof(date), " - ""_");
    
ReplaceString(datesizeof(date), ":""_");
    
ReplaceString(datesizeof(date), " ""_");
    
    
// path
    
BuildPath(Path_SMtempsizeof(temp), "logs\\%s_%s_%s.txt"gameversiondate);
    
    
// return
    
strcopy(buffermaxlengthtemp);

step is offline
Greyscale
SourceMod Plugin Approver
Join Date: Dec 2007
Location: strYoMommasHouse[you];
Old 07-16-2010 , 21:52   Re: Is this a StrCat bug?
Reply With Quote #7

Change your 'decl' to 'new' in your real source and see if that fixes it.

decl is allocating memory without clearing the contents of it. So random crap could be left in that memory, or it could be untouched memory (empty)

new allocates the memory and then empties its contents. 'decl' is just a more "lean" version of 'new' because it doesn't do any work clearing out the contents.

Moral of the story, only use 'decl' if you are going to copy your own known values to it.

EDIT:

You beat me.
But I see you have decl String:game[32] = "";
That is equivalent to using 'new'
__________________
Greyscale is offline
step
Senior Member
Join Date: May 2010
Old 07-16-2010 , 22:16   Re: Is this a StrCat bug?
Reply With Quote #8

Quote:
Originally Posted by Greyscale View Post
You beat me.
But I see you have decl String:game[32] = "";
That is equivalent to using 'new'
You're right. And ranbefore won't work either since it's being set to 0 in every run. Any suggestions?
step is offline
Greyscale
SourceMod Plugin Approver
Join Date: Dec 2007
Location: strYoMommasHouse[you];
Old 07-16-2010 , 22:24   Re: Is this a StrCat bug?
Reply With Quote #9

Use static instead of new.

If you declare a variable in a local function with the 'static' initializer then the value will be remembered. The only change you need to make is changing new to static.
__________________
Greyscale is offline
Monkeys
Veteran Member
Join Date: Jan 2010
Old 07-16-2010 , 22:27   Re: Is this a StrCat bug?
Reply With Quote #10

Quote:
Originally Posted by Greyscale View Post
But I see you have decl String:game[32] = "";
That is equivalent to using 'new'
Not that it matters in any way, but that's not entirely true ;p

decl String:game[32] = ""; sets game[0] to '\0' and the other chars remain garbage (although they'll never get read)
and new String:game[32]; sets everything to '\0'

EDIT: I'm saying this because you're better off using decl String:string[value] = ""; rather than new when value is rather high.
__________________
Get a lid on that zombie,
he's never gonna be alri-i-ight.
Oooh get a lid on that zombie,
or he's gonna feed all night.
Monkeys 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 00:11.


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