[CONC-400] error with sizeof("commit") in function mysql_commit Created: 2019-03-27  Updated: 2020-02-02  Resolved: 2020-02-02

Status: Closed
Project: MariaDB Connector/C
Component/s: None
Affects Version/s: 2.3.7
Fix Version/s: 3.1.3

Type: Bug Priority: Minor
Reporter: anklean Assignee: Georg Richter
Resolution: Fixed Votes: 0
Labels: None
Environment:

linux ppc64/window10 x64



 Description   

Hi team,
I want to connect a drds database with library mariadb-connector-c-2.3.7-src. when i use mysql_commit function ,i got an error like this:

 mysql_error:You have an error in you SQL syntax:Error occurs around this fragment : {COMMIT 
 mysql_errno:1064

I found that the mysql_commit function uses the sizeof("commit") , the value is 7.

3363 my_bool STDCALL mysql_commit(MYSQL *mysql)
3364 {
3365   DBUG_ENTER("mysql_commit");
3366   DBUG_RETURN((my_bool)mysql_real_query(mysql, "COMMIT", sizeof("COMMIT")));
3367 }
3368 
3369 my_bool STDCALL mysql_rollback(MYSQL *mysql)
3370 {
3371   DBUG_ENTER("mysql_rollback");
3372   DBUG_RETURN((my_bool)mysql_real_query(mysql, "ROLLBACK", sizeof("ROLLBACK")));
3373 }

and the libmysql.c in library mysql-5.5.37/libmysql ,the implement is

 4745 my_bool STDCALL mysql_commit(MYSQL * mysql)
4746 {
4747   DBUG_ENTER("mysql_commit");
4748   DBUG_RETURN((my_bool) mysql_real_query(mysql, "commit", 6));
4749 }

when i use libmysql, the error is fixed.

the poco c++ library use length() function ,and the error is fixed too.

  sql = "commit";
    rc = mysql_real_query(hsession, sql.c_str(), static_cast<unsigned long>(sql.length()));
    if (rc != 0)
    {
        print_mysql_error();
    }
    assert(rc == 0);

I'm not sure if it's a bug, so I want to get an answer.



 Comments   
Comment by Pali [ 2019-06-18 ]

sizeof("COMMIT") is 7 and strlen("COMMIT") is 6 by definition of C language. 6 is length of string "COMMIT" and 7 is size of buffer needed for storing null-term string "COMMIT.

According to both MariaDB and MySQL documentation of mysql_real_query() function, caller should pass length of string in bytes and not size of needed for storing null-term string. See:

https://mariadb.com/kb/en/library/mysql_real_query/
https://dev.mysql.com/doc/en/mysql-real-query.html

So correct implementation should use either

sizeof("COMMIT")-1

or

strlen("COMMIT")

So, could you fix current implementation of mysql_commit and mysql_rollback?

Comment by anklean [ 2019-06-19 ]

ok,i'd like to fix it

Comment by Pali [ 2020-02-02 ]

anklean: Are you going to fix it? It should be really simple, just adds -1 after sizeof().

Comment by Georg Richter [ 2020-02-02 ]

This was already fixed:
commit 86829abc874f572f11bc2e568c714e5d3f7804bb
Merge: 7ba08bc 9cb1322
Author: Georg Richter <9EOR9@users.noreply.github.com>
Date: Fri Jul 5 08:24:21 2019 +0200

Merge pull request #115 from anklean/3.1

fix bug CONC-400 error with sizeof("commit") in function mysql_commit

Comment by Pali [ 2020-02-02 ]

Great! I did not know that this was already fixed as in this issue was still in open state and there was no update.

Generated at Thu Feb 08 03:05:03 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.