[CONCPP-29] Conector/C++ returns NULL on invalid URI instead of throwing an exception Created: 2020-06-30  Updated: 2020-08-11  Resolved: 2020-08-11

Status: Closed
Project: MariaDB Connector/C++
Component/s: General
Affects Version/s: 0.9.2
Fix Version/s: 0.9.3

Type: Bug Priority: Minor
Reporter: Anders Karlsson Assignee: Lawrin Novitsky
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Linux CentOS 7.7



 Description   

The following code cause a core dump. This is due to the URI being badly formatted. The expected behavior would be to throw an exception.

#include <iostream>
#include <exception>
#include <ConnCpp.h>
#include <Exception.h>
 
int main(int argc, char *argv[])
   {
   sql::Driver* driver = sql::mariadb::get_driver_instance();
   sql::Connection *conn;
   sql::Statement *stmt;
 
   try {
      conn = driver->connect("moe:3306", "user", "pwd");
      stmt = conn->createStatement();
   } catch(sql::SQLException &e) {
      std::cerr << "Exception: " << e.what() << "\n";
   }
 
   return 0;
   }

The error is in MariaDbDriver.cpp at:

  Connection* MariaDbDriver::connect(const SQLString& url, Properties& props)
  {
    UrlParser* urlParser= UrlParser::parse(url, props);
 
    if (urlParser == nullptr || urlParser->getHostAddresses().empty())
    {
==>      return nullptr;



 Comments   
Comment by Lawrin Novitsky [ 2020-07-01 ]

karlsson C/J also returns null, and does not throw. Thus I suspect that is in conformance with jdbc specs.

Comment by Anders Karlsson [ 2020-07-02 ]

I had a look, and yes, the connect method of a JDBC Driver returns null if the URL is invalid according to the JDBC Standard. User connecting to a JDBC driver would connect through the DriverManager.getConnection() method though, and this will throw an error instead of returning NULL if the URL is wrong. As there is no DriverManager in C++, a user will have to use the connect method instead, which is reasonable, but it would be better if it them followed the semantics of DriverManager.getConnection().

Comment by Lawrin Novitsky [ 2020-08-11 ]

Introduced DriverManager class with getConnection functionality only. Unlike connecting via Driver class, DriverManager::getConnection methods throw an exception in case connection could not be established for whatever reason. i.e. also if the format of url is incorrect and could not be parsed. DriverManager only permits jdbc:mariadb:// type of url. Well, it's driver manager after all, and needs to know which driver to invoke. Changed unit tests to connect via DriverManager
The fix and the tests changes have been been pushed in the commit 9a8daef

Generated at Thu Feb 08 03:07:49 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.