Skip to content
This repository has been archived by the owner on Dec 18, 2021. It is now read-only.

Data is truncated with MariaDB Connector/C 3.0.1+ #39

Closed
WopsS opened this issue Aug 8, 2018 · 11 comments
Closed

Data is truncated with MariaDB Connector/C 3.0.1+ #39

WopsS opened this issue Aug 8, 2018 · 11 comments

Comments

@WopsS
Copy link
Contributor

WopsS commented Aug 8, 2018

When using MariaDB Connector/C 3.0.1 or newer the data is truncated because of this line

meta_data.bound_text_buffer.resize(8);

in bind_result_t::_bind_text_result, which will get just 8 characters from database and later will fail at

mysql_stmt_fetch_column(_handle->mysql_stmt, _handle->result_params.data() + r.index, r.index, 0);

in bind_result_t::next_impl() with error

MySQL: Fetch column after reallocate failed: error-code: 1, stmt-error: , stmt-errno: 2051

A possible fix would be to resize it to the cloumn's length

auto result_meta_data = mysql_stmt_result_metadata(_handle->mysql_stmt);
if (result_meta_data)
{
    auto fields = mysql_fetch_fields(result_meta_data);
    if (fields)
    {
        auto charset = mariadb_get_charset_by_nr(fields[index].charsetnr);
        meta_data.bound_text_buffer.resize(fields[index].length / charset->char_maxlen);
    }

    mysql_free_result(result_meta_data);
}

OS: Windows
Arch: x64
Compiler: MSVC++ 2017 (latest)
Standard: C++17
MariaDB version: 10.3.8 (Dockerized container, image mariadb:latest)
MariaDB Connector/C: 3.0.6

@rbock
Copy link
Owner

rbock commented Aug 9, 2018

That is a surprise! I will need a few days to look into that.

It would be great if you could provide a pull request with a test that demonstrates the current failure.

@WopsS
Copy link
Contributor Author

WopsS commented Aug 9, 2018

I forgot to add the response, I added 1 test with 3 cases (see #40), first 2 should pass and the last one should fail.

@rbock
Copy link
Owner

rbock commented Aug 9, 2018

Thanks a lot!

@WopsS
Copy link
Contributor Author

WopsS commented Aug 9, 2018

No problem :)

@rbock
Copy link
Owner

rbock commented Aug 15, 2018

Good news: I can reproduce the effect on my local machine.

@WopsS
Copy link
Contributor Author

WopsS commented Aug 15, 2018

Nice! <3

@rbock
Copy link
Owner

rbock commented Aug 15, 2018

After playing around with this a bit, I tend to consider this a bug in the client library.

Using mysql, after calling mysql_stmt_fetch, if there is data, then stmt->state == MYSQL_STMT_FETCH_DONE regardless of whether data is truncated or not.

Using the mariadb connector, if there is

  • non-truncated data, then stmt->state == MYSQL_STMT_USER_FETCHING
  • truncated data then
    • if there was non-truncated data in the call before, then stmt->state == MYSQL_STMT_USER_FETCHING
    • if it is the first row, then stmt->state == MYSQL_STMT_USE_OR_STORE_CALLED

The latter is an invalid state for calling mysql_stmt_fetch_column (looking at the source code).

Maybe you can double check and file an issue for the connector library, in case you come to the same conclusion?

@WopsS
Copy link
Contributor Author

WopsS commented Aug 15, 2018

Sure, I will do that and let you know. I will need few days to do it, I am a little bit busy this week.

@WopsS
Copy link
Contributor Author

WopsS commented Aug 21, 2018

I played around with it too and it is as you said.

When I use MySQL _handle->mysql_stmt->state == MYSQL_STMT_FETCH_DONE even if data is truncated or not.

When using MariaDB

  • if data is not truncated _handle->mysql_stmt->state == MYSQL_STMT_USER_FETCHING, also when there was non-truncated data before.
  • if first row has truncated data _handle->mysql_stmt->state == MYSQL_STMT_USE_OR_STORE_CALLED.

I created a bug report on MariaDB Connector/C bug tracker (https://jira.mariadb.org/browse/CONC-354).

@rbock
Copy link
Owner

rbock commented Aug 21, 2018

Thanks for testing and the bug report!

@WopsS
Copy link
Contributor Author

WopsS commented Aug 21, 2018

No problem :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants