SA-MP Forums Archive
how to fixed this nativechecker - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: how to fixed this nativechecker (/showthread.php?tid=515875)



how to fixed this nativechecker - kurniarocki - 28.05.2014

how to fix this error



Re: how to fixed this nativechecker - Misiur - 28.05.2014

Quote:

Invalid file handles used without checking may produce run-time errors or crashes.

You didn't check if your fopen returns a valid handle.


Re: how to fixed this nativechecker - kurniarocki - 28.05.2014

could explain why? more detailed


Re: how to fixed this nativechecker - Konstantinos - 28.05.2014

https://sampwiki.blast.hk/wiki/Fwrite

See the example and read the comments of it so you can understand where and how it is checked if the file handle is valid.


Re: how to fixed this nativechecker - kurniarocki - 28.05.2014

this code anyone wrong?
pawn Код:
public AddReportToken(playerid) {
    new
        sz_FileStr[32 + MAX_PLAYER_NAME],
        sz_playerName[MAX_PLAYER_NAME],
        i_timestamp[3],
        File: fPointer;

    GetPlayerName(playerid, sz_playerName, MAX_PLAYER_NAME);
    getdate(i_timestamp[0], i_timestamp[1], i_timestamp[2]);

    format(sz_FileStr, sizeof(sz_FileStr), "admins/rtokens/%s[%d'%d'%d]", sz_playerName, i_timestamp[1], i_timestamp[2], i_timestamp[0]);
    if(fexist(sz_FileStr))
    {
        fPointer = fopen(sz_FileStr, io_read);
        fread(fPointer, sz_playerName), fclose(fPointer);

        new
            i_tokenVal = strval(sz_playerName);

        format(sz_playerName, sizeof(sz_playerName), "%i", i_tokenVal + 1);
        fPointer = fopen(sz_FileStr, io_write);
        fwrite(fPointer, sz_playerName);
        fclose(fPointer);
    }
    else {
        fPointer = fopen(sz_FileStr, io_write);
        fwrite(fPointer, "1");
    }
    return fclose(fPointer);
}



Re: how to fixed this nativechecker - Konstantinos - 29.05.2014

Assuming it's not called by a timer or Call Local/Remote Function, you don't need to use forward/public for it.
pawn Код:
if (!AddReportToken(playerid))
{
    // the file couldn't be opened..
}
else
{
    // everything is fine
}
pawn Код:
AddReportToken(playerid)
{
    new
        sz_FileStr[32 + MAX_PLAYER_NAME],
        sz_playerName[MAX_PLAYER_NAME],
        i_timestamp[3],
        File: fPointer;
   
    GetPlayerName(playerid, sz_playerName, MAX_PLAYER_NAME);
    getdate(i_timestamp[0], i_timestamp[1], i_timestamp[2]);
   
    format(sz_FileStr, sizeof(sz_FileStr), "admins/rtokens/%s[%d'%d'%d]", sz_playerName, i_timestamp[1], i_timestamp[2], i_timestamp[0]);
    if(fexist(sz_FileStr)) // checking if exists to open the file with io_read, else it will crash the server
    {
        fPointer = fopen(sz_FileStr, io_read);
        if (!fPointer) return 0; // return 0 and don't allow fPointer to be passed as argument and cause the crash
        fread(fPointer, sz_playerName);
        fclose(fPointer);
       
        new
            i_tokenVal = strval(sz_playerName);
       
        format(sz_playerName, sizeof(sz_playerName), "%i", i_tokenVal + 1);
        fPointer = fopen(sz_FileStr, io_write);
        if (!fPointer) return 0; // return 0 and don't allow fPointer to be passed as argument and cause the crash
        fwrite(fPointer, sz_playerName);
        fclose(fPointer);
    }
    else
    {
        fPointer = fopen(sz_FileStr, io_write);
        if (!fPointer) return 0; // return 0 and don't allow fPointer to be passed as argument and cause the crash
        fwrite(fPointer, "1");
        fclose(fPointer);
    }
    return 1;
}