diff --git a/plugins/pvio/pvio_socket.c b/plugins/pvio/pvio_socket.c index 74e4a323..2599c0ee 100644 --- a/plugins/pvio/pvio_socket.c +++ b/plugins/pvio/pvio_socket.c @@ -750,8 +750,8 @@ my_bool pvio_socket_connect(MARIADB_PVIO *pvio, MA_PVIO_CINFO *cinfo) { PVIO_SET_ERROR(cinfo->mysql, CR_UNKNOWN_HOST, SQLSTATE_UNKNOWN, ER(CR_UNKNOWN_HOST), cinfo->host, gai_rc); - if (bres) - freeaddrinfo(bres); + if (bind_res) + freeaddrinfo(bind_res); goto error; } @@ -767,9 +767,9 @@ my_bool pvio_socket_connect(MARIADB_PVIO *pvio, MA_PVIO_CINFO *cinfo) if (bind_res) { - for (bind_res= bres; bind_res; bind_res= bind_res->ai_next) + for (bres= bind_res; bres; bres= bres->ai_next) { - if (!(rc= bind(csock->socket, bind_res->ai_addr, bind_res->ai_addrlen))) + if (!(rc= bind(csock->socket, bres->ai_addr, bres->ai_addrlen))) break; } if (rc) @@ -795,7 +795,8 @@ my_bool pvio_socket_connect(MARIADB_PVIO *pvio, MA_PVIO_CINFO *cinfo) } freeaddrinfo(res); - freeaddrinfo(bres); + if (bind_res) + freeaddrinfo(bind_res); if (csock->socket == SOCKET_ERROR) { diff --git a/unittest/libmariadb/connection.c b/unittest/libmariadb/connection.c index 272b47f3..a42b183f 100644 --- a/unittest/libmariadb/connection.c +++ b/unittest/libmariadb/connection.c @@ -669,7 +669,80 @@ static int test_conc118(MYSQL *mysql) return OK; } +static int test_wrong_bind_address(MYSQL *my) +{ + char *bind_addr= "100.188.111.112"; + MYSQL *mysql; + + if (!strcmp(hostname, "localhost")) + { + diag("test doesn't work with unix sockets"); + return SKIP; + } + + mysql= mysql_init(NULL); + + mysql_options(mysql, MYSQL_OPT_BIND, bind_addr); + if (mysql_real_connect(mysql, hostname, username, + password, schema, port, socketname, 0)) + { + diag("Error expected"); + mysql_close(mysql); + return FAIL; + } + diag("Error: %s", mysql_error(mysql)); + mysql_close(mysql); + return OK; +} + +static int test_bind_address(MYSQL *my) +{ + MYSQL *mysql; + char *bind_addr= getenv("MYSQL_TEST_BINDADDR"); + char query[128]; + int rc; + + if (!strcmp(hostname, "localhost")) + { + diag("test doesn't work with unix sockets"); + return SKIP; + } + + sprintf(query, "DROP USER '%s'@'%s'", username, bind_addr); + rc= mysql_query(my, query); + + sprintf(query, "CREATE USER '%s'@'%s'", username, bind_addr); + rc= mysql_query(my, query); + check_mysql_rc(rc, my); + + sprintf(query, "GRANT ALL ON %s.* TO '%s'@'%s'", schema, username, bind_addr); + rc= mysql_query(my, query); + check_mysql_rc(rc, my); + + if (!bind_addr) + { + diag("No bind address specified"); + return SKIP; + } + + mysql= mysql_init(NULL); + mysql_options(mysql, MYSQL_OPT_BIND, bind_addr); + + if (!mysql_real_connect(mysql, bind_addr, username, + password, schema, port, socketname, 0)) + { + diag("Error: %s\n", mysql_error(mysql)); + mysql_close(mysql); + return FAIL; + } + diag("%s", mysql_get_host_info(mysql)); + mysql_close(mysql); + return OK; +} + struct my_tests_st my_tests[] = { + {"test_wrong_bind_address", test_wrong_bind_address, TEST_CONNECTION_DEFAULT, 0, NULL, NULL}, + {"test_bind_address", test_bind_address, TEST_CONNECTION_DEFAULT, 0, NULL, NULL}, {"test_conc118", test_conc118, TEST_CONNECTION_DEFAULT, 0, NULL, NULL}, {"test_conc66", test_conc66, TEST_CONNECTION_DEFAULT, 0, NULL, NULL}, {"test_bug20023", test_bug20023, TEST_CONNECTION_NEW, 0, NULL, NULL},