diff --git a/libmariadb/mariadb_lib.c b/libmariadb/mariadb_lib.c
index 55b25df5..a6490f71 100644
--- a/libmariadb/mariadb_lib.c
+++ b/libmariadb/mariadb_lib.c
@@ -207,9 +207,14 @@ void net_get_error(char *buf, size_t buf_len,
 ulong
 ma_net_safe_read(MYSQL *mysql)
 {
-  NET *net= &mysql->net;
+  NET *net;
   ulong len=0;
 
+  if (!mysql)
+    return(packet_error);
+
+  net= &mysql->net;
+
 restart:
   if (net->pvio != 0)
     len=ma_net_read(net);
@@ -401,9 +406,14 @@ int
 mthd_my_send_cmd(MYSQL *mysql,enum enum_server_command command, const char *arg,
 	       size_t length, my_bool skip_check, void *opt_arg)
 {
-  NET *net= &mysql->net;
+  NET *net;
   int result= -1;
 
+  if (!mysql)
+    goto end;
+
+  net= &mysql->net;
+
   /* CONC-589: If reconnect option was specified, we have to check if the connection
                (socket) is still available */
   if (command != COM_QUIT && mysql->options.reconnect && !ma_pvio_is_alive(mysql->net.pvio))
@@ -494,6 +504,9 @@ int
 ma_simple_command(MYSQL *mysql,enum enum_server_command command, const char *arg,
 	       size_t length, my_bool skip_check, void *opt_arg)
 {
+  if (!mysql)
+    return 1;
+
   return mysql->methods->db_command(mysql, command, arg, length, skip_check, opt_arg);
 }
 
@@ -533,6 +546,9 @@ int ma_multi_command(MYSQL *mysql, enum enum_multi_status status)
 
 static void free_old_query(MYSQL *mysql)
 {
+  if (!mysql)
+    return;
+
   if (mysql->fields)
     ma_free_root(&mysql->field_alloc,MYF(0));
   ma_init_alloc_root(&mysql->field_alloc,8192,0);	/* Assume rowlength < 8192 */
@@ -594,6 +610,9 @@ void read_user_name(char *name)
 static void
 end_server(MYSQL *mysql)
 {
+  if (!mysql)
+    return;
+
   /* if net->error 2 and reconnect is activated, we need to inform
      connection handler */
   if (mysql->net.pvio != 0)
@@ -610,6 +629,9 @@ void mthd_my_skip_result(MYSQL *mysql)
 {
   ulong pkt_len;
 
+  if (!mysql)
+    return;
+
   do {
     pkt_len= ma_net_safe_read(mysql);
     if (pkt_len == packet_error)
@@ -1190,7 +1212,12 @@ MYSQL_DATA *mthd_my_read_rows(MYSQL *mysql,MYSQL_FIELD *mysql_fields,
   char	*to, *end_to;
   MYSQL_DATA *result;
   MYSQL_ROWS **prev_ptr,*cur;
-  NET *net = &mysql->net;
+  NET *net;
+
+  if (!mysql)
+    return 0;
+
+  net = &mysql->net;
 
   if ((pkt_len= ma_net_safe_read(mysql)) == packet_error)
     return(0);
@@ -1405,6 +1432,9 @@ const char * STDCALL
 mysql_get_ssl_cipher(MYSQL *mysql __attribute__((unused)))
 {
 #ifdef HAVE_TLS
+  if (!mysql)
+    return(NULL);
+
   if (mysql->net.pvio && mysql->net.pvio->ctls)
   {
     return ma_pvio_tls_cipher(mysql->net.pvio->ctls);
@@ -1420,6 +1450,9 @@ mysql_get_ssl_cipher(MYSQL *mysql __attribute__((unused)))
 
 char *ma_send_connect_attr(MYSQL *mysql, unsigned char *buffer)
 {
+  if (!mysql)
+    return NULL;
+
   if (mysql->server_capabilities & CLIENT_CONNECT_ATTRS)
   {
     buffer= (unsigned char *)mysql_net_store_length((unsigned char *)buffer, (mysql->options.extension) ?
@@ -1496,7 +1529,12 @@ mysql_real_connect(MYSQL *mysql, const char *host, const char *user,
 		   uint port, const char *unix_socket,unsigned long client_flag)
 {
   char *end= NULL;
-  char *connection_handler= (mysql->options.extension) ?
+  char *connection_handler;
+
+  if (!mysql)
+    return NULL;
+
+  connection_handler= (mysql->options.extension) ?
                             mysql->options.extension->connection_handler : 0;
 
   if (!mysql->options.extension || !mysql->options.extension->tls_verification_callback)
@@ -1620,12 +1658,17 @@ MYSQL *mthd_my_real_connect(MYSQL *mysql, const char *host, const char *user,
   char    *scramble_data;
   const char *scramble_plugin;
   uint pkt_length, scramble_len, pkt_scramble_len= 0;
-  NET	*net= &mysql->net;
+  NET	*net;
   my_bool is_multi= 0;
   char *host_copy= NULL;
   struct st_host *host_list= NULL;
   int connect_attempts= 0;
 
+  if (!mysql)
+    return(NULL);
+
+  net= &mysql->net;
+
   if (!mysql->methods)
     mysql->methods= &MARIADB_DEFAULT_METHODS;
 
@@ -2110,7 +2153,12 @@ my_bool STDCALL mariadb_reconnect(MYSQL *mysql)
   MYSQL tmp_mysql;
   struct my_hook_data hook_data;
   struct mysql_async_context *ctxt= NULL;
-  LIST *li_stmt= mysql->stmts;
+  LIST *li_stmt;
+
+  if (!mysql)
+    return 1;
+
+  li_stmt= mysql->stmts;
 
   /* check if connection handler is active */
   if (IS_CONNHDLR_ACTIVE(mysql))
@@ -2258,12 +2306,18 @@ mysql_get_timeout_value_ms(const MYSQL *mysql)
 my_bool	STDCALL mysql_change_user(MYSQL *mysql, const char *user,
 				  const char *passwd, const char *db)
 {
-  const MARIADB_CHARSET_INFO *s_cs= mysql->charset;
-  char *s_user= mysql->user,
-       *s_passwd= mysql->passwd,
-       *s_db= mysql->db;
+  const MARIADB_CHARSET_INFO *s_cs;
+  char *s_user, *s_passwd, *s_db;
   int rc;
 
+  if (!mysql)
+    return 1;
+
+  s_cs= mysql->charset;
+  s_user= mysql->user;
+  s_passwd= mysql->passwd;
+  s_db= mysql->db;
+
   if (mysql->options.charset_name)
     mysql->charset= mysql_find_charset_name(mysql->options.charset_name);
   else
@@ -2333,6 +2387,9 @@ mysql_select_db(MYSQL *mysql, const char *db)
 
 static void mysql_close_options(MYSQL *mysql)
 {
+  if (!mysql)
+    return;
+
   if (mysql->options.init_command)
   {
     char **begin= (char **)mysql->options.init_command->buffer;
@@ -2395,6 +2452,9 @@ static void mysql_close_options(MYSQL *mysql)
 
 static void mysql_close_memory(MYSQL *mysql)
 {
+  if (!mysql)
+    return;
+
   ma_clear_session_state(mysql);
   free(mysql->host_info);
   free(mysql->host);
@@ -2417,6 +2477,9 @@ void my_set_error(MYSQL *mysql,
 
   const char *errmsg= format;
 
+  if (!mysql)
+    return;
+
   mysql->net.last_errno= error_nr;
   ma_strmake(mysql->net.sqlstate, sqlstate, SQLSTATE_LENGTH);
 
@@ -2439,6 +2502,9 @@ void my_set_error(MYSQL *mysql,
 
 void mysql_close_slow_part(MYSQL *mysql)
 {
+  if (!mysql)
+    return;
+
   free_old_query(mysql);
   mysql->status=MYSQL_STATUS_READY; /* Force command */
   mysql->options.reconnect=0;
@@ -2562,7 +2628,8 @@ void ma_save_session_track_info(void *ptr, enum enum_mariadb_status_info type, .
   enum enum_session_state_type track_type;
   va_list ap;
 
-  DBUG_ASSERT(mysql != NULL);
+  if (!mysql)
+    return;
 
   /* We only handle SESSION_TRACK_TYPE here */
   if (type != SESSION_TRACK_TYPE)
@@ -2627,9 +2694,15 @@ mem_error:
 
 int ma_read_ok_packet(MYSQL *mysql, uchar *pos, ulong length)
 {
-  uchar *end= mysql->net.read_pos+length;
+  uchar *end;
   size_t item_len;
-  unsigned int last_status= mysql->server_status;
+  unsigned int last_status;
+
+  if (!mysql)
+    return -1;
+
+  end= mysql->net.read_pos+length;
+  last_status= mysql->server_status;
   mysql->affected_rows= net_field_length_ll(&pos);
   mysql->insert_id=	  net_field_length_ll(&pos);
 
@@ -2866,8 +2939,12 @@ int mthd_my_read_query_result(MYSQL *mysql)
   ulong length;
   const uchar *end;
   uchar has_metadata;
+  my_bool can_local_infile;
+
+  if (!mysql)
+    return(-1);
 
-  my_bool can_local_infile= (mysql->options.extension) && (mysql->extension->auto_local_infile != WAIT_FOR_QUERY);
+  can_local_infile= (mysql->options.extension) && (mysql->extension->auto_local_infile != WAIT_FOR_QUERY);
 
   if (mysql->options.extension && mysql->extension->auto_local_infile == ACCEPT_FILE_REQUEST)
     mysql->extension->auto_local_infile= WAIT_FOR_QUERY;
@@ -2954,6 +3031,9 @@ int STDCALL mysql_session_track_get_next(MYSQL *mysql, enum enum_session_state_t
 int STDCALL mysql_session_track_get_first(MYSQL *mysql, enum enum_session_state_type type,
                                           const char **data, size_t *length)
 {
+  if (!mysql)
+    return 0;
+
   mysql->extension->session_state[type].current= mysql->extension->session_state[type].list;
   return mysql_session_track_get_next(mysql, type, data, length);
 }
@@ -2967,7 +3047,12 @@ mysql_read_query_result(MYSQL *mysql)
 int STDCALL
 mysql_real_query(MYSQL *mysql, const char *query, unsigned long length)
 {
-  my_bool skip_result= OPT_EXT_VAL(mysql, multi_command);
+  my_bool skip_result;
+
+  if (!mysql)
+    return(-1);
+
+  skip_result= OPT_EXT_VAL(mysql, multi_command);
 
   if (length == (unsigned long)-1)
     length= (unsigned long)strlen(query);
@@ -2991,8 +3076,9 @@ mysql_store_result(MYSQL *mysql)
 {
   MYSQL_RES *result;
 
-  if (!mysql->fields)
+  if (!mysql || !mysql->fields)
     return(0);
+
   if (mysql->status != MYSQL_STATUS_GET_RESULT)
   {
     SET_CLIENT_ERROR(mysql, CR_COMMANDS_OUT_OF_SYNC, SQLSTATE_UNKNOWN, 0);
@@ -3039,8 +3125,9 @@ mysql_use_result(MYSQL *mysql)
 {
   MYSQL_RES *result;
 
-  if (!mysql->fields)
+  if (!mysql || !mysql->fields)
     return(0);
+
   if (mysql->status != MYSQL_STATUS_GET_RESULT)
   {
     SET_CLIENT_ERROR(mysql, CR_COMMANDS_OUT_OF_SYNC, SQLSTATE_UNKNOWN, 0);
@@ -3276,6 +3363,9 @@ mysql_list_fields(MYSQL *mysql, const char *table, const char *wild)
   char buff[255];
   int length= 0;
 
+  if (!mysql)
+    return(NULL);
+
   LINT_INIT(query);
 
   length= snprintf(buff, 128, "%s%c%s", table, '\0', wild ? wild : "");
@@ -3319,6 +3409,9 @@ mysql_list_processes(MYSQL *mysql)
   uint field_count;
   uchar *pos;
 
+  if (!mysql)
+    return(NULL);
+
   LINT_INIT(fields);
   if (ma_simple_command(mysql, COM_PROCESS_INFO,0,0,0,0))
     return(NULL);
@@ -3378,6 +3471,9 @@ mysql_dump_debug_info(MYSQL *mysql)
 char * STDCALL
 mysql_stat(MYSQL *mysql)
 {
+  if (!mysql)
+    return(NULL);
+
   if (ma_simple_command(mysql, COM_STATISTICS,0,0,0,0))
     return mysql->net.last_error;
   mysql->net.read_pos[mysql->packet_length]=0;	/* End of stat string */
@@ -3393,6 +3489,10 @@ int STDCALL
 mysql_ping(MYSQL *mysql)
 {
   int rc;
+
+  if (!mysql)
+    return 0;
+
   rc= ma_simple_command(mysql, COM_PING, 0, 0, 0, 0);
   if (rc && mysql->options.reconnect)
     rc= ma_simple_command(mysql, COM_PING, 0, 0, 0, 0);
@@ -3488,6 +3588,20 @@ mysql_optionsv(MYSQL *mysql,enum mysql_option option, ...)
 
   arg1= va_arg(ap, void *);
 
+  if (!mysql) {
+    /* Not connection-related options */
+    switch (option) {
+    case MYSQL_OPT_MAX_ALLOWED_PACKET:
+      max_allowed_packet= (unsigned long)(*(size_t *)arg1);
+      break;
+    case MYSQL_OPT_NET_BUFFER_LENGTH:
+      net_buffer_length= (unsigned long)(*(size_t *)arg1);
+      break;
+    default:
+      goto end;
+    }
+  }
+
   switch (option) {
   case MYSQL_OPT_CONNECT_TIMEOUT:
     mysql->options.connect_timeout= *(uint*) arg1;
@@ -3603,13 +3717,7 @@ mysql_optionsv(MYSQL *mysql,enum mysql_option option, ...)
     mysql->options.extension->async_context= ctxt;
     break;
   case MYSQL_OPT_MAX_ALLOWED_PACKET:
-    if (mysql)
-      mysql->options.max_allowed_packet= (unsigned long)(*(size_t *)arg1);
-    else
-      max_allowed_packet= (unsigned long)(*(size_t *)arg1);
-    break;
-  case MYSQL_OPT_NET_BUFFER_LENGTH:
-    net_buffer_length= (unsigned long)(*(size_t *)arg1);
+    mysql->options.max_allowed_packet= (unsigned long)(*(size_t *)arg1);
     break;
   case MYSQL_OPT_CAN_HANDLE_EXPIRED_PASSWORDS:
     if (*(my_bool *)arg1)
@@ -3922,6 +4030,20 @@ mysql_get_optionv(MYSQL *mysql, enum mysql_option option, void *arg, ...)
 
   va_start(ap, arg);
 
+  if (!mysql) {
+    /* Not connection-related options */
+    switch (option) {
+    case MYSQL_OPT_MAX_ALLOWED_PACKET:
+      *((unsigned long *)arg)= max_allowed_packet;
+      break;
+    case MYSQL_OPT_NET_BUFFER_LENGTH:
+      *((unsigned long *)arg)= net_buffer_length;
+      break;
+    default:
+      goto error;
+    }
+  }
+
   switch(option) {
   case MYSQL_OPT_CONNECT_TIMEOUT:
     *((uint *)arg)= mysql->options.connect_timeout;
@@ -4072,11 +4194,7 @@ mysql_get_optionv(MYSQL *mysql, enum mysql_option option, void *arg, ...)
     }
     break;
   case MYSQL_OPT_MAX_ALLOWED_PACKET:
-    *((unsigned long *)arg)= (mysql) ? mysql->options.max_allowed_packet :
-                                       max_allowed_packet;
-    break;
-  case MYSQL_OPT_NET_BUFFER_LENGTH:
-    *((unsigned long *)arg)= net_buffer_length;
+    *((unsigned long *)arg)= mysql->options.max_allowed_packet;
     break;
   case MYSQL_SECURE_AUTH:
     *((my_bool *)arg)= mysql->options.secure_auth;
@@ -4251,11 +4369,16 @@ const char *STDCALL mysql_info(MYSQL *mysql)
 
 my_bool STDCALL mysql_more_results(MYSQL *mysql)
 {
+  if (!mysql)
+    return(1);
+
   return(test(mysql->server_status & SERVER_MORE_RESULTS_EXIST));
 }
 
 int STDCALL mysql_next_result(MYSQL *mysql)
 {
+  if (!mysql)
+    return(-1);
 
   /* make sure communication is not blocking */
   if (mysql->status != MYSQL_STATUS_READY)
@@ -4316,6 +4439,9 @@ ulong STDCALL
 mysql_real_escape_string(MYSQL *mysql, char *to,const char *from,
 			 ulong length)
 {
+  if (!mysql)
+    return 0;
+
   if (mysql->server_status & SERVER_STATUS_NO_BACKSLASH_ESCAPES)
     return (ulong)mysql_cset_escape_quotes(mysql->charset, to, from, length);
   else
@@ -4348,6 +4474,9 @@ int STDCALL mysql_set_character_set(MYSQL *mysql, const char *csname)
 {
   const MARIADB_CHARSET_INFO *cs;
 
+  if (!mysql)
+  return(0);
+
   if (!csname)
     goto error;
 
@@ -4535,6 +4664,9 @@ ulong STDCALL mysql_hex_string(char *to, const char *from, unsigned long len)
 
 my_bool STDCALL mariadb_connection(MYSQL *mysql)
 {
+  if (!mysql)
+    return(1);
+
   return (strstr(mysql->server_version, "MariaDB") ||
           strstr(mysql->server_version, "-maria-"));
 }
@@ -4590,6 +4722,9 @@ my_bool mariadb_get_infov(MYSQL *mysql, enum mariadb_value value, void *arg, ...
 
   va_start(ap, arg);
 
+  if (!mysql)
+    return(-1);
+
   switch(value) {
 #ifdef HAVE_TLS
   case MARIADB_TLS_PEER_CERT_INFO:
@@ -4616,23 +4751,17 @@ my_bool mariadb_get_infov(MYSQL *mysql, enum mariadb_value value, void *arg, ...
     *((size_t *)arg)= (size_t)net_buffer_length;
     break;
   case MARIADB_CONNECTION_ERROR_ID:
-    if (!mysql)
-      goto error;
     *((unsigned int *)arg)= mysql->net.last_errno;
     break;
   case MARIADB_CONNECTION_ERROR:
-    if (!mysql)
-      goto error;
     *((char **)arg)= mysql->net.last_error;
     break;
   case MARIADB_CONNECTION_SQLSTATE:
-    if (!mysql)
-      goto error;
     *((char **)arg)= mysql->net.sqlstate;
     break;
   case MARIADB_CONNECTION_TLS_VERSION:
     #ifdef HAVE_TLS
-    if (mysql && mysql->net.pvio && mysql->net.pvio->ctls)
+    if (mysql->net.pvio && mysql->net.pvio->ctls)
       *((char **)arg)= (char *)ma_pvio_tls_get_protocol_version(mysql->net.pvio->ctls);
     else
     #endif
@@ -4640,7 +4769,7 @@ my_bool mariadb_get_infov(MYSQL *mysql, enum mariadb_value value, void *arg, ...
     break;
   case MARIADB_CONNECTION_TLS_VERSION_ID:
     #ifdef HAVE_TLS
-    if (mysql && mysql->net.pvio && mysql->net.pvio->ctls)
+    if (mysql->net.pvio && mysql->net.pvio->ctls)
       *((unsigned int *)arg)= ma_pvio_tls_get_protocol_version_id(mysql->net.pvio->ctls);
     else
     #endif
@@ -4660,53 +4789,35 @@ my_bool mariadb_get_infov(MYSQL *mysql, enum mariadb_value value, void *arg, ...
     *((size_t *)arg)= MARIADB_VERSION_ID;
     break;
   case MARIADB_CONNECTION_SERVER_VERSION:
-    if (mysql)
-      *((char **)arg)= mysql->server_version;
-    else
-      goto error;
+    *((char **)arg)= mysql->server_version;
     break;
   case MARIADB_CONNECTION_SERVER_TYPE:
-    if (mysql)
-      *((const char **)arg)= mariadb_connection(mysql) ? "MariaDB" : "MySQL";
-    else
-      goto error;
+    *((const char **)arg)= mariadb_connection(mysql) ? "MariaDB" : "MySQL";
     break;
   case MARIADB_CONNECTION_SERVER_VERSION_ID:
-    if (mysql)
-      *((size_t *)arg)= mariadb_server_version_id(mysql);
-    else
-      goto error;
+    *((size_t *)arg)= mariadb_server_version_id(mysql);
     break;
   case MARIADB_CONNECTION_PROTOCOL_VERSION_ID:
-    if (mysql)
-      *((unsigned int *)arg)= mysql->protocol_version;
-    else
-      goto error;
+    *((unsigned int *)arg)= mysql->protocol_version;
     break;
   case MARIADB_CONNECTION_MARIADB_CHARSET_INFO:
-    if (mysql)
-      mariadb_get_charset_info(mysql, (MY_CHARSET_INFO *)arg);
-    else
-      goto error;
+    mariadb_get_charset_info(mysql, (MY_CHARSET_INFO *)arg);
     break;
   case MARIADB_CONNECTION_SOCKET:
-    if (mysql)
-      *((my_socket *)arg)= mariadb_get_socket(mysql);
-    else
-      goto error;
+    *((my_socket *)arg)= mariadb_get_socket(mysql);
     break;
   case MARIADB_CONNECTION_TYPE:
-    if (mysql  && mysql->net.pvio)
+    if (mysql->net.pvio)
       *((int *)arg)= (int)mysql->net.pvio->type;
     else
       goto error;
     break;
   case MARIADB_CONNECTION_ASYNC_TIMEOUT_MS:
-    if (mysql && mysql->options.extension && mysql->options.extension->async_context)
+    if (mysql->options.extension && mysql->options.extension->async_context)
       *((unsigned int *)arg)= mysql->options.extension->async_context->timeout_value;
     break;
   case MARIADB_CONNECTION_ASYNC_TIMEOUT:
-    if (mysql && mysql->options.extension && mysql->options.extension->async_context)
+    if (mysql->options.extension && mysql->options.extension->async_context)
     {
       unsigned int timeout= mysql->options.extension->async_context->timeout_value;
       if (timeout > UINT_MAX - 999)
@@ -4734,7 +4845,7 @@ my_bool mariadb_get_infov(MYSQL *mysql, enum mariadb_value value, void *arg, ...
     break;
   case MARIADB_CONNECTION_SSL_CIPHER:
     #ifdef HAVE_TLS
-    if (mysql && mysql->net.pvio && mysql->net.pvio->ctls)
+    if (mysql->net.pvio && mysql->net.pvio->ctls)
       *((char **)arg)= (char *)ma_pvio_tls_cipher(mysql->net.pvio->ctls);
     else
     #endif
@@ -4744,70 +4855,40 @@ my_bool mariadb_get_infov(MYSQL *mysql, enum mariadb_value value, void *arg, ...
     *((char ***)arg)= (char **)client_errors;
     break;
   case MARIADB_CONNECTION_INFO:
-    if (mysql)
-      *((char **)arg)= (char *)mysql->info;
-    else
-      goto error;
+    *((char **)arg)= (char *)mysql->info;
     break;
   case MARIADB_CONNECTION_PVIO_TYPE:
-    if (mysql && mysql->net.pvio)
+    if (mysql->net.pvio)
       *((unsigned int *)arg)= (unsigned int)mysql->net.pvio->type;
     else
       goto error;
     break;
   case MARIADB_CONNECTION_SCHEMA:
-    if (mysql)
-      *((char **)arg)= mysql->db;
-    else
-      goto error;
+    *((char **)arg)= mysql->db;
     break;
   case MARIADB_CONNECTION_USER:
-    if (mysql)
-      *((char **)arg)= mysql->user;
-    else
-      goto error;
+    *((char **)arg)= mysql->user;
     break;
   case MARIADB_CONNECTION_PORT:
-    if (mysql)
-      *((unsigned int *)arg)= mysql->port;
-    else
-      goto error;
+    *((unsigned int *)arg)= mysql->port;
     break;
   case MARIADB_CONNECTION_UNIX_SOCKET:
-    if (mysql)
-      *((char **)arg)= mysql->unix_socket;
-    else
-      goto error;
+    *((char **)arg)= mysql->unix_socket;
     break;
   case MARIADB_CONNECTION_HOST:
-    if (mysql)
-      *((char **)arg)= mysql->host;
-    else
-      goto error;
+    *((char **)arg)= mysql->host;
     break;
   case MARIADB_CONNECTION_SERVER_STATUS:
-    if (mysql)
-      *((unsigned int *)arg)= mysql->server_status;
-    else
-      goto error;
+    *((unsigned int *)arg)= mysql->server_status;
     break;
   case MARIADB_CONNECTION_SERVER_CAPABILITIES:
-    if (mysql)
-      *((unsigned long *)arg)= mysql->server_capabilities;
-    else
-      goto error;
+    *((unsigned long *)arg)= mysql->server_capabilities;
     break;
   case MARIADB_CONNECTION_EXTENDED_SERVER_CAPABILITIES:
-    if (mysql)
-      *((unsigned long *)arg)= mysql->extension->mariadb_server_capabilities;
-    else
-      goto error;
+    *((unsigned long *)arg)= mysql->extension->mariadb_server_capabilities;
     break;
   case MARIADB_CONNECTION_CLIENT_CAPABILITIES:
-    if (mysql)
-      *((unsigned long *)arg)= mysql->client_flag;
-    else
-      goto error;
+    *((unsigned long *)arg)= mysql->client_flag;
     break;
   case MARIADB_CONNECTION_BYTES_READ:
     *((size_t *)arg)= mysql->net.pvio->bytes_read;
@@ -4889,6 +4970,9 @@ int STDCALL mysql_reset_connection(MYSQL *mysql)
 {
   int rc;
 
+  if (!mysql)
+    return 1;
+
   /* check if connection handler is active */
   if (IS_CONNHDLR_ACTIVE(mysql))
   {
