Bug #101614 mysql_stmt_prepare() does not return on query with "order by ?"
Submitted: 15 Nov 2020 12:32 Modified: 16 Nov 2020 6:40
Reporter: Masahiro Tomita Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: C API (client library) Severity:S3 (Non-critical)
Version:8.0.22 OS:Any
Assigned to: CPU Architecture:Any
Tags: regression

[15 Nov 2020 12:32] Masahiro Tomita
Description:
On MySQL 8.0.22, mysql_stmt_prepare() does not return on query with "order by ?".

How to repeat:
mysql> create database test;
mysql> use test;
mysql> create table t (id int auto_increment primary key, n int);
mysql> insert into t values (1,5),(2,4),(3,3),(4,2),(5,1);

--- test.c ---
#include <stdio.h>
#include <string.h>
#include <mysql/mysql.h>

int main(int argc, char *argv[])
{
  MYSQL my;
  MYSQL_STMT *st;
  MYSQL_BIND bind[1], bind2[2];
  memset(bind, 0, sizeof(bind));
  memset(bind2, 0, sizeof(bind2));

  mysql_init(&my);
  if (mysql_real_connect(&my, "127.0.0.1", "root", "", "test", 0, NULL, 0) == NULL)
    goto error;
  if ((st = mysql_stmt_init(&my)) == NULL)
    goto error;

  char *q = "select id,n from t order by ?";
  mysql_stmt_prepare(st, q, strlen(q));

  long n = 2;
  bind[0].buffer_type = MYSQL_TYPE_LONG;
  bind[0].buffer = &n;
  bind[0].length = NULL;
  bind[0].buffer_length = sizeof(n);
  bind[0].is_null = 0;

  if (mysql_stmt_bind_param(st, bind) != 0)
    goto stmt_error;
  if (mysql_stmt_execute(st) != 0)
    goto stmt_error;

  long id = 0;
  bind2[0].buffer_type = MYSQL_TYPE_LONG;
  bind2[0].buffer = &id;
  bind2[0].buffer_length = sizeof(id);
  bind2[0].length = NULL;
  bind2[1].buffer_type = MYSQL_TYPE_LONG;
  bind2[1].buffer = &n;
  bind2[1].buffer_length = sizeof(n);
  bind2[1].length = NULL;

  if (mysql_stmt_bind_result(st, bind2) != 0)
    goto stmt_error;
  if (mysql_stmt_store_result(st) != 0)
    goto stmt_error;

  while (mysql_stmt_fetch(st) == 0) {
    printf("%ld %ld\n", id, n);
  }
  exit(0);

error:
  puts(mysql_error(&my));
  exit(1);

stmt_error:
  puts(mysql_stmt_error(st));
  exit(1);
}
---

% gcc test.c -lmysqlclient
% ./a.out

On 8.0.21:
5 1
4 2
3 3
2 4
1 5

On 8.0.22:
(no reply)
[16 Nov 2020 6:40] MySQL Verification Team
Hello Masahiro Tomita,

Thank you for the report and test case.

regards,
Umesh