|
| 1 | +/** |
| 2 | + * @file reg_test_3371_prepared_statement_crash-t.cpp |
| 3 | + * @brief Tries to execute prepared statements with a not existing stmt_id. |
| 4 | + * This used to crash ProxySQL , so this tap test verifies that ProxySQL |
| 5 | + * doesn't crash |
| 6 | + */ |
| 7 | + |
| 8 | + |
| 9 | +#include <string> |
| 10 | +#include <stdio.h> |
| 11 | +#include <cstring> |
| 12 | +#include <unistd.h> |
| 13 | + |
| 14 | +#include "mysql.h" |
| 15 | + |
| 16 | +#include "tap.h" |
| 17 | +#include "command_line.h" |
| 18 | +#include "utils.h" |
| 19 | + |
| 20 | +using std::string; |
| 21 | + |
| 22 | +const int NUM_LOOPS = 100; ///< Number of loops for statement execution. |
| 23 | + |
| 24 | +int main(int argc, char** argv) { |
| 25 | + CommandLine cl; |
| 26 | + |
| 27 | + // Checking for required environmental variables |
| 28 | + if (cl.getEnv()) { |
| 29 | + diag("Failed to get the required environmental variables."); |
| 30 | + return -1; |
| 31 | + } |
| 32 | + |
| 33 | + plan(1+NUM_LOOPS*2); // Plan for testing purposes |
| 34 | + |
| 35 | + MYSQL* mysql = mysql_init(NULL); ///< MySQL connection object |
| 36 | + if (!mysql) { |
| 37 | + fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(mysql)); |
| 38 | + return exit_status(); |
| 39 | + } |
| 40 | + |
| 41 | + // Connecting to ProxySQL |
| 42 | + diag("Connecting to '%s@%s:%d'", cl.mysql_username, cl.mysql_host, cl.port); |
| 43 | + if (!mysql_real_connect(mysql, cl.mysql_host, cl.mysql_username, cl.mysql_password, NULL, cl.port, NULL, 0)) { |
| 44 | + fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(mysql)); |
| 45 | + return exit_status(); |
| 46 | + } |
| 47 | + |
| 48 | + // Initialize and prepare all the statements |
| 49 | + MYSQL_STMT* stmt = mysql_stmt_init(mysql); |
| 50 | + if (!stmt) { |
| 51 | + fprintf(stderr, "mysql_stmt_init(), out of memory\n"); |
| 52 | + return exit_status(); |
| 53 | + } |
| 54 | + |
| 55 | + std::string select_query = "SELECT 1"; |
| 56 | + diag("select_query: %s", select_query.c_str()); |
| 57 | + if (mysql_stmt_prepare(stmt, select_query.c_str(), strlen(select_query.c_str()))) { |
| 58 | + fprintf(stderr, "mysql_stmt_prepare at line %d failed: %s\n", __LINE__ , mysql_error(mysql)); |
| 59 | + mysql_close(mysql); |
| 60 | + mysql_library_end(); |
| 61 | + return exit_status(); |
| 62 | + } |
| 63 | + |
| 64 | + diag("Increasing stmt_id by 1, so that mysql_stmt_execute() must fail"); |
| 65 | + int rc = 0; |
| 66 | + for (int i = 0; i < NUM_LOOPS ; i++) { |
| 67 | + stmt->stmt_id += 1; |
| 68 | + rc = mysql_stmt_execute(stmt); |
| 69 | + ok (rc , "mysql_stmt_execute() must fail"); |
| 70 | + if (rc) { |
| 71 | + unsigned int psrc = mysql_stmt_errno(stmt); |
| 72 | + ok( psrc == 1243 , "mysql_stmt_execute at line %d failed: %d , %s", __LINE__ , psrc , mysql_stmt_error(stmt)); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + diag("Decreasing stmt_id by 1, so that mysql_stmt_execute() must succeed"); |
| 77 | + stmt->stmt_id -= NUM_LOOPS; |
| 78 | + rc = mysql_stmt_execute(stmt); |
| 79 | + ok (rc == 0 , "mysql_stmt_execute() succeeded"); |
| 80 | + if (rc) { |
| 81 | + fprintf(stderr, "mysql_stmt_execute at line %d failed: %d , %s\n", __LINE__ , rc , mysql_stmt_error(stmt)); |
| 82 | + mysql_close(mysql); |
| 83 | + mysql_library_end(); |
| 84 | + return exit_status(); |
| 85 | + } |
| 86 | + mysql_close(mysql); |
| 87 | + |
| 88 | + return exit_status(); |
| 89 | +} |
0 commit comments