• Create Account

Banner advertising on our site currently available from just \$5!

#ActualPromit

Posted 29 July 2013 - 08:04 AM

Using strlen() on a character buffer that you haven't explicitly initialized is a bad idea.

Bad idea? BAD IDEA?! That doesn't really capture the situation here. The code makes no sense as written. It's not so much a bad idea as it is badly broken.

First, you can't set an array's size like that. You have to do a dynamic allocation. Second, you don't need to call strlen at all, since you know exactly what size you started with. Third, you're asking if a file exists after you've already attempted to open it, which is nonsense. If you're opening it, just check if you got a file back. Fourth, you've misunderstood the parameters to PHYSFS_read.

So you actually need something like:

PHYSFS_File* pyfile = PHYSFS_openRead(pyname);
if(pyfile != NULL)
{
PHYSFS_sint64 fileSize = PHYSFS_fileLength(pyfile);
char* scriptbuffer = new char[fileSize];
PHYSFS_close(pyfile);

PyRun_SimpleString( scriptbuffer );
delete[] scriptbuffer;
return true;
}
return false;


#4Promit

Posted 26 July 2013 - 04:14 PM

Using strlen() on a character buffer that you haven't explicitly initialized is a bad idea.

Bad idea? BAD IDEA?! That doesn't really capture the situation here. The code makes no sense as written. It's not so much a bad idea as it is badly broken.

First, you can't set an array's size like that. You have to do a dynamic allocation. Second, you don't need to call strlen at all, since you know exactly what size you started with. Third, you're asking if a file exists after you've already attempted to open it, which is nonsense. If you're opening it, just check if you got a file back. Fourth, you've misunderstood the parameters to PHYSFS_read.

So you actually need something like:

PHYSFS_File* pyfile = PHYSFS_openRead(pyname);
if(pyfile != NULL)
{
PHYSFS_sint64 fileSize = PHYSFS_fileLength(pyfile);
char* scriptbuffer = new char[fileSize];
PHYSFS_close(pyfile);

PyRun_SimpleString( scriptbuffer );
delete[] scriptbuffer;
return true;
}
return false;


#3Promit

Posted 26 July 2013 - 04:12 PM

Using strlen() on a character buffer that you haven't explicitly initialized is a bad idea.

Bad idea? BAD IDEA?! That doesn't really capture the situation here. The code makes no sense as written. It's not so much a bad idea as it is badly broken.

First, you can't set an array's size like that. You have to do a dynamic allocation. Second, you don't need to call strlen at all, since you know exactly what size you started with. Third, you're asking if a file exists after you've already attempted to open it, which is nonsense. If you're opening it, just check if you got a file back. Fourth, you've misunderstood the parameters to PHYSFS_read.

So you actually need something like:

PHYSFS_File* pyfile = PHYSFS_openRead(pyname);
if(pyfile != NULL)
{
PHYSFS_sint64 fileSize = PHYSFS_fileLength(pyfile);
char* scriptbuffer = new char[fileSize];
PyRun_SimpleString( scriptbuffer );
delete[] scriptbuffer;
return true;
}
return false;


#2Promit

Posted 26 July 2013 - 04:12 PM

Using strlen() on a character buffer that you haven't explicitly initialized is a bad idea.

Bad idea? BAD IDEA?! That doesn't really capture the situation here. The code makes no sense as written. It's not so much a bad idea as it is badly broken.

First, you can't set an array's size like that. You have to do a dynamic allocation. Second, you don't need to call strlen at all, since you know exactly what size you started with. Third, you're asking if a file exists after you've already attempted to open it, which is nonsense. If you're opening it, just check if you got a file back. Fourth, you've misunderstood the parameters to PHYSFS_read.

So you actually need something like:

PHYSFS_File* pyfile = PHYSFS_openRead(pyname);
if(pyfile != NULL)
{
PHYSFS_sint64 fileSize = PHYSFS_fileLength(pyfile);
char* scriptbuffer = new char[fileSize];
PyRun_SimpleString( scriptbuffer );
delete[] scriptbuffer;
return true;
}
return false;


#1Promit

Posted 26 July 2013 - 04:11 PM

Using strlen() on a character buffer that you haven't explicitly initialized is a bad idea.
Bad idea? BAD IDEA?! That doesn't really capture the situation here; the code makes no sense as written.

First, you can't set an array's size like that. You have to do a dynamic allocation. Second, you don't need to call strlen at all, since you know exactly what size you started with. Third, you're asking if a file exists after you've already attempted to open it, which is nonsense. If you're opening it, just check if you got a file back. Fourth, you've misunderstood the parameters to PHYSFS_read.

So you actually need something like:

PHYSFS_File* pyfile = PHYSFS_openRead(pyname);
if(pyfile != NULL)
{
PHYSFS_sint64 fileSize = PHYSFS_fileLength(pyfile);
char* scriptbuffer = new char[fileSize];