mysql C API - mysql_stmt_fetch throw exception: ac

2019-09-17 21:32发布

问题:

I'm getting "Unhandled exception at 0x0369AF03 (libmysql.dll) in node.exe: 0xC0000005: Access violation writing location 0x00000008." error message while using mysql C API in a loop.

Environment: Windows 7 32bit, Mysql 5.6

The error only occurs in Debug mode, in release mode, it passed and fetched result, this is very weird.

I googled, and set the STMT_ATTR_CURSOR_TYPE, but it's the same

Please advise, thanks!

code:

prepare statement

  static const char *sql_odb_read =
    "SELECT `type`, `size`, UNCOMPRESS(`data`) FROM GIT_ODB WHERE `repo` = ? AND `oid` = ?;";
  mysql->odb_read = mysql_stmt_init(mysql->db);
  if (mysql->odb_read == NULL)
    return GIT_ERROR;

  if (mysql_stmt_prepare(mysql->odb_read, sql_odb_read, strlen(sql_odb_read)) != 0)
    return GIT_ERROR;

  if (mysql_stmt_attr_set(mysql->odb_read, STMT_ATTR_UPDATE_MAX_LENGTH, &truth) != 0)
    return GIT_ERROR;

  if (mysql_stmt_attr_set(mysql->odb_read, STMT_ATTR_CURSOR_TYPE, (const void *)&type) != 0)
      return GIT_ERROR;

execute statement

 // bind the repo passed to the statement
  bind_buffers[0].buffer = &(backend->mysql->repo);
  bind_buffers[0].buffer_length = sizeof(backend->mysql->repo);
  bind_buffers[0].length = &bind_buffers[0].buffer_length;
  bind_buffers[0].buffer_type = MYSQL_TYPE_LONGLONG;

  // bind the oid passed to the statement
  bind_buffers[1].buffer = (void*)oid->id;
  bind_buffers[1].buffer_length = 20;
  bind_buffers[1].length = &bind_buffers[1].buffer_length;
  bind_buffers[1].buffer_type = MYSQL_TYPE_BLOB;

if (mysql_stmt_bind_param(backend->mysql->odb_read, bind_buffers) != 0)
      return GIT_ERROR;

  // execute the statement
  if (mysql_stmt_execute(backend->mysql->odb_read) != 0)
      return GIT_ERROR;

  if (mysql_stmt_num_rows(backend->mysql->odb_read) == 1) {
    result_buffers[0].buffer_type = MYSQL_TYPE_TINY;
    result_buffers[0].buffer = type_p;
    result_buffers[0].buffer_length = sizeof(type_p); //sizeof(signed char);
    result_buffers[0].is_null = 0;
    result_buffers[0].length = &type_len;
    memset(type_p, 0, sizeof(type_p));

    result_buffers[1].buffer_type = MYSQL_TYPE_LONGLONG;
    result_buffers[1].buffer = len_p;
    result_buffers[1].buffer_length = sizeof(len_p);
    result_buffers[1].is_null = 0;
    result_buffers[1].length = &len_len;
    memset(len_p, 0, sizeof(len_p));

    result_buffers[2].buffer_type = MYSQL_TYPE_LONG_BLOB;
    result_buffers[2].buffer = 0;
    result_buffers[2].is_null = 0;
    result_buffers[2].buffer_length = 0;
    result_buffers[2].length = &data_len;

    if (mysql_stmt_bind_result(backend->mysql->odb_read, result_buffers) != 0)
      return GIT_ERROR;

error = mysql_stmt_fetch(backend->mysql->odb_read);

回答1:

i downloaded libmysql symbol and source file and debugged into it after mysql_stmt_execute, result buffer is changed, i don't know why, it's libmysql bug? so i moved mem_set right before i use result buffer, then it's all working fine