Permalink
Browse files

add a proxy for sppintf and vspprintf

  • Loading branch information...
1 parent c90df58 commit f06443593eb832dd1c796c787f079f9aeb3fe691 Andrey Hristov committed Mar 18, 2011
View
@@ -524,11 +524,11 @@ mysqlnd_connect_run_authentication(
{
char * plugin_name = NULL;
- spprintf(&plugin_name, 0, "auth_plugin_%s", requested_protocol);
+ mnd_sprintf(&plugin_name, 0, "auth_plugin_%s", requested_protocol);
DBG_INF_FMT("looking for %s auth plugin", plugin_name);
auth_plugin = mysqlnd_plugin_find(plugin_name);
- efree(plugin_name);
+ mnd_sprintf_free(plugin_name);
if (!auth_plugin) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "The server requested authentication method unknown to the client [%s]", requested_protocol);
@@ -695,22 +695,22 @@ MYSQLND_METHOD(mysqlnd_conn, connect)(MYSQLND * conn,
if (!socket_or_pipe) {
socket_or_pipe = "/tmp/mysql.sock";
}
- transport_len = spprintf(&transport, 0, "unix://%s", socket_or_pipe);
+ transport_len = mnd_sprintf(&transport, 0, "unix://%s", socket_or_pipe);
unix_socket = TRUE;
#else
if (host_len == sizeof(".") - 1 && host[0] == '.') {
/* named pipe in socket */
if (!socket_or_pipe) {
socket_or_pipe = "\\\\.\\pipe\\MySQL";
}
- transport_len = spprintf(&transport, 0, "pipe://%s", socket_or_pipe);
+ transport_len = mnd_sprintf(&transport, 0, "pipe://%s", socket_or_pipe);
named_pipe = TRUE;
#endif
} else {
if (!port) {
port = 3306;
}
- transport_len = spprintf(&transport, 0, "tcp://%s:%u", host, port);
+ transport_len = mnd_sprintf(&transport, 0, "tcp://%s:%u", host, port);
}
if (!transport) {
SET_OOM_ERROR(conn->error_info);
@@ -719,7 +719,7 @@ MYSQLND_METHOD(mysqlnd_conn, connect)(MYSQLND * conn,
DBG_INF_FMT("transport=%s", transport);
conn->scheme = mnd_pestrndup(transport, transport_len, conn->persistent);
conn->scheme_len = transport_len;
- efree(transport); /* allocated by spprintf */
+ mnd_sprintf_free(transport);
transport = NULL;
if (!conn->scheme) {
goto err; /* OOM */
@@ -833,13 +833,13 @@ MYSQLND_METHOD(mysqlnd_conn, connect)(MYSQLND * conn,
conn->host_len = strlen(conn->host);
{
char *p;
- spprintf(&p, 0, "%s via TCP/IP", conn->host);
+ mnd_sprintf(&p, 0, "%s via TCP/IP", conn->host);
if (!p) {
SET_OOM_ERROR(conn->error_info);
goto err; /* OOM */
}
conn->host_info = mnd_pestrdup(p, conn->persistent);
- efree(p); /* allocated by spprintf */
+ mnd_sprintf_free(p);
if (!conn->host_info) {
SET_OOM_ERROR(conn->error_info);
goto err; /* OOM */
@@ -851,13 +851,13 @@ MYSQLND_METHOD(mysqlnd_conn, connect)(MYSQLND * conn,
conn->host_info = mnd_pestrdup("Localhost via UNIX socket", conn->persistent);
} else if (named_pipe) {
char *p;
- spprintf(&p, 0, "%s via named pipe", conn->unix_socket);
+ mnd_sprintf(&p, 0, "%s via named pipe", conn->unix_socket);
if (!p) {
SET_OOM_ERROR(conn->error_info);
goto err; /* OOM */
}
conn->host_info = mnd_pestrdup(p, conn->persistent);
- efree(p); /* allocated by spprintf */
+ mnd_sprintf_free(p);
if (!conn->host_info) {
SET_OOM_ERROR(conn->error_info);
goto err; /* OOM */
@@ -1317,13 +1317,13 @@ MYSQLND_METHOD(mysqlnd_conn, list_method)(MYSQLND * conn, const char * query, co
if (par1) {
if (achtung_wild) {
- show_query_len = spprintf(&show_query, 0, query, par1, achtung_wild);
+ show_query_len = mnd_sprintf(&show_query, 0, query, par1, achtung_wild);
} else {
- show_query_len = spprintf(&show_query, 0, query, par1);
+ show_query_len = mnd_sprintf(&show_query, 0, query, par1);
}
} else {
if (achtung_wild) {
- show_query_len = spprintf(&show_query, 0, query, achtung_wild);
+ show_query_len = mnd_sprintf(&show_query, 0, query, achtung_wild);
} else {
show_query_len = strlen(show_query = (char *)query);
}
@@ -1333,7 +1333,7 @@ MYSQLND_METHOD(mysqlnd_conn, list_method)(MYSQLND * conn, const char * query, co
result = conn->m->store_result(conn TSRMLS_CC);
}
if (show_query != query) {
- efree(show_query); /* allocated by spprintf */
+ mnd_sprintf_free(show_query);
}
DBG_RETURN(result);
}
@@ -1547,7 +1547,7 @@ MYSQLND_METHOD(mysqlnd_conn, set_charset)(MYSQLND * const conn, const char * con
DBG_RETURN(FAIL);
}
- query_len = spprintf(&query, 0, "SET NAMES %s", csname);
+ query_len = mnd_sprintf(&query, 0, "SET NAMES %s", csname);
if (FAIL == conn->m->query(conn, query, query_len TSRMLS_CC)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error executing query");
@@ -1556,7 +1556,7 @@ MYSQLND_METHOD(mysqlnd_conn, set_charset)(MYSQLND * const conn, const char * con
} else {
conn->charset = charset;
}
- efree(query); /* allocated by spprintf */
+ mnd_sprintf_free(query);
DBG_INF(ret == PASS? "PASS":"FAIL");
DBG_RETURN(ret);
@@ -2033,11 +2033,11 @@ MYSQLND_METHOD(mysqlnd_conn, change_user)(MYSQLND * const conn,
{
char * plugin_name = NULL;
- spprintf(&plugin_name, 0, "auth_plugin_%s", requested_protocol);
+ mnd_sprintf(&plugin_name, 0, "auth_plugin_%s", requested_protocol);
DBG_INF_FMT("looking for %s auth plugin", plugin_name);
auth_plugin = mysqlnd_plugin_find(plugin_name);
- efree(plugin_name);
+ mnd_sprintf_free(plugin_name);
if (!auth_plugin) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "The server requested authentication method unknown to the client [%s]", requested_protocol);
@@ -510,6 +510,35 @@ char * _mysqlnd_pestrdup(const char * const ptr, zend_bool persistent MYSQLND_ME
}
/* }}} */
+
+/* {{{ _mysqlnd_sprintf */
+PHPAPI int _mysqlnd_sprintf(char ** pbuf, size_t max_len, const char *format, ...)
+{
+ int len;
+ va_list ap;
+ va_start(ap, format);
+ len = vspprintf(pbuf, max_len, format, ap);
+ va_end(ap);
+ return len;
+}
+/* }}} */
+
+
+/* {{{ _mysqlnd_sprintf_free */
+PHPAPI void _mysqlnd_sprintf_free(char * p)
+{
+ efree(p);
+}
+/* }}} */
+
+
+PHPAPI int _mysqlnd_vsprintf(char ** pbuf, size_t max_len, const char * format, va_list ap)
+{
+ return vspprintf(pbuf, max_len, format, ap);
+}
+/* }}} */
+
+
#define MYSQLND_DEBUG_MEMORY 1
#if MYSQLND_DEBUG_MEMORY == 0
@@ -644,7 +673,10 @@ PHPAPI struct st_mysqlnd_allocator_methods mysqlnd_allocator =
_mysqlnd_realloc,
_mysqlnd_free,
_mysqlnd_pestrndup,
- _mysqlnd_pestrdup
+ _mysqlnd_pestrdup,
+ _mysqlnd_sprintf,
+ _mysqlnd_vsprintf,
+ _mysqlnd_sprintf_free
#else
mysqlnd_zend_mm_emalloc,
mysqlnd_zend_mm_pemalloc,
@@ -660,6 +692,8 @@ PHPAPI struct st_mysqlnd_allocator_methods mysqlnd_allocator =
mysqlnd_zend_mm_free,
mysqlnd_zend_mm_pestrndup,
mysqlnd_zend_mm_pestrdup
+ sprintf,
+ mysqlnd_zend_mm_efree,
#endif
};
@@ -45,6 +45,9 @@ struct st_mysqlnd_allocator_methods
void (*m_free)(void *ptr MYSQLND_MEM_D);
char * (*m_pestrndup)(const char * const ptr, size_t size, zend_bool persistent MYSQLND_MEM_D);
char * (*m_pestrdup)(const char * const ptr, zend_bool persistent MYSQLND_MEM_D);
+ int (*m_sprintf)(char **pbuf, size_t max_len, const char *format, ...);
+ int (*m_vsprintf)(char **pbuf, size_t max_len, const char *format, va_list ap);
+ void (*m_sprintf_free)(char * p);
};
PHPAPI extern struct st_mysqlnd_allocator_methods mysqlnd_allocator;
@@ -63,7 +66,9 @@ PHPAPI void * _mysqlnd_realloc(void *ptr, size_t new_size MYSQLND_MEM_D);
PHPAPI void _mysqlnd_free(void *ptr MYSQLND_MEM_D);
PHPAPI char * _mysqlnd_pestrndup(const char * const ptr, size_t size, zend_bool persistent MYSQLND_MEM_D);
PHPAPI char * _mysqlnd_pestrdup(const char * const ptr, zend_bool persistent MYSQLND_MEM_D);
-
+PHPAPI int _mysqlnd_sprintf(char **pbuf, size_t max_len, const char *format, ...);
+PHPAPI void _mysqlnd_sprintf_free(char * p);
+PHPAPI int _mysqlnd_vsprintf(char **pbuf, size_t max_len, const char *format, va_list ap);
#define mnd_emalloc(size) mysqlnd_allocator.m_emalloc((size) MYSQLND_MEM_C)
#define mnd_pemalloc(size, pers) mysqlnd_allocator.m_pemalloc((size), (pers) MYSQLND_MEM_C)
@@ -79,6 +84,9 @@ PHPAPI char * _mysqlnd_pestrdup(const char * const ptr, zend_bool persistent MYS
#define mnd_free(ptr) mysqlnd_allocator.m_free((ptr) MYSQLND_MEM_C)
#define mnd_pestrndup(ptr, size, pers) mysqlnd_allocator.m_pestrndup((ptr), (size), (pers) MYSQLND_MEM_C)
#define mnd_pestrdup(ptr, pers) mysqlnd_allocator.m_pestrdup((ptr), (pers) MYSQLND_MEM_C)
+#define mnd_sprintf(p, mx_len, fmt,...) mysqlnd_allocator.m_sprintf((p), (mx_len), (fmt), __VA_ARGS__)
+#define mnd_vsprintf(p, mx_len, fmt,ap) mysqlnd_allocator.m_vsprintf((p), (mx_len), (fmt), (ap))
+#define mnd_sprintf_free(p) mysqlnd_allocator.m_sprintf_free((p))
#endif /* MYSQLND_ALLOC_H */
@@ -129,7 +129,7 @@ MYSQLND_METHOD(mysqlnd_debug, log)(MYSQLND_DEBUG * self,
level_buffer[sizeof(level_buffer) - 1 ] = '\0';
}
- message_line_len = spprintf(&message_line, 0, "%s%s%s%s%s%s%s%s\n",
+ message_line_len = mnd_sprintf(&message_line, 0, "%s%s%s%s%s%s%s%s\n",
flags & MYSQLND_DEBUG_DUMP_PID? pid_buffer:"",
flags & MYSQLND_DEBUG_DUMP_TIME? time_buffer:"",
flags & MYSQLND_DEBUG_DUMP_FILE? file_buffer:"",
@@ -138,7 +138,7 @@ MYSQLND_METHOD(mysqlnd_debug, log)(MYSQLND_DEBUG * self,
pipe_buffer, type? type:"", message);
ret = php_stream_write(self->stream, message_line, message_line_len)? PASS:FAIL;
- efree(message_line); /* allocated by spprintf */
+ mnd_sprintf_free(message_line);
if (flags & MYSQLND_DEBUG_FLUSH) {
self->m->close(self);
self->m->open(self, TRUE);
@@ -227,21 +227,20 @@ MYSQLND_METHOD(mysqlnd_debug, log_va)(MYSQLND_DEBUG *self,
level_buffer[sizeof(level_buffer) - 1 ] = '\0';
}
-
va_start(args, format);
- vspprintf(&buffer, 0, format, args);
+ mnd_vsprintf(&buffer, 0, format, args);
va_end(args);
- message_line_len = spprintf(&message_line, 0, "%s%s%s%s%s%s%s%s\n",
+ message_line_len = mnd_sprintf(&message_line, 0, "%s%s%s%s%s%s%s%s\n",
flags & MYSQLND_DEBUG_DUMP_PID? pid_buffer:"",
flags & MYSQLND_DEBUG_DUMP_TIME? time_buffer:"",
flags & MYSQLND_DEBUG_DUMP_FILE? file_buffer:"",
flags & MYSQLND_DEBUG_DUMP_LINE? line_buffer:"",
flags & MYSQLND_DEBUG_DUMP_LEVEL? level_buffer:"",
pipe_buffer, type? type:"", buffer);
- efree(buffer);
+ mnd_sprintf_free(buffer);
ret = php_stream_write(self->stream, message_line, message_line_len)? PASS:FAIL;
- efree(message_line); /* allocated by spprintf */
+ mnd_sprintf_free(message_line);
if (flags & MYSQLND_DEBUG_FLUSH) {
self->m->close(self);
@@ -124,7 +124,7 @@ MYSQLND_METHOD(mysqlnd_net, connect)(MYSQLND_NET * net, const char * const schem
net->stream = php_stream_open_wrapper((char*) scheme + sizeof("pipe://") - 1, "r+", streams_options, NULL);
} else {
if (persistent) {
- hashed_details_len = spprintf(&hashed_details, 0, "%p", net);
+ hashed_details_len = mnd_sprintf(&hashed_details, 0, "%p", net);
DBG_INF_FMT("hashed_details=%s", hashed_details);
}
@@ -140,7 +140,7 @@ MYSQLND_METHOD(mysqlnd_net, connect)(MYSQLND_NET * net, const char * const schem
}
if (*errstr || !net->stream) {
if (hashed_details) {
- efree(hashed_details); /* allocated by spprintf */
+ mnd_sprintf_free(hashed_details);
}
*errcode = CR_CONNECTION_ERROR;
DBG_RETURN(FAIL);
@@ -168,7 +168,7 @@ MYSQLND_METHOD(mysqlnd_net, connect)(MYSQLND_NET * net, const char * const schem
/* Shut-up the streams, they don't know what they are doing */
net->stream->__exposed = 1;
#endif
- efree(hashed_details);
+ mnd_sprintf_free(hashed_details);
}
/*
Streams are not meant for C extensions! Thus we need a hack. Every connected stream will
View
@@ -660,11 +660,11 @@ MYSQLND_METHOD(mysqlnd_stmt, execute)(MYSQLND_STMT * const s TSRMLS_DC)
}
if (not_bound) {
char * msg;
- spprintf(&msg, 0, "No data supplied for %u parameter%s in prepared statement",
- not_bound, not_bound>1 ?"s":"");
+ mnd_sprintf(&msg, 0, "No data supplied for %u parameter%s in prepared statement",
+ not_bound, not_bound>1 ?"s":"");
SET_STMT_ERROR(stmt, CR_PARAMS_NOT_BOUND, UNKNOWN_SQLSTATE, msg);
if (msg) {
- efree(msg); /* allocated by spprintf */
+ mnd_sprintf_free(msg);
}
DBG_INF("FAIL");
DBG_RETURN(FAIL);
@@ -258,18 +258,14 @@ void ps_fetch_time(zval *zv, const MYSQLND_FIELD * const field,
t.time_type = MYSQLND_TIMESTAMP_TIME;
}
- /*
- QQ : How to make this unicode without copying two times the buffer -
- Unicode equivalent of spprintf?
- */
- length = spprintf(&value, 0, "%s%02u:%02u:%02u", (t.neg ? "-" : ""), t.hour, t.minute, t.second);
+ length = mnd_sprintf(&value, 0, "%s%02u:%02u:%02u", (t.neg ? "-" : ""), t.hour, t.minute, t.second);
DBG_INF_FMT("%s", value);
#if MYSQLND_UNICODE
if (!as_unicode) {
#endif
ZVAL_STRINGL(zv, value, length, 1);
- efree(value); /* allocated by spprintf */
+ mnd_sprintf_free(value);
#if MYSQLND_UNICODE
} else {
ZVAL_UTF8_STRINGL(zv, value, length, ZSTR_AUTOFREE);
@@ -309,18 +305,14 @@ void ps_fetch_date(zval *zv, const MYSQLND_FIELD * const field,
t.time_type = MYSQLND_TIMESTAMP_DATE;
}
- /*
- QQ : How to make this unicode without copying two times the buffer -
- Unicode equivalent of spprintf?
- */
- length = spprintf(&value, 0, "%04u-%02u-%02u", t.year, t.month, t.day);
+ length = mnd_sprintf(&value, 0, "%04u-%02u-%02u", t.year, t.month, t.day);
DBG_INF_FMT("%s", value);
#if MYSQLND_UNICODE
if (!as_unicode) {
#endif
ZVAL_STRINGL(zv, value, length, 1);
- efree(value); /* allocated by spprintf */
+ mnd_sprintf_free(value);
#if MYSQLND_UNICODE
} else {
ZVAL_UTF8_STRINGL(zv, value, length, ZSTR_AUTOFREE);
@@ -367,19 +359,14 @@ void ps_fetch_datetime(zval *zv, const MYSQLND_FIELD * const field,
t.time_type = MYSQLND_TIMESTAMP_DATETIME;
}
- /*
- QQ : How to make this unicode without copying two times the buffer -
- Unicode equivalent of spprintf?
- */
- length = spprintf(&value, 0, "%04u-%02u-%02u %02u:%02u:%02u",
- t.year, t.month, t.day, t.hour, t.minute, t.second);
+ length = mnd_sprintf(&value, 0, "%04u-%02u-%02u %02u:%02u:%02u", t.year, t.month, t.day, t.hour, t.minute, t.second);
DBG_INF_FMT("%s", value);
#if MYSQLND_UNICODE
if (!as_unicode) {
#endif
ZVAL_STRINGL(zv, value, length, 1);
- efree(value); /* allocated by spprintf */
+ mnd_sprintf_free(value);
#if MYSQLND_UNICODE
} else {
ZVAL_UTF8_STRINGL(zv, to, length, ZSTR_AUTOFREE);

0 comments on commit f064435

Please sign in to comment.