1
0
mirror of https://github.com/mariadb-corporation/mariadb-connector-c.git synced 2025-08-08 14:02:17 +03:00

Fixed conversion from zerofill integer to MYSQL_TYPE_STRING:

if the length of the provided result buffer is equal or smaller than the converted number and
ZEROFILL FLAG is set, a truncation error will be reported.
This commit is contained in:
Georg Richter
2019-02-06 11:00:42 +01:00
parent 3beab84b83
commit 0ca2f2cb38
2 changed files with 52 additions and 6 deletions

View File

@@ -644,21 +644,27 @@ static void convert_from_long(MYSQL_BIND *r_param, const MYSQL_FIELD *field, lon
char *buffer;
char *endptr;
uint len;
my_bool zf_truncated= 0;
buffer= alloca(MAX(field->length, 22));
endptr= ma_ll2str(val, buffer, is_unsigned ? 10 : -10);
len= (uint)(endptr - buffer);
/* check if field flag is zerofill */
if (field->flags & ZEROFILL_FLAG &&
len < field->length && len < r_param->buffer_length)
if (field->flags & ZEROFILL_FLAG)
{
if (len < field->length && len < r_param->buffer_length)
{
ma_bmove_upp(buffer + field->length, buffer + len, len);
/* coverity [bad_memset] */
memset((void*) buffer, (int) '0', field->length - len);
len= field->length;
}
else
zf_truncated= 1;
}
convert_froma_string(r_param, buffer, len);
*r_param->error+= zf_truncated;
}
break;
}

View File

@@ -5009,11 +5009,51 @@ static int test_conc_fraction(MYSQL *mysql)
return OK;
}
static int test_zerofill_1byte(MYSQL *mysql)
{
MYSQL_STMT *stmt= mysql_stmt_init(mysql);
int rc;
MYSQL_BIND bind;
char buffer[3];
rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
check_mysql_rc(rc, mysql);
rc= mysql_query(mysql, "CREATE TABLE t1 (a int zerofill)");
check_mysql_rc(rc, mysql);
rc= mysql_query(mysql, "INSERT INTO t1 VALUES(1)");
check_mysql_rc(rc, mysql);
rc= mysql_stmt_prepare(stmt, SL("SELECT a FROM t1"));
check_stmt_rc(rc, stmt);
rc= mysql_stmt_execute(stmt);
check_stmt_rc(rc, stmt);
memset(&bind, 0, sizeof(MYSQL_BIND));
bind.buffer_type= MYSQL_TYPE_STRING;
bind.buffer= buffer;
bind.buffer_length= 1;
rc= mysql_stmt_bind_result(stmt, &bind);
rc= mysql_stmt_fetch(stmt);
FAIL_IF(rc != 101, "expected truncation warning");
mysql_stmt_close(stmt);
rc= mysql_query(mysql, "DROP TABLE t1");
check_mysql_rc(rc, mysql);
return OK;
}
struct my_tests_st my_tests[] = {
{"test_conc344", test_conc344, TEST_CONNECTION_NEW, 0, NULL, NULL},
{"test_conc334", test_conc334, TEST_CONNECTION_NEW, 0, NULL, NULL},
{"test_compress", test_compress, TEST_CONNECTION_NEW, CLIENT_COMPRESS, NULL, NULL},
{"test_zerofill_1byte", test_zerofill_1byte, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
{"test_codbc138", test_codbc138, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
{"test_conc208", test_conc208, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
{"test_mdev14165", test_mdev14165, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},