diff --git a/libmariadb/my_stmt_codec.c b/libmariadb/my_stmt_codec.c index 0261195c..34c4c0ff 100644 --- a/libmariadb/my_stmt_codec.c +++ b/libmariadb/my_stmt_codec.c @@ -813,9 +813,19 @@ void ps_fetch_bin(MYSQL_BIND *r_param, const MYSQL_FIELD *field, ulong field_length= net_field_length(row); size_t copylen; + /* Bug conc-155: For text columns we need to store terminating zero character */ + if (!(field->flags & BINARY_FLAG) && r_param->buffer_type == MYSQL_TYPE_STRING) + field_length++; + copylen= MIN(field_length, r_param->buffer_length); memcpy(r_param->buffer, *row, copylen); *r_param->error= copylen < field_length; + + /* don't count trailing zero if we fetch into string */ + if (r_param->buffer_type == MYSQL_TYPE_STRING && + !*r_param->error) + field_length--; + *r_param->length= field_length; (*row) += field_length; diff --git a/unittest/libmariadb/ps_bugs.c b/unittest/libmariadb/ps_bugs.c index 94b946ea..d5fdded3 100644 --- a/unittest/libmariadb/ps_bugs.c +++ b/unittest/libmariadb/ps_bugs.c @@ -4012,7 +4012,52 @@ static int test_conc154(MYSQL *mysql) return OK; } +static int test_conc155(MYSQL *mysql) +{ + MYSQL_STMT *stmt; + MYSQL_BIND bind; + char buffer[50]; + int rc; + + rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1"); + check_mysql_rc(rc, mysql); + rc= mysql_query(mysql, "CREATE TABLE t1 (a TEXT)"); + check_mysql_rc(rc, mysql); + rc= mysql_query(mysql, "INSERT INTO t1 VALUES ('zero terminated string')"); + check_mysql_rc(rc, mysql); + + stmt= mysql_stmt_init(mysql); + rc= mysql_stmt_prepare(stmt, "SELECT a FROM t1", strlen("SELECT a FROM t1")); + check_stmt_rc(rc, stmt); + + rc= mysql_stmt_execute(stmt); + check_stmt_rc(rc, stmt); + + memset(buffer, 'X', 50); + memset(&bind, 0, sizeof(MYSQL_BIND)); + + bind.buffer= buffer; + bind.buffer_length= 50; + bind.buffer_type= MYSQL_TYPE_STRING; + + rc= mysql_stmt_bind_result(stmt, &bind); + check_stmt_rc(rc, stmt); + + rc= mysql_stmt_fetch(stmt); + check_stmt_rc(rc, stmt); + + if (strlen(buffer) != strlen("zero terminated string")) + { + diag("Wrong buffer length"); + return FAIL; + } + + mysql_stmt_close(stmt); + return OK; +} + struct my_tests_st my_tests[] = { + {"test_conc155", test_conc155, TEST_CONNECTION_DEFAULT, 0, NULL, NULL}, {"test_conc154", test_conc154, TEST_CONNECTION_DEFAULT, 0, NULL , NULL}, {"test_conc141", test_conc141, TEST_CONNECTION_NEW, 0, NULL , NULL}, {"test_conc67", test_conc67, TEST_CONNECTION_DEFAULT, 0, NULL , NULL}, @@ -4083,3 +4128,4 @@ int main(int argc, char **argv) return(exit_status()); } +