udan11/samp-plugin-sql

sql_escape_string Crash

semen7907 opened this issue · 3 comments

Plugin ver: mysql-v2.6-static-linux

[15:01:06] [debug] Server crashed while executing DM.amx
[15:01:06] [debug] AMX backtrace:
[15:01:06] [debug] #0 native sql_escape_string () [f7070f30] from sql.so
[15:01:06] [debug] #1 00135e7c in public OnDialogResponse (playerid=59, dialogid=126, response=1, listitem=-1, inputtext[]=@0x000ffe0c "\\\\\\") at C:\Users\semen\Desktop\SERVER\DM.pwn:2593
[15:01:06] [debug] Native backtrace:
[15:01:06] [debug] #0 f74cb09b in _ZN10StackTraceC1EPv () from plugins/crashdetect.so
[15:01:06] [debug] #1 f74c62d2 in _ZN11CrashDetect20PrintNativeBacktraceERSoPv () from plugins/crashdetect.so
[15:01:06] [debug] #2 f74c6ebc in _ZN11CrashDetect20PrintNativeBacktraceEPv () from plugins/crashdetect.so
[15:01:06] [debug] #3 f74c7366 in _ZN11CrashDetect11OnExceptionEPv () from plugins/crashdetect.so
[15:01:06] [debug] #4 f74cacec in ?? () from plugins/crashdetect.so
[15:01:06] [debug] #5 ffffe410 in __kernel_rt_sigreturn () from linux-gate.so.1
[15:01:06] [debug] #6 ffffe425 in __kernel_vsyscall () from linux-gate.so.1
[15:01:06] [debug] #7 f750b789 in gsignal () from /lib32/libc.so.6
[15:01:06] [debug] #8 f750d00b in abort () from /lib32/libc.so.6
[15:01:06] [debug] #9 f754aef5 in ?? () from /lib32/libc.so.6
[15:01:06] [debug] #10 f75511df in ?? () from /lib32/libc.so.6
[15:01:06] [debug] #11 f70710a7 in _ZN7Natives17sql_escape_stringEP6tagAMXPi () from plugins/sql.so
[15:01:06] [debug] #12 08094054 in ?? () from ./samp03svr
[15:01:06] [debug] #13 f74c3b9c in _ZN11CrashDetect13DoAmxCallbackEiPiS0_ () from plugins/crashdetect.so
[15:01:06] [debug] #14 f74c9d38 in ?? () from plugins/crashdetect.so
[15:01:06] [debug] #15 f74d01f8 in amx_Exec () from plugins/crashdetect.so
[15:01:06] [debug] #16 f74c6218 in _ZN11CrashDetect9DoAmxExecEPii () from plugins/crashdetect.so
[15:01:06] [debug] #17 f74c9aa9 in ?? () from plugins/crashdetect.so
[15:01:06] [debug] #18 080a524f in ?? () from ./samp03svr
[15:01:06] [debug] #19 080b11c4 in ?? () from ./samp03svr
[15:01:06] [debug] #20 08071b38 in ?? () from ./samp03svr
[15:01:06] [debug] #21 08071c32 in ?? () from ./samp03svr
[15:01:06] [debug] #22 0807bb80 in ?? () from ./samp03svr
[15:01:06] [debug] #23 080ad3bd in ?? () from ./samp03svr
[15:01:06] [debug] #24 080ad562 in ?? () from ./samp03svr
[15:01:06] [debug] #25 080a88ce in ?? () from ./samp03svr
[15:01:06] [debug] #26 f74f7646 in __libc_start_main () from /lib32/libc.so.6
[15:01:06] [debug] #27 0804b4d1 in ?? () from ./samp03svr



public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    switch(dialogid)
    {
    case DIALOG_REGISTER:
        {
            if ( !response )
            {
                Kick( playerid );
                return 0;
            }

            if(strlen(inputtext) < 5 || strlen(inputtext) > 16)
            {
                new dialogStr[500];
                format(dialogStr, sizeof(dialogStr),
                "Регистрация", playerName(playerid));
                ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, ""COL_WHITE"{FF3300}Регистрация [ERROR]",dialogStr,"Далее","Выход");
                return 1;
            }

            new
            escapedpw[80],
            Query[356];

            sql_escape_string(sqlconnection, inputtext, escapedpw);  // Строка 2593

            format(Query, sizeof(Query), "INSERT INTO `players` (`Name`, `Password`, `Kills`, `Deaths`, `Cash`, `Scr`, `Admin`, `Spawn`, `SpawnX`, `SpawnY`, `SpawnZ`) VALUES ('%s', MD5('%s'), 0, 0, 0, 0, 0, 0, 0, 0, 0)", playerName(playerid), escapedpw);
            sql_query(sqlconnection, Query, QUERY_THREADED);

            GivePlayerCash(playerid, 50000);

            PlayerData[playerid][pLogged] = true;

            TogglePlayerSpectating(playerid, false);

            PlayerPlaySound(playerid, 21002, 0.0, 0.0, 0.0);

            CountREGISTRATION++;
        }

I've been trying to reproduce this bug, but I failed. For me, it works fine.

I've used exactly the same plugin (version 2.6, MySQL only, statically linked) with the following Linux machine:

Linux vagrant-ubuntu-trusty-32 3.13.0-32-generic #57-Ubuntu SMP Tue Jul 15 03:51:12 UTC 2014 i686 i686 i686 GNU/Linux

This it the code, which is mostly yours.

#include <a_samp>
#include <sql>

#define COL_WHITE                       "{000000}"
#define DIALOG_REGISTER                 1
#define GivePlayerCash                  GivePlayerMoney

enum ePlayer {
    pLogged
}

new SQL:sqlconnection, CountREGISTRATION, PlayerData[MAX_PLAYERS][ePlayer];

stock playerName(playerid) 
{
    new ret[MAX_PLAYER_NAME];
    GetPlayerName(playerid, ret, sizeof(ret));
    return ret;
}

public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]) 
{
    printf("OnDialogResponse called");

    // SNIPPET FROM THE ORIGINAL ISSUE (https://github.com/udan11/samp-plugin-sql/issues/10)
    if(strlen(inputtext) < 5 || strlen(inputtext) > 16)
    {
        new dialogStr[500];
        format(dialogStr, sizeof(dialogStr),
        "Регистрация", playerName(playerid));
        ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, ""COL_WHITE"{FF3300}Регистрация [ERROR]",dialogStr,"Далее","Выход");
        return 1;
    }

    new
    escapedpw[80],
    Query[356];

    sql_escape_string(sqlconnection, inputtext, escapedpw);  // Строка 2593

    format(Query, sizeof(Query), "INSERT INTO `players` (`Name`, `Password`, `Kills`, `Deaths`, `Cash`, `Scr`, `Admin`, `Spawn`, `SpawnX`, `SpawnY`, `SpawnZ`) VALUES ('%s', MD5('%s'), 0, 0, 0, 0, 0, 0, 0, 0, 0)", playerName(playerid), escapedpw);
    sql_query(sqlconnection, Query, QUERY_THREADED);

    GivePlayerCash(playerid, 50000);

    PlayerData[playerid][pLogged] = true;

    TogglePlayerSpectating(playerid, false);

    PlayerPlaySound(playerid, 21002, 0.0, 0.0, 0.0);

    CountREGISTRATION++;
    // END OF SNIPPET

    printf("inputtext = %s", inputtext);
    printf("escapedpw = %s", escapedpw);
    printf("Query = %s", Query);
    return 1;
}



public OnGameModeInit()
{
    sqlconnection = mysql_connect("127.0.0.1", "root", "root", "samp-gm-v1");
    printf("sqlconnection = %d", _:sqlconnection);
    CallLocalFunction("OnDialogResponse", "iiiis", 59, 126, 1, -1, "\\\\\\\\\\\\");
    return 1;
}

main() {}

And my output is this (as expected):

[20:02:48]  Loading plugin: sql.so
[20:02:48]   >> SQL plugin v2.6 successfully loaded.
[20:02:48]       + MySQL support is enabled.
[20:02:48]   Loaded.
...
[20:02:48] sqlconnection = 1
[20:02:48] OnDialogResponse called
[20:02:48] inputtext = \\\\\\
[20:02:48] escapedpw = \\\\\\\\\\\\
[20:02:48] Query = INSERT INTO `players` (`Name`, `Password`, `Kills`, `Deaths`, `Cash`, `Scr`, `Admin`, `Spawn`, `SpawnX`, `SpawnY`, `SpawnZ`) VALUES ('', MD5('\\\\\\\\\\\\'), 0, 0, 0, 0, 0, 0, 0, 0, 0)

My sql_log.txt:

[20:02:48][info] Natives::sql_connect: Connecting to database (type = 1) root:***@127.0.0.1:0/samp-gm-v1...
[20:02:48][info] Natives::sql_connect: Connection (conn->id = 1) was succesful!
[20:02:48][debug] Natives::sql_escape_string: Escaping (conn->id = 1) string '\\\\\\'...
[20:02:48][debug] Natives::sql_query: Scheduling statement (stmt->id = 1, stmt->query = INSERT INTO `players` (`Name`, `Password`, `Kills`, `Deaths`, `Cash`, `Scr`, `Admin`, `Spawn`, `SpawnX`, `SpawnY`, `SpawnZ`) VALUES ('', MD5('\\\\\\\\\\\\'), 0, 0, 0, 0, 0, 0, 0, 0, 0), stmt->callback = ) for execution...
[20:02:48][debug] SQL_Worker[1]: Executing query (stmt->id = 1, stmt->query = INSERT INTO `players` (`Name`, `Password`, `Kills`, `Deaths`, `Cash`, `Scr`, `Admin`, `Spawn`, `SpawnX`, `SpawnY`, `SpawnZ`) VALUES ('', MD5('\\\\\\\\\\\\'), 0, 0, 0, 0, 0, 0, 0, 0, 0))...
[20:02:48][debug] ProccessTick: Executing query callback (stmt->id = 1, stmt->error = 1146, stmt->callback = )...
[20:02:48][debug] ProccessTick: Erasing query (stmt->id = 1)...

Can you post the relevant parts of your sql_log.txt file, please?

I reinstalled the plugin and include and it seems that errors are no more, may have been damaged plugin...

If you crash again, I will give log

sorry for the English :d

No problem! I'm glad to hear that you've fixed your issues.