summaryrefslogtreecommitdiff
path: root/sql-common
diff options
context:
space:
mode:
Diffstat (limited to 'sql-common')
-rw-r--r--sql-common/client.c542
-rw-r--r--sql-common/my_time.c85
-rw-r--r--sql-common/my_user.c20
-rw-r--r--sql-common/pack.c12
4 files changed, 378 insertions, 281 deletions
diff --git a/sql-common/client.c b/sql-common/client.c
index 0748f15a43f..0df7c242969 100644
--- a/sql-common/client.c
+++ b/sql-common/client.c
@@ -1,5 +1,5 @@
-/* Copyright (c) 2003-2008 MySQL AB, 2009 Sun Microsystems, Inc.
- Use is subject to license terms.
+/*
+ Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -12,7 +12,8 @@
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+*/
/*
This file is included by both libmysql.c (the MySQL client C API)
@@ -51,7 +52,7 @@
#define MYSQL_CLIENT
#endif
-#define CLI_MYSQL_REAL_CONNECT cli_mysql_real_connect
+#define CLI_MYSQL_REAL_CONNECT STDCALL cli_mysql_real_connect
#undef net_flush
my_bool net_flush(NET *net);
@@ -71,11 +72,6 @@ my_bool net_flush(NET *net);
#include <my_pthread.h> /* because of signal() */
#endif /* defined(THREAD) && !defined(__WIN__) */
-#if defined(OS2) && defined(MYSQL_SERVER)
-#undef ER
-#define ER CER
-#endif /* defined( OS2) && defined(MYSQL_SERVER) */
-
#include <sys/stat.h>
#include <signal.h>
#include <time.h>
@@ -126,8 +122,9 @@ const char *def_shared_memory_base_name= default_shared_memory_base_name;
static void mysql_close_free_options(MYSQL *mysql);
static void mysql_close_free(MYSQL *mysql);
+static void mysql_prune_stmt_list(MYSQL *mysql);
-#if !(defined(__WIN__) || defined(OS2) || defined(__NETWARE__))
+#if !(defined(__WIN__) || defined(__NETWARE__))
static int wait_for_data(my_socket fd, uint timeout);
#endif
@@ -149,7 +146,7 @@ char mysql_server_last_error[MYSQL_ERRMSG_SIZE];
int my_connect(my_socket fd, const struct sockaddr *name, uint namelen,
uint timeout)
{
-#if defined(__WIN__) || defined(OS2) || defined(__NETWARE__)
+#if defined(__WIN__) || defined(__NETWARE__)
return connect(fd, (struct sockaddr*) name, namelen);
#else
int flags, res, s_err;
@@ -189,7 +186,7 @@ int my_connect(my_socket fd, const struct sockaddr *name, uint namelen,
If not, we will use select()
*/
-#if !(defined(__WIN__) || defined(OS2) || defined(__NETWARE__))
+#if !(defined(__WIN__) || defined(__NETWARE__))
static int wait_for_data(my_socket fd, uint timeout)
{
@@ -242,7 +239,7 @@ static int wait_for_data(my_socket fd, uint timeout)
implementations of select that don't adjust tv upon
failure to reflect the time remaining
*/
- start_time = time(NULL);
+ start_time= my_time(0);
for (;;)
{
tv.tv_sec = (long) timeout;
@@ -256,7 +253,7 @@ static int wait_for_data(my_socket fd, uint timeout)
#endif
if (res == 0) /* timeout */
return -1;
- now_time=time(NULL);
+ now_time= my_time(0);
timeout-= (uint) (now_time - start_time);
if (errno != EINTR || (int) timeout <= 0)
return -1;
@@ -280,7 +277,84 @@ static int wait_for_data(my_socket fd, uint timeout)
return (0); /* ok */
#endif /* HAVE_POLL */
}
-#endif /* defined(__WIN__) || defined(OS2) || defined(__NETWARE__) */
+#endif /* defined(__WIN__) || defined(__NETWARE__) */
+
+/**
+ Set the internal error message to mysql handler
+
+ @param mysql connection handle (client side)
+ @param errcode CR_ error code, passed to ER macro to get
+ error text
+ @parma sqlstate SQL standard sqlstate
+*/
+
+void set_mysql_error(MYSQL *mysql, int errcode, const char *sqlstate)
+{
+ NET *net;
+ DBUG_ENTER("set_mysql_error");
+ DBUG_PRINT("enter", ("error :%d '%s'", errcode, ER(errcode)));
+ DBUG_ASSERT(mysql != 0);
+
+ if (mysql)
+ {
+ net= &mysql->net;
+ net->last_errno= errcode;
+ strmov(net->last_error, ER(errcode));
+ strmov(net->sqlstate, sqlstate);
+ }
+ else
+ {
+ mysql_server_last_errno= errcode;
+ strmov(mysql_server_last_error, ER(errcode));
+ }
+ DBUG_VOID_RETURN;
+}
+
+/**
+ Clear possible error state of struct NET
+
+ @param net clear the state of the argument
+*/
+
+void net_clear_error(NET *net)
+{
+ net->last_errno= 0;
+ net->last_error[0]= '\0';
+ strmov(net->sqlstate, not_error_sqlstate);
+}
+
+/**
+ Set an error message on the client.
+
+ @param mysql connection handle
+ @param errcode CR_* errcode, for client errors
+ @param sqlstate SQL standard sql state, unknown_sqlstate for the
+ majority of client errors.
+ @param format error message template, in sprintf format
+ @param ... variable number of arguments
+*/
+
+static void set_mysql_extended_error(MYSQL *mysql, int errcode,
+ const char *sqlstate,
+ const char *format, ...)
+{
+ NET *net;
+ va_list args;
+ DBUG_ENTER("set_mysql_extended_error");
+ DBUG_PRINT("enter", ("error :%d '%s'", errcode, format));
+ DBUG_ASSERT(mysql != 0);
+
+ net= &mysql->net;
+ net->last_errno= errcode;
+ va_start(args, format);
+ my_vsnprintf(net->last_error, sizeof(net->last_error)-1,
+ format, args);
+ va_end(args);
+ strmov(net->sqlstate, sqlstate);
+
+ DBUG_VOID_RETURN;
+}
+
/*
@@ -289,7 +363,7 @@ static int wait_for_data(my_socket fd, uint timeout)
#ifdef __WIN__
-HANDLE create_named_pipe(NET *net, uint connect_timeout, char **arg_host,
+HANDLE create_named_pipe(MYSQL *mysql, uint connect_timeout, char **arg_host,
char **arg_unix_socket)
{
HANDLE hPipe=INVALID_HANDLE_VALUE;
@@ -317,47 +391,39 @@ HANDLE create_named_pipe(NET *net, uint connect_timeout, char **arg_host,
0,
NULL,
OPEN_EXISTING,
- 0,
+ FILE_FLAG_OVERLAPPED,
NULL )) != INVALID_HANDLE_VALUE)
break;
if (GetLastError() != ERROR_PIPE_BUSY)
{
- net->last_errno=CR_NAMEDPIPEOPEN_ERROR;
- strmov(net->sqlstate, unknown_sqlstate);
- my_snprintf(net->last_error, sizeof(net->last_error)-1,
- ER(net->last_errno), host, unix_socket,
- (ulong) GetLastError());
+ set_mysql_extended_error(mysql, CR_NAMEDPIPEOPEN_ERROR,
+ unknown_sqlstate, ER(CR_NAMEDPIPEOPEN_ERROR),
+ host, unix_socket, (ulong) GetLastError());
return INVALID_HANDLE_VALUE;
}
/* wait for for an other instance */
if (! WaitNamedPipe(pipe_name, connect_timeout*1000) )
{
- net->last_errno=CR_NAMEDPIPEWAIT_ERROR;
- strmov(net->sqlstate, unknown_sqlstate);
- my_snprintf(net->last_error, sizeof(net->last_error)-1,
- ER(net->last_errno), host, unix_socket,
- (ulong) GetLastError());
+ set_mysql_extended_error(mysql, CR_NAMEDPIPEWAIT_ERROR, unknown_sqlstate,
+ ER(CR_NAMEDPIPEWAIT_ERROR),
+ host, unix_socket, (ulong) GetLastError());
return INVALID_HANDLE_VALUE;
}
}
if (hPipe == INVALID_HANDLE_VALUE)
{
- net->last_errno=CR_NAMEDPIPEOPEN_ERROR;
- strmov(net->sqlstate, unknown_sqlstate);
- my_snprintf(net->last_error, sizeof(net->last_error)-1,
- ER(net->last_errno), host, unix_socket,
- (ulong) GetLastError());
+ set_mysql_extended_error(mysql, CR_NAMEDPIPEOPEN_ERROR, unknown_sqlstate,
+ ER(CR_NAMEDPIPEOPEN_ERROR), host, unix_socket,
+ (ulong) GetLastError());
return INVALID_HANDLE_VALUE;
}
dwMode = PIPE_READMODE_BYTE | PIPE_WAIT;
if ( !SetNamedPipeHandleState(hPipe, &dwMode, NULL, NULL) )
{
CloseHandle( hPipe );
- net->last_errno=CR_NAMEDPIPESETSTATE_ERROR;
- strmov(net->sqlstate, unknown_sqlstate);
- my_snprintf(net->last_error, sizeof(net->last_error)-1,
- ER(net->last_errno),host, unix_socket,
- (ulong) GetLastError());
+ set_mysql_extended_error(mysql, CR_NAMEDPIPESETSTATE_ERROR,
+ unknown_sqlstate, ER(CR_NAMEDPIPESETSTATE_ERROR),
+ host, unix_socket, (ulong) GetLastError());
return INVALID_HANDLE_VALUE;
}
*arg_host=host ; *arg_unix_socket=unix_socket; /* connect arg */
@@ -559,7 +625,7 @@ HANDLE create_shared_memory(MYSQL *mysql,NET *net, uint connect_timeout)
err2:
if (error_allow == 0)
{
- net->vio= vio_new_win32shared_memory(net,handle_file_map,handle_map,
+ net->vio= vio_new_win32shared_memory(handle_file_map,handle_map,
event_server_wrote,
event_server_read,event_client_wrote,
event_client_read,event_conn_closed);
@@ -597,14 +663,12 @@ err:
CloseHandle(handle_connect_file_map);
if (error_allow)
{
- net->last_errno=error_allow;
- strmov(net->sqlstate, unknown_sqlstate);
if (error_allow == CR_SHARED_MEMORY_EVENT_ERROR)
- my_snprintf(net->last_error,sizeof(net->last_error)-1,
- ER(net->last_errno),suffix_pos,error_code);
+ set_mysql_extended_error(mysql, error_allow, unknown_sqlstate,
+ ER(error_allow), suffix_pos, error_code);
else
- my_snprintf(net->last_error,sizeof(net->last_error)-1,
- ER(net->last_errno),error_code);
+ set_mysql_extended_error(mysql, error_allow, unknown_sqlstate,
+ ER(error_allow), error_code);
return(INVALID_HANDLE_VALUE);
}
return(handle_map);
@@ -655,6 +719,16 @@ cli_safe_read(MYSQL *mysql)
strmake(net->sqlstate, pos+1, SQLSTATE_LENGTH);
pos+= SQLSTATE_LENGTH+1;
}
+ else
+ {
+ /*
+ The SQL state hasn't been received -- it should be reset to HY000
+ (unknown error sql state).
+ */
+
+ strmov(net->sqlstate, unknown_sqlstate);
+ }
+
(void) strmake(net->last_error,(char*) pos,
min((uint) len,(uint) sizeof(net->last_error)-1));
}
@@ -672,7 +746,9 @@ cli_safe_read(MYSQL *mysql)
mysql->server_status&= ~SERVER_MORE_RESULTS_EXISTS;
DBUG_PRINT("error",("Got error: %d/%s (%s)",
- net->last_errno, net->sqlstate, net->last_error));
+ net->last_errno,
+ net->sqlstate,
+ net->last_error));
return(packet_error);
}
return len;
@@ -683,14 +759,14 @@ void free_rows(MYSQL_DATA *cur)
if (cur)
{
free_root(&cur->alloc,MYF(0));
- my_free((gptr) cur,MYF(0));
+ my_free((uchar*) cur,MYF(0));
}
}
my_bool
cli_advanced_command(MYSQL *mysql, enum enum_server_command command,
- const char *header, ulong header_length,
- const char *arg, ulong arg_length, my_bool skip_check,
+ const uchar *header, ulong header_length,
+ const uchar *arg, ulong arg_length, my_bool skip_check,
MYSQL_STMT *stmt)
{
NET *net= &mysql->net;
@@ -715,13 +791,15 @@ cli_advanced_command(MYSQL *mysql, enum enum_server_command command,
DBUG_RETURN(1);
}
- net->last_error[0]=0;
- net->last_errno= 0;
- strmov(net->sqlstate, not_error_sqlstate);
- mysql->net.report_error=0;
+ net_clear_error(net);
mysql->info=0;
mysql->affected_rows= ~(my_ulonglong) 0;
- net_clear(&mysql->net); /* Clear receive buffer */
+ /*
+ We don't want to clear the protocol buffer on COM_QUIT, because if
+ the previous command was a shutdown command, we may have the
+ response for the COM_QUIT already in the communication buffer
+ */
+ net_clear(&mysql->net, (command != COM_QUIT));
if (net_write_command(net,(uchar) command, header, header_length,
arg, arg_length))
@@ -730,8 +808,7 @@ cli_advanced_command(MYSQL *mysql, enum enum_server_command command,
socket_errno));
if (net->last_errno == ER_NET_PACKET_TOO_LARGE)
{
- net->last_errno=CR_NET_PACKET_TOO_LARGE;
- strmov(net->last_error,ER(net->last_errno));
+ set_mysql_error(mysql, CR_NET_PACKET_TOO_LARGE, unknown_sqlstate);
goto end;
}
end_server(mysql);
@@ -740,8 +817,7 @@ cli_advanced_command(MYSQL *mysql, enum enum_server_command command,
if (net_write_command(net,(uchar) command, header, header_length,
arg, arg_length))
{
- net->last_errno=CR_SERVER_GONE_ERROR;
- strmov(net->last_error,ER(net->last_errno));
+ set_mysql_error(mysql, CR_SERVER_GONE_ERROR, unknown_sqlstate);
goto end;
}
}
@@ -769,56 +845,6 @@ void free_old_query(MYSQL *mysql)
}
/*
- Set the internal error message to mysql handler
-*/
-
-void set_mysql_error(MYSQL *mysql, int errcode, const char *sqlstate)
-{
- NET *net;
- DBUG_ENTER("set_mysql_error");
- DBUG_PRINT("enter", ("error :%d '%s'", errcode, ER(errcode)));
- DBUG_ASSERT(mysql != 0);
-
- if (mysql)
- {
- net= &mysql->net;
- net->last_errno= errcode;
- strmov(net->last_error, ER(errcode));
- strmov(net->sqlstate, sqlstate);
- }
- else
- {
- mysql_server_last_errno= errcode;
- strmov(mysql_server_last_error, ER(errcode));
- }
-
- DBUG_VOID_RETURN;
-}
-
-
-static void set_mysql_extended_error(MYSQL *mysql, int errcode,
- const char *sqlstate,
- const char *format, ...)
-{
- NET *net;
- va_list args;
- DBUG_ENTER("set_mysql_extended_error");
- DBUG_PRINT("enter", ("error :%d '%s'", errcode, format));
- DBUG_ASSERT(mysql != 0);
-
- net= &mysql->net;
- net->last_errno= errcode;
- va_start(args, format);
- my_vsnprintf(net->last_error, sizeof(net->last_error)-1,
- format, args);
- va_end(args);
- strmov(net->sqlstate, sqlstate);
-
- DBUG_VOID_RETURN;
-}
-
-
-/*
Flush result set sent from server
*/
@@ -857,16 +883,15 @@ static my_bool is_NT(void)
#ifdef CHECK_LICENSE
-/*
+/**
Check server side variable 'license'.
- If the variable does not exist or does not contain 'Commercial',
+
+ If the variable does not exist or does not contain 'Commercial',
we're talking to non-commercial server from commercial client.
- SYNOPSIS
- check_license()
- RETURN VALUE
- 0 success
- !0 network error or the server is not commercial.
- Error code is saved in mysql->net.last_errno.
+
+ @retval 0 success
+ @retval !0 network error or the server is not commercial.
+ Error code is saved in mysql->net.last_errno.
*/
static int check_license(MYSQL *mysql)
@@ -881,9 +906,8 @@ static int check_license(MYSQL *mysql)
{
if (net->last_errno == ER_UNKNOWN_SYSTEM_VARIABLE)
{
- net->last_errno= CR_WRONG_LICENSE;
- my_snprintf(net->last_error, sizeof(net->last_error)-1,
- ER(net->last_errno), required_license);
+ set_mysql_extended_error(mysql, CR_WRONG_LICENSE, unknown_sqlstate,
+ ER(CR_WRONG_LICENSE), required_license);
}
return 1;
}
@@ -899,9 +923,8 @@ static int check_license(MYSQL *mysql)
(!row || !row[0] ||
strncmp(row[0], required_license, sizeof(required_license))))
{
- net->last_errno= CR_WRONG_LICENSE;
- my_snprintf(net->last_error, sizeof(net->last_error)-1,
- ER(net->last_errno), required_license);
+ set_mysql_extended_error(mysql, CR_WRONG_LICENSE, unknown_sqlstate,
+ ER(CR_WRONG_LICENSE), required_license);
}
mysql_free_result(res);
return net->last_errno;
@@ -928,6 +951,7 @@ void end_server(MYSQL *mysql)
vio_delete(mysql->net.vio);
reset_sigpipe(mysql);
mysql->net.vio= 0; /* Marker */
+ mysql_prune_stmt_list(mysql);
}
net_end(&mysql->net);
free_old_query(mysql);
@@ -960,8 +984,8 @@ mysql_free_result(MYSQL_RES *result)
if (result->fields)
free_root(&result->field_alloc,MYF(0));
if (result->row)
- my_free((gptr) result->row,MYF(0));
- my_free((gptr) result,MYF(0));
+ my_free((uchar*) result->row,MYF(0));
+ my_free((uchar*) result,MYF(0));
}
DBUG_VOID_RETURN;
}
@@ -1004,7 +1028,7 @@ static int add_init_command(struct st_mysql_options *options, const char *cmd)
}
if (!(tmp= my_strdup(cmd,MYF(MY_WME))) ||
- insert_dynamic(options->init_commands, (gptr)&tmp))
+ insert_dynamic(options->init_commands, (uchar*)&tmp))
{
my_free(tmp, MYF(MY_ALLOW_ZERO_PTR));
return 1;
@@ -1025,7 +1049,7 @@ void mysql_read_default_options(struct st_mysql_options *options,
argc=1; argv=argv_buff; argv_buff[0]= (char*) "client";
groups[0]= (char*) "client"; groups[1]= (char*) group; groups[2]=0;
- load_defaults(filename, groups, &argc, &argv);
+ my_load_defaults(filename, groups, &argc, &argv, NULL);
if (argc != 1) /* If some default option */
{
char **option=argv;
@@ -1221,7 +1245,7 @@ static void cli_fetch_lengths(ulong *to, MYSQL_ROW column,
unsigned int field_count)
{
ulong *prev_length;
- byte *start=0;
+ char *start=0;
MYSQL_ROW end;
prev_length=0; /* Keep gcc happy */
@@ -1763,7 +1787,6 @@ static MYSQL_METHODS client_methods=
C_MODE_START
int mysql_init_character_set(MYSQL *mysql)
{
- NET *net= &mysql->net;
const char *default_collation_name;
/* Set character set */
@@ -1807,24 +1830,22 @@ int mysql_init_character_set(MYSQL *mysql)
}
charsets_dir= save;
}
-
+
if (!mysql->charset)
{
- net->last_errno=CR_CANT_READ_CHARSET;
- strmov(net->sqlstate, unknown_sqlstate);
if (mysql->options.charset_dir)
- my_snprintf(net->last_error, sizeof(net->last_error)-1,
- ER(net->last_errno),
- mysql->options.charset_name,
- mysql->options.charset_dir);
+ set_mysql_extended_error(mysql, CR_CANT_READ_CHARSET, unknown_sqlstate,
+ ER(CR_CANT_READ_CHARSET),
+ mysql->options.charset_name,
+ mysql->options.charset_dir);
else
{
char cs_dir_name[FN_REFLEN];
get_charsets_dir(cs_dir_name);
- my_snprintf(net->last_error, sizeof(net->last_error)-1,
- ER(net->last_errno),
- mysql->options.charset_name,
- cs_dir_name);
+ set_mysql_extended_error(mysql, CR_CANT_READ_CHARSET, unknown_sqlstate,
+ ER(CR_CANT_READ_CHARSET),
+ mysql->options.charset_name,
+ cs_dir_name);
}
return 1;
}
@@ -1928,7 +1949,13 @@ CLI_MYSQL_REAL_CONNECT(MYSQL *mysql,const char *host, const char *user,
(int) have_tcpip));
if (mysql->options.protocol == MYSQL_PROTOCOL_MEMORY)
goto error;
- /* Try also with PIPE or TCP/IP */
+
+ /*
+ Try also with PIPE or TCP/IP. Clear the error from
+ create_shared_memory().
+ */
+
+ net_clear_error(net);
}
else
{
@@ -1955,10 +1982,10 @@ CLI_MYSQL_REAL_CONNECT(MYSQL *mysql,const char *host, const char *user,
DBUG_PRINT("info",("Using UNIX sock '%s'",unix_socket));
if ((sock = socket(AF_UNIX,SOCK_STREAM,0)) == SOCKET_ERROR)
{
- net->last_errno=CR_SOCKET_CREATE_ERROR;
- strmov(net->sqlstate, unknown_sqlstate);
- my_snprintf(net->last_error,sizeof(net->last_error)-1,
- ER(net->last_errno),socket_errno);
+ set_mysql_extended_error(mysql, CR_SOCKET_CREATE_ERROR,
+ unknown_sqlstate,
+ ER(CR_SOCKET_CREATE_ERROR),
+ socket_errno);
goto error;
}
net->vio= vio_new(sock, VIO_TYPE_SOCKET,
@@ -1971,10 +1998,10 @@ CLI_MYSQL_REAL_CONNECT(MYSQL *mysql,const char *host, const char *user,
{
DBUG_PRINT("error",("Got error %d on connect to local server",
socket_errno));
- net->last_errno=CR_CONNECTION_ERROR;
- strmov(net->sqlstate, unknown_sqlstate);
- my_snprintf(net->last_error,sizeof(net->last_error)-1,
- ER(net->last_errno),unix_socket,socket_errno);
+ set_mysql_extended_error(mysql, CR_CONNECTION_ERROR,
+ unknown_sqlstate,
+ ER(CR_CONNECTION_ERROR),
+ unix_socket, socket_errno);
goto error;
}
mysql->options.protocol=MYSQL_PROTOCOL_SOCKET;
@@ -1986,8 +2013,8 @@ CLI_MYSQL_REAL_CONNECT(MYSQL *mysql,const char *host, const char *user,
(! have_tcpip && (unix_socket || !host && is_NT()))))
{
sock=0;
- if ((hPipe=create_named_pipe(net, mysql->options.connect_timeout,
- (char**) &host, (char**) &unix_socket)) ==
+ if ((hPipe= create_named_pipe(mysql, mysql->options.connect_timeout,
+ (char**) &host, (char**) &unix_socket)) ==
INVALID_HANDLE_VALUE)
{
DBUG_PRINT("error",
@@ -2003,7 +2030,7 @@ CLI_MYSQL_REAL_CONNECT(MYSQL *mysql,const char *host, const char *user,
}
else
{
- net->vio=vio_new_win32pipe(hPipe);
+ net->vio= vio_new_win32pipe(hPipe);
my_snprintf(host_info=buff, sizeof(buff)-1,
ER(CR_NAMEDPIPE_CONNECTION), unix_socket);
}
@@ -2013,6 +2040,7 @@ CLI_MYSQL_REAL_CONNECT(MYSQL *mysql,const char *host, const char *user,
(!mysql->options.protocol ||
mysql->options.protocol == MYSQL_PROTOCOL_TCP))
{
+ int status= -1;
unix_socket=0; /* This is not used */
if (!port)
port=mysql_port;
@@ -2031,15 +2059,14 @@ CLI_MYSQL_REAL_CONNECT(MYSQL *mysql,const char *host, const char *user,
#endif
if (sock == SOCKET_ERROR)
{
- net->last_errno=CR_IPSOCK_ERROR;
- strmov(net->sqlstate, unknown_sqlstate);
- my_snprintf(net->last_error,sizeof(net->last_error)-1,
- ER(net->last_errno),socket_errno);
+ set_mysql_extended_error(mysql, CR_IPSOCK_ERROR, unknown_sqlstate,
+ ER(CR_IPSOCK_ERROR), socket_errno);
goto error;
}
net->vio= vio_new(sock, VIO_TYPE_TCPIP, VIO_BUFFERED_READ);
bzero((char*) &sock_addr,sizeof(sock_addr));
sock_addr.sin_family = AF_INET;
+ sock_addr.sin_port = (ushort) htons((ushort) port);
/*
The server name may be a host name or IP address
@@ -2048,37 +2075,51 @@ CLI_MYSQL_REAL_CONNECT(MYSQL *mysql,const char *host, const char *user,
if ((int) (ip_addr = inet_addr(host)) != (int) INADDR_NONE)
{
memcpy_fixed(&sock_addr.sin_addr,&ip_addr,sizeof(ip_addr));
+ status= my_connect(sock, (struct sockaddr *) &sock_addr,
+ sizeof(sock_addr), mysql->options.connect_timeout);
}
else
{
- int tmp_errno;
+ int i, tmp_errno;
struct hostent tmp_hostent,*hp;
char buff2[GETHOSTBYNAME_BUFF_SIZE];
hp = my_gethostbyname_r(host,&tmp_hostent,buff2,sizeof(buff2),
&tmp_errno);
- if (!hp)
+
+ /*
+ Don't attempt to connect to non IPv4 addresses as the client could
+ end up sending information to a unknown server. For example, a IPv6
+ address might be returned from gethostbyname depending on options
+ set via the RES_OPTIONS environment variable.
+ */
+ if (!hp || (hp->h_addrtype != AF_INET))
{
my_gethostbyname_r_free();
- net->last_errno=CR_UNKNOWN_HOST;
- strmov(net->sqlstate, unknown_sqlstate);
- my_snprintf(net->last_error, sizeof(net->last_error)-1,
- ER(CR_UNKNOWN_HOST), host, tmp_errno);
+ set_mysql_extended_error(mysql, CR_UNKNOWN_HOST, unknown_sqlstate,
+ ER(CR_UNKNOWN_HOST), host, tmp_errno);
goto error;
}
- memcpy(&sock_addr.sin_addr, hp->h_addr,
- min(sizeof(sock_addr.sin_addr), (size_t) hp->h_length));
+
+ for (i= 0; status && hp->h_addr_list[i]; i++)
+ {
+ IF_DBUG(char ipaddr[18];)
+ memcpy(&sock_addr.sin_addr, hp->h_addr_list[i],
+ min(sizeof(sock_addr.sin_addr), (size_t) hp->h_length));
+ DBUG_PRINT("info",("Trying %s...",
+ (my_inet_ntoa(sock_addr.sin_addr, ipaddr), ipaddr)));
+ status= my_connect(sock, (struct sockaddr *) &sock_addr,
+ sizeof(sock_addr), mysql->options.connect_timeout);
+ }
+
my_gethostbyname_r_free();
}
- sock_addr.sin_port = (ushort) htons((ushort) port);
- if (my_connect(sock,(struct sockaddr *) &sock_addr, sizeof(sock_addr),
- mysql->options.connect_timeout))
+
+ if (status)
{
DBUG_PRINT("error",("Got error %d on connect to '%s'",socket_errno,
host));
- net->last_errno= CR_CONN_HOST_ERROR;
- strmov(net->sqlstate, unknown_sqlstate);
- my_snprintf(net->last_error, sizeof(net->last_error)-1,
- ER(CR_CONN_HOST_ERROR), host, socket_errno);
+ set_mysql_extended_error(mysql, CR_CONN_HOST_ERROR, unknown_sqlstate,
+ ER(CR_CONN_HOST_ERROR), host, socket_errno);
goto error;
}
}
@@ -2142,11 +2183,9 @@ CLI_MYSQL_REAL_CONNECT(MYSQL *mysql,const char *host, const char *user,
PROTOCOL_VERSION, mysql->protocol_version));
if (mysql->protocol_version != PROTOCOL_VERSION)
{
- strmov(net->sqlstate, unknown_sqlstate);
- net->last_errno= CR_VERSION_ERROR;
- my_snprintf(net->last_error, sizeof(net->last_error)-1,
- ER(CR_VERSION_ERROR), mysql->protocol_version,
- PROTOCOL_VERSION);
+ set_mysql_extended_error(mysql, CR_VERSION_ERROR, unknown_sqlstate,
+ ER(CR_VERSION_ERROR), mysql->protocol_version,
+ PROTOCOL_VERSION);
goto error;
}
end=strend((char*) net->read_pos+1);
@@ -2265,7 +2304,7 @@ CLI_MYSQL_REAL_CONNECT(MYSQL *mysql,const char *host, const char *user,
Send client_flag, max_packet_size - unencrypted otherwise
the server does not know we want to do SSL
*/
- if (my_net_write(net,buff,(uint) (end-buff)) || net_flush(net))
+ if (my_net_write(net, (uchar*) buff, (uint) (end-buff)) || net_flush(net))
{
set_mysql_extended_error(mysql, CR_SERVER_LOST, unknown_sqlstate,
ER(CR_SERVER_LOST_EXTENDED),
@@ -2347,7 +2386,7 @@ CLI_MYSQL_REAL_CONNECT(MYSQL *mysql,const char *host, const char *user,
db= 0;
}
/* Write authentication package */
- if (my_net_write(net,buff,(ulong) (end-buff)) || net_flush(net))
+ if (my_net_write(net, (uchar*) buff, (size_t) (end-buff)) || net_flush(net))
{
set_mysql_extended_error(mysql, CR_SERVER_LOST, unknown_sqlstate,
ER(CR_SERVER_LOST_EXTENDED),
@@ -2379,7 +2418,8 @@ CLI_MYSQL_REAL_CONNECT(MYSQL *mysql,const char *host, const char *user,
password in old format.
*/
scramble_323(buff, mysql->scramble, passwd);
- if (my_net_write(net, buff, SCRAMBLE_LENGTH_323 + 1) || net_flush(net))
+ if (my_net_write(net, (uchar*) buff, SCRAMBLE_LENGTH_323 + 1) ||
+ net_flush(net))
{
set_mysql_extended_error(mysql, CR_SERVER_LOST, unknown_sqlstate,
ER(CR_SERVER_LOST_EXTENDED),
@@ -2417,6 +2457,11 @@ CLI_MYSQL_REAL_CONNECT(MYSQL *mysql,const char *host, const char *user,
goto error;
}
+ /*
+ Using init_commands is not supported when connecting from within the
+ server.
+ */
+#ifndef MYSQL_SERVER
if (mysql->options.init_commands)
{
DYNAMIC_ARRAY *init_commands= mysql->options.init_commands;
@@ -2428,18 +2473,26 @@ CLI_MYSQL_REAL_CONNECT(MYSQL *mysql,const char *host, const char *user,
for (; ptr < end_command; ptr++)
{
- MYSQL_RES *res;
+ int status;
+
if (mysql_real_query(mysql,*ptr, (ulong) strlen(*ptr)))
goto error;
- if (mysql->fields)
- {
- if (!(res= cli_use_result(mysql)))
- goto error;
- mysql_free_result(res);
- }
+
+ do {
+ if (mysql->fields)
+ {
+ MYSQL_RES *res;
+ if (!(res= cli_use_result(mysql)))
+ goto error;
+ mysql_free_result(res);
+ }
+ if ((status= mysql_next_result(mysql)) > 0)
+ goto error;
+ } while (status == 0);
}
mysql->reconnect=reconnect;
}
+#endif
#ifndef TO_BE_DELETED
if (mysql->options.rpl_probe && mysql_rpl_probe(mysql))
@@ -2453,7 +2506,9 @@ CLI_MYSQL_REAL_CONNECT(MYSQL *mysql,const char *host, const char *user,
error:
reset_sigpipe(mysql);
DBUG_PRINT("error",("message: %u/%s (%s)",
- net->last_errno, net->sqlstate, net->last_error));
+ net->last_errno,
+ net->sqlstate,
+ net->last_error));
{
/* Free alloced memory */
end_server(mysql);
@@ -2531,30 +2586,9 @@ my_bool mysql_reconnect(MYSQL *mysql)
tmp_mysql.reconnect= 1;
tmp_mysql.free_me= mysql->free_me;
- /*
- For each stmt in mysql->stmts, move it to tmp_mysql if it is
- in state MYSQL_STMT_INIT_DONE, otherwise close it.
- */
- {
- LIST *element= mysql->stmts;
- for (; element; element= element->next)
- {
- MYSQL_STMT *stmt= (MYSQL_STMT *) element->data;
- if (stmt->state != MYSQL_STMT_INIT_DONE)
- {
- stmt->mysql= 0;
- stmt->last_errno= CR_SERVER_LOST;
- strmov(stmt->last_error, ER(CR_SERVER_LOST));
- strmov(stmt->sqlstate, unknown_sqlstate);
- }
- else
- {
- tmp_mysql.stmts= list_add(tmp_mysql.stmts, &stmt->list);
- }
- /* No need to call list_delete for statement here */
- }
- mysql->stmts= NULL;
- }
+ /* Move prepared statements (if any) over to the new mysql object */
+ tmp_mysql.stmts= mysql->stmts;
+ mysql->stmts= 0;
/* Don't free options as these are now used in tmp_mysql */
bzero((char*) &mysql->options,sizeof(mysql->options));
@@ -2562,7 +2596,7 @@ my_bool mysql_reconnect(MYSQL *mysql)
mysql_close(mysql);
*mysql=tmp_mysql;
mysql_fix_pointers(mysql, &tmp_mysql); /* adjust connection pointers */
- net_clear(&mysql->net);
+ net_clear(&mysql->net, 1);
mysql->affected_rows= ~(my_ulonglong) 0;
DBUG_RETURN(0);
}
@@ -2579,7 +2613,8 @@ mysql_select_db(MYSQL *mysql, const char *db)
DBUG_ENTER("mysql_select_db");
DBUG_PRINT("enter",("db: '%s'",db));
- if ((error=simple_command(mysql,COM_INIT_DB,db,(ulong) strlen(db),0)))
+ if ((error=simple_command(mysql,COM_INIT_DB, (const uchar*) db,
+ (ulong) strlen(db),0)))
DBUG_RETURN(error);
my_free(mysql->db,MYF(MY_ALLOW_ZERO_PTR));
mysql->db=my_strdup(db,MYF(MY_WME));
@@ -2630,7 +2665,7 @@ static void mysql_close_free_options(MYSQL *mysql)
static void mysql_close_free(MYSQL *mysql)
{
- my_free((gptr) mysql->host_info,MYF(MY_ALLOW_ZERO_PTR));
+ my_free((uchar*) mysql->host_info,MYF(MY_ALLOW_ZERO_PTR));
my_free(mysql->user,MYF(MY_ALLOW_ZERO_PTR));
my_free(mysql->passwd,MYF(MY_ALLOW_ZERO_PTR));
my_free(mysql->db,MYF(MY_ALLOW_ZERO_PTR));
@@ -2643,6 +2678,46 @@ static void mysql_close_free(MYSQL *mysql)
}
+/**
+ For use when the connection to the server has been lost (in which case
+ the server has discarded all information about prepared statements
+ associated with the connection).
+
+ Mark all statements in mysql->stmts by setting stmt->mysql= 0 if the
+ statement has transitioned beyond the MYSQL_STMT_INIT_DONE state, and
+ unlink the statement from the mysql->stmts list.
+
+ The remaining pruned list of statements (if any) is kept in mysql->stmts.
+
+ @param mysql pointer to the MYSQL object
+
+ @return none
+*/
+static void mysql_prune_stmt_list(MYSQL *mysql)
+{
+ LIST *element= mysql->stmts;
+ LIST *pruned_list= 0;
+
+ for (; element; element= element->next)
+ {
+ MYSQL_STMT *stmt= (MYSQL_STMT *) element->data;
+ if (stmt->state != MYSQL_STMT_INIT_DONE)
+ {
+ stmt->mysql= 0;
+ stmt->last_errno= CR_SERVER_LOST;
+ strmov(stmt->last_error, ER(CR_SERVER_LOST));
+ strmov(stmt->sqlstate, unknown_sqlstate);
+ }
+ else
+ {
+ pruned_list= list_add(pruned_list, element);
+ }
+ }
+
+ mysql->stmts= pruned_list;
+}
+
+
/*
Clear connection pointer of every statement: this is necessary
to give error on attempt to use a prepared statement of closed
@@ -2651,24 +2726,32 @@ static void mysql_close_free(MYSQL *mysql)
SYNOPSYS
mysql_detach_stmt_list()
stmt_list pointer to mysql->stmts
+ func_name name of calling function
NOTE
There is similar code in mysql_reconnect(), so changes here
should also be reflected there.
*/
-void mysql_detach_stmt_list(LIST **stmt_list __attribute__((unused)))
+void mysql_detach_stmt_list(LIST **stmt_list __attribute__((unused)),
+ const char *func_name __attribute__((unused)))
{
#ifdef MYSQL_CLIENT
/* Reset connection handle in all prepared statements. */
LIST *element= *stmt_list;
+ char buff[MYSQL_ERRMSG_SIZE];
+ DBUG_ENTER("mysql_detach_stmt_list");
+
+ my_snprintf(buff, sizeof(buff)-1, ER(CR_STMT_CLOSED), func_name);
for (; element; element= element->next)
{
MYSQL_STMT *stmt= (MYSQL_STMT *) element->data;
+ set_stmt_error(stmt, CR_STMT_CLOSED, unknown_sqlstate, buff);
stmt->mysql= 0;
/* No need to call list_delete for statement here */
}
*stmt_list= 0;
+ DBUG_VOID_RETURN;
#endif /* MYSQL_CLIENT */
}
@@ -2684,12 +2767,12 @@ void STDCALL mysql_close(MYSQL *mysql)
free_old_query(mysql);
mysql->status=MYSQL_STATUS_READY; /* Force command */
mysql->reconnect=0;
- simple_command(mysql,COM_QUIT,NullS,0,1);
+ simple_command(mysql,COM_QUIT,(uchar*) 0,0,1);
end_server(mysql); /* Sets mysql->net.vio= 0 */
}
mysql_close_free_options(mysql);
mysql_close_free(mysql);
- mysql_detach_stmt_list(&mysql->stmts);
+ mysql_detach_stmt_list(&mysql->stmts, "mysql_close");
#ifndef TO_BE_DELETED
/* free/close slave list */
if (mysql->rpl_pivot)
@@ -2712,7 +2795,7 @@ void STDCALL mysql_close(MYSQL *mysql)
(*mysql->methods->free_embedded_thd)(mysql);
#endif
if (mysql->free_me)
- my_free((gptr) mysql,MYF(0));
+ my_free((uchar*) mysql,MYF(0));
}
DBUG_VOID_RETURN;
}
@@ -2766,7 +2849,15 @@ get_info:
#ifdef MYSQL_CLIENT
if (field_count == NULL_LENGTH) /* LOAD DATA LOCAL INFILE */
{
- int error=handle_local_infile(mysql,(char*) pos);
+ int error;
+
+ if (!(mysql->options.client_flag & CLIENT_LOCAL_FILES))
+ {
+ set_mysql_error(mysql, CR_MALFORMED_PACKET, unknown_sqlstate);
+ DBUG_RETURN(1);
+ }
+
+ error= handle_local_infile(mysql,(char*) pos);
if ((length= cli_safe_read(mysql)) == packet_error || error)
DBUG_RETURN(1);
goto get_info; /* Get info packet */
@@ -2815,7 +2906,7 @@ mysql_send_query(MYSQL* mysql, const char* query, ulong length)
mysql->last_used_con = mysql;
#endif
- DBUG_RETURN(simple_command(mysql, COM_QUERY, query, length, 1));
+ DBUG_RETURN(simple_command(mysql, COM_QUERY, (uchar*) query, length, 1));
}
@@ -2865,7 +2956,7 @@ MYSQL_RES * STDCALL mysql_store_result(MYSQL *mysql)
if (!(result->data=
(*mysql->methods->read_rows)(mysql,mysql->fields,mysql->field_count)))
{
- my_free((gptr) result,MYF(0));
+ my_free((uchar*) result,MYF(0));
DBUG_RETURN(0);
}
mysql->affected_rows= result->row_count= result->data->rows;
@@ -2875,6 +2966,7 @@ MYSQL_RES * STDCALL mysql_store_result(MYSQL *mysql)
result->field_count= mysql->field_count;
/* The rest of result members is bzeroed in malloc */
mysql->fields=0; /* fields is now in result */
+ clear_alloc_root(&mysql->field_alloc);
/* just in case this was mistakenly called after mysql_stmt_execute() */
mysql->unbuffered_fetch_owner= 0;
DBUG_RETURN(result); /* Data fetched */
@@ -2914,7 +3006,7 @@ static MYSQL_RES * cli_use_result(MYSQL *mysql)
if (!(result->row=(MYSQL_ROW)
my_malloc(sizeof(result->row[0])*(mysql->field_count+1), MYF(MY_WME))))
{ /* Ptrs: to one row */
- my_free((gptr) result,MYF(0));
+ my_free((uchar*) result,MYF(0));
DBUG_RETURN(0);
}
result->fields= mysql->fields;
@@ -2924,6 +3016,7 @@ static MYSQL_RES * cli_use_result(MYSQL *mysql)
result->handle= mysql;
result->current_row= 0;
mysql->fields=0; /* fields is now in result */
+ clear_alloc_root(&mysql->field_alloc);
mysql->status=MYSQL_STATUS_USE_RESULT;
mysql->unbuffered_fetch_owner= &result->unbuffered_fetch_cancelled;
DBUG_RETURN(result); /* Data is read to be fetched */
@@ -3003,7 +3096,7 @@ mysql_fetch_lengths(MYSQL_RES *res)
int STDCALL
-mysql_options(MYSQL *mysql,enum mysql_option option, const char *arg)
+mysql_options(MYSQL *mysql,enum mysql_option option, const void *arg)
{
DBUG_ENTER("mysql_option");
DBUG_PRINT("enter",("option: %d",(int) option));
@@ -3178,11 +3271,8 @@ int STDCALL mysql_set_character_set(MYSQL *mysql, const char *cs_name)
{
char cs_dir_name[FN_REFLEN];
get_charsets_dir(cs_dir_name);
- mysql->net.last_errno= CR_CANT_READ_CHARSET;
- strmov(mysql->net.sqlstate, unknown_sqlstate);
- my_snprintf(mysql->net.last_error, sizeof(mysql->net.last_error) - 1,
- ER(mysql->net.last_errno), cs_name, cs_dir_name);
-
+ set_mysql_extended_error(mysql, CR_CANT_READ_CHARSET, unknown_sqlstate,
+ ER(CR_CANT_READ_CHARSET), cs_name, cs_dir_name);
}
charsets_dir= save_csdir;
return mysql->net.last_errno;
diff --git a/sql-common/my_time.c b/sql-common/my_time.c
index c63e4d0cdd4..bd60277d1bc 100644
--- a/sql-common/my_time.c
+++ b/sql-common/my_time.c
@@ -1,4 +1,5 @@
-/* Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
+/*
+ Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -11,7 +12,8 @@
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+*/
#include <my_time.h>
#include <m_string.h>
@@ -262,8 +264,19 @@ str_to_datetime(const char *str, uint length, MYSQL_TIME *l_time,
{
const char *start= str;
ulong tmp_value= (uint) (uchar) (*str++ - '0');
+
+ /*
+ Internal format means no delimiters; every field has a fixed
+ width. Otherwise, we scan until we find a delimiter and discard
+ leading zeroes -- except for the microsecond part, where leading
+ zeroes are significant, and where we never process more than six
+ digits.
+ */
+ my_bool scan_until_delim= !is_internal_format &&
+ ((i != format_position[6]));
+
while (str != end && my_isdigit(&my_charset_latin1,str[0]) &&
- (!is_internal_format || --field_length))
+ (scan_until_delim || --field_length))
{
tmp_value=tmp_value*10 + (ulong) (uchar) (*str - '0');
str++;
@@ -299,9 +312,6 @@ str_to_datetime(const char *str, uint length, MYSQL_TIME *l_time,
field_length= 6; /* 6 digits */
}
continue;
-
- /* No part seconds */
- date[++i]= 0;
}
while (str != end &&
(my_ispunct(&my_charset_latin1,*str) ||
@@ -440,9 +450,7 @@ str_to_datetime(const char *str, uint length, MYSQL_TIME *l_time,
}
}
- DBUG_RETURN(l_time->time_type=
- (number_of_fields <= 3 ? MYSQL_TIMESTAMP_DATE :
- MYSQL_TIMESTAMP_DATETIME));
+ DBUG_RETURN(l_time->time_type);
err:
bzero((char*) l_time, sizeof(*l_time));
@@ -451,7 +459,7 @@ err:
/*
- Convert a time string to a TIME struct.
+ Convert a time string to a MYSQL_TIME struct.
SYNOPSIS
str_to_time()
@@ -567,12 +575,12 @@ my_bool str_to_time(const char *str, uint length, MYSQL_TIME *l_time,
/* Fix the date to assume that seconds was given */
if (!found_hours && !found_days)
{
- bmove_upp((char*) (date+4), (char*) (date+state),
+ bmove_upp((uchar*) (date+4), (uchar*) (date+state),
sizeof(long)*(state-1));
- bzero((char*) date, sizeof(long)*(4-state));
+ bzero((uchar*) date, sizeof(long)*(4-state));
}
else
- bzero((char*) (date+state), sizeof(long)*(4-state));
+ bzero((uchar*) (date+state), sizeof(long)*(4-state));
}
fractional:
@@ -637,11 +645,11 @@ fractional:
l_time->second_part= date[4];
l_time->time_type= MYSQL_TIMESTAMP_TIME;
- /* Check if the value is valid and fits into TIME range */
+ /* Check if the value is valid and fits into MYSQL_TIME range */
if (check_time_range(l_time, warning))
return 1;
- /* Check if there is garbage at end of the TIME specification */
+ /* Check if there is garbage at end of the MYSQL_TIME specification */
if (str != end)
{
do
@@ -658,11 +666,11 @@ fractional:
/*
- Check 'time' value to lie in the TIME range
+ Check 'time' value to lie in the MYSQL_TIME range
SYNOPSIS:
check_time_range()
- time pointer to TIME value
+ time pointer to MYSQL_TIME value
warning set MYSQL_TIME_WARN_OUT_OF_RANGE flag if the value is out of range
DESCRIPTION
@@ -766,16 +774,18 @@ long calc_daynr(uint year,uint month,uint day)
int y= year; /* may be < 0 temporarily */
DBUG_ENTER("calc_daynr");
- if (y == 0 && month == 0 && day == 0)
+ if (y == 0 && month == 0)
DBUG_RETURN(0); /* Skip errors */
- delsum= (long) (365L * y+ 31*(month-1) +day);
+ /* Cast to int to be able to handle month == 0 */
+ delsum= (long) (365 * y + 31 *((int) month - 1) + (int) day);
if (month <= 2)
y--;
else
- delsum-= (long) (month*4+23)/10;
+ delsum-= (long) ((int) month * 4 + 23) / 10;
temp=(int) ((y/100+1)*3)/4;
DBUG_PRINT("exit",("year: %d month: %d day: %d -> daynr: %ld",
y+(month <= 2),month,day,delsum+y/4-temp));
+ DBUG_ASSERT(delsum+(int) y/4-temp >= 0);
DBUG_RETURN(delsum+(int) y/4-temp);
} /* calc_daynr */
@@ -1004,7 +1014,7 @@ void set_zero_time(MYSQL_TIME *tm, enum enum_mysql_timestamp_type time_type)
/*
Functions to convert time/date/datetime value to a string,
using default format.
- This functions don't check that given TIME structure members are
+ This functions don't check that given MYSQL_TIME structure members are
in valid range. If they are not, return value won't reflect any
valid date either. Additionally, make_time doesn't take into
account time->day member: it's assumed that days have been converted
@@ -1017,30 +1027,21 @@ void set_zero_time(MYSQL_TIME *tm, enum enum_mysql_timestamp_type time_type)
int my_time_to_str(const MYSQL_TIME *l_time, char *to)
{
uint extra_hours= 0;
- return my_sprintf(to, (to, "%s%02u:%02u:%02u",
- (l_time->neg ? "-" : ""),
- extra_hours+ l_time->hour,
- l_time->minute,
- l_time->second));
+ return sprintf(to, "%s%02u:%02u:%02u", (l_time->neg ? "-" : ""),
+ extra_hours+ l_time->hour, l_time->minute, l_time->second);
}
int my_date_to_str(const MYSQL_TIME *l_time, char *to)
{
- return my_sprintf(to, (to, "%04u-%02u-%02u",
- l_time->year,
- l_time->month,
- l_time->day));
+ return sprintf(to, "%04u-%02u-%02u",
+ l_time->year, l_time->month, l_time->day);
}
int my_datetime_to_str(const MYSQL_TIME *l_time, char *to)
{
- return my_sprintf(to, (to, "%04u-%02u-%02u %02u:%02u:%02u",
- l_time->year,
- l_time->month,
- l_time->day,
- l_time->hour,
- l_time->minute,
- l_time->second));
+ return sprintf(to, "%04u-%02u-%02u %02u:%02u:%02u",
+ l_time->year, l_time->month, l_time->day,
+ l_time->hour, l_time->minute, l_time->second);
}
@@ -1090,7 +1091,7 @@ int my_TIME_to_str(const MYSQL_TIME *l_time, char *to)
DESCRIPTION
Convert a datetime value of formats YYMMDD, YYYYMMDD, YYMMDDHHMSS,
- YYYYMMDDHHMMSS to broken-down TIME representation. Return value in
+ YYYYMMDDHHMMSS to broken-down MYSQL_TIME representation. Return value in
YYYYMMDDHHMMSS format as side-effect.
This function also checks if datetime value fits in DATETIME range.
@@ -1190,7 +1191,7 @@ ulonglong TIME_to_ulonglong_datetime(const MYSQL_TIME *my_time)
}
-/* Convert TIME value to integer in YYYYMMDD format */
+/* Convert MYSQL_TIME value to integer in YYYYMMDD format */
ulonglong TIME_to_ulonglong_date(const MYSQL_TIME *my_time)
{
@@ -1200,7 +1201,7 @@ ulonglong TIME_to_ulonglong_date(const MYSQL_TIME *my_time)
/*
- Convert TIME value to integer in HHMMSS format.
+ Convert MYSQL_TIME value to integer in HHMMSS format.
This function doesn't take into account time->day member:
it's assumed that days have been converted to hours already.
*/
@@ -1214,7 +1215,7 @@ ulonglong TIME_to_ulonglong_time(const MYSQL_TIME *my_time)
/*
- Convert struct TIME (date and time split into year/month/day/hour/...
+ Convert struct MYSQL_TIME (date and time split into year/month/day/hour/...
to a number in format YYYYMMDDHHMMSS (DATETIME),
YYYYMMDD (DATE) or HHMMSS (TIME).
@@ -1228,7 +1229,7 @@ ulonglong TIME_to_ulonglong_time(const MYSQL_TIME *my_time)
SELECT ?+1;
NOTE
- This function doesn't check that given TIME structure members are
+ This function doesn't check that given MYSQL_TIME structure members are
in valid range. If they are not, return value won't reflect any
valid date either.
*/
diff --git a/sql-common/my_user.c b/sql-common/my_user.c
index e23d789e188..3f9bd619b3c 100644
--- a/sql-common/my_user.c
+++ b/sql-common/my_user.c
@@ -1,4 +1,5 @@
-/* Copyright (c) 2006 MySQL AB, 2009 Sun Microsystems, Inc.
+/*
+ Copyright (c) 2006-2008 MySQL AB, 2009 Sun Microsystems, Inc.
Use is subject to license terms.
This program is free software; you can redistribute it and/or modify
@@ -12,11 +13,12 @@
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+*/
#include <my_user.h>
#include <m_string.h>
-
+#include <mysql_com.h>
/*
Parse user value to user name and host name parts.
@@ -32,9 +34,9 @@
host_name_len [OUT] A place to store length of the host name part.
*/
-void parse_user(const char *user_id_str, uint user_id_len,
- char *user_name_str, uint *user_name_len,
- char *host_name_str, uint *host_name_len)
+void parse_user(const char *user_id_str, size_t user_id_len,
+ char *user_name_str, size_t *user_name_len,
+ char *host_name_str, size_t *host_name_len)
{
char *p= strrchr(user_id_str, '@');
@@ -48,6 +50,12 @@ void parse_user(const char *user_id_str, uint user_id_len,
*user_name_len= (uint) (p - user_id_str);
*host_name_len= (uint) (user_id_len - *user_name_len - 1);
+ if (*user_name_len > USERNAME_LENGTH)
+ *user_name_len= USERNAME_LENGTH;
+
+ if (*host_name_len > HOSTNAME_LENGTH)
+ *host_name_len= HOSTNAME_LENGTH;
+
memcpy(user_name_str, user_id_str, *user_name_len);
memcpy(host_name_str, p + 1, *host_name_len);
}
diff --git a/sql-common/pack.c b/sql-common/pack.c
index 33c1103fb04..7ff89471b45 100644
--- a/sql-common/pack.c
+++ b/sql-common/pack.c
@@ -94,30 +94,28 @@ my_ulonglong net_field_length_ll(uchar **packet)
Position in 'pkg' after the packed length
*/
-char *
-net_store_length(char *pkg, ulonglong length)
+uchar *net_store_length(uchar *packet, ulonglong length)
{
- uchar *packet=(uchar*) pkg;
if (length < (ulonglong) LL(251))
{
*packet=(uchar) length;
- return (char*) packet+1;
+ return packet+1;
}
/* 251 is reserved for NULL */
if (length < (ulonglong) LL(65536))
{
*packet++=252;
int2store(packet,(uint) length);
- return (char*) packet+2;
+ return packet+2;
}
if (length < (ulonglong) LL(16777216))
{
*packet++=253;
int3store(packet,(ulong) length);
- return (char*) packet+3;
+ return packet+3;
}
*packet++=254;
int8store(packet,length);
- return (char*) packet+8;
+ return packet+8;
}