summaryrefslogtreecommitdiff
path: root/ext/pgsql/pgsql.c
diff options
context:
space:
mode:
Diffstat (limited to 'ext/pgsql/pgsql.c')
-rw-r--r--ext/pgsql/pgsql.c125
1 files changed, 52 insertions, 73 deletions
diff --git a/ext/pgsql/pgsql.c b/ext/pgsql/pgsql.c
index 27462bc336..9f51256ac5 100644
--- a/ext/pgsql/pgsql.c
+++ b/ext/pgsql/pgsql.c
@@ -2,7 +2,7 @@
+----------------------------------------------------------------------+
| PHP Version 7 |
+----------------------------------------------------------------------+
- | Copyright (c) 1997-2018 The PHP Group |
+ | Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
@@ -38,12 +38,6 @@
#ifdef PHP_WIN32
# include "win32/time.h"
#endif
-
-#undef PACKAGE_BUGREPORT
-#undef PACKAGE_NAME
-#undef PACKAGE_STRING
-#undef PACKAGE_TARNAME
-#undef PACKAGE_VERSION
#include "php_pgsql.h"
#include "php_globals.h"
#include "zend_exceptions.h"
@@ -1063,7 +1057,7 @@ static int _php_pgsql_detect_identifier_escape(const char *identifier, size_t le
if (len <= 2) {
return FAILURE;
}
- /* Detect double qoutes */
+ /* Detect double quotes */
if (identifier[0] == '"' && identifier[len-1] == '"') {
size_t i;
@@ -1336,6 +1330,11 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
smart_str_appendl(&str, Z_STRVAL(args[i]), Z_STRLEN(args[i]));
}
+ /* Exception thrown during a string conversion. */
+ if (EG(exception)) {
+ goto cleanup;
+ }
+
smart_str_0(&str);
if (ZEND_NUM_ARGS() == 1) { /* new style, using connection string */
@@ -2817,7 +2816,7 @@ static void php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, zend_long result_
zval retval;
ZVAL_COPY_VALUE(&dataset, return_value);
- object_and_properties_init(return_value, ce, NULL);
+ object_init_ex(return_value, ce);
if (!ce->default_properties_count && !ce->__set) {
Z_OBJ_P(return_value)->properties = Z_ARR(dataset);
} else {
@@ -3051,7 +3050,6 @@ static void php_pgsql_data_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type)
switch (Z_TYPE_P(field)) {
case IS_STRING:
- convert_to_string_ex(field);
field_offset = PQfnumber(pgsql_result, Z_STRVAL_P(field));
if (field_offset < 0 || field_offset >= PQnfields(pgsql_result)) {
php_error_docref(NULL, E_WARNING, "Bad column offset specified");
@@ -4286,22 +4284,23 @@ PHP_FUNCTION(pg_copy_from)
PQclear(pgsql_result);
#if HAVE_PQPUTCOPYDATA
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(pg_rows), value) {
- zval tmp;
- ZVAL_COPY(&tmp, value);
- convert_to_string_ex(&tmp);
- query = (char *)emalloc(Z_STRLEN(tmp) + 2);
- strlcpy(query, Z_STRVAL(tmp), Z_STRLEN(tmp) + 2);
- if(Z_STRLEN(tmp) > 0 && *(query + Z_STRLEN(tmp) - 1) != '\n') {
- strlcat(query, "\n", Z_STRLEN(tmp) + 2);
+ zend_string *tmp = zval_try_get_string(value);
+ if (UNEXPECTED(!tmp)) {
+ return;
+ }
+ query = (char *)emalloc(ZSTR_LEN(tmp) + 2);
+ strlcpy(query, ZSTR_VAL(tmp), ZSTR_LEN(tmp) + 2);
+ if (ZSTR_LEN(tmp) > 0 && *(query + ZSTR_LEN(tmp) - 1) != '\n') {
+ strlcat(query, "\n", ZSTR_LEN(tmp) + 2);
}
if (PQputCopyData(pgsql, query, (int)strlen(query)) != 1) {
efree(query);
- zval_ptr_dtor_str(&tmp);
+ zend_string_release(tmp);
PHP_PQ_ERROR("copy failed: %s", pgsql);
RETURN_FALSE;
}
efree(query);
- zval_ptr_dtor_str(&tmp);
+ zend_string_release(tmp);
} ZEND_HASH_FOREACH_END();
if (PQputCopyEnd(pgsql, NULL) != 1) {
@@ -4310,22 +4309,23 @@ PHP_FUNCTION(pg_copy_from)
}
#else
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(pg_rows), value) {
- zval tmp;
- ZVAL_COPY(&tmp, value);
- convert_to_string_ex(&tmp);
- query = (char *)emalloc(Z_STRLEN(tmp) + 2);
- strlcpy(query, Z_STRVAL(tmp), Z_STRLEN(tmp) + 2);
- if(Z_STRLEN(tmp) > 0 && *(query + Z_STRLEN(tmp) - 1) != '\n') {
- strlcat(query, "\n", Z_STRLEN(tmp) + 2);
+ zend_string *tmp = zval_try_get_string(value);
+ if (UNEXPECTED(!tmp)) {
+ return;
+ }
+ query = (char *)emalloc(ZSTR_LEN(tmp) + 2);
+ strlcpy(query, ZSTR_LVAL(tmp), ZSTR_LEN(tmp) + 2);
+ if (ZSTR_LEN(tmp) > 0 && *(query + ZSTR_LEN(tmp) - 1) != '\n') {
+ strlcat(query, "\n", ZSTR_LEN(tmp) + 2);
}
if (PQputline(pgsql, query)==EOF) {
efree(query);
- zval_ptr_dtor_str(&tmp);
+ zend_string_release(tmp);
PHP_PQ_ERROR("copy failed: %s", pgsql);
RETURN_FALSE;
}
efree(query);
- zval_ptr_dtor_str(&tmp);
+ zend_string_release(tmp);
} ZEND_HASH_FOREACH_END();
if (PQputline(pgsql, "\\.\n") == EOF) {
@@ -5173,17 +5173,13 @@ PHP_FUNCTION(pg_send_execute)
if (Z_TYPE_P(tmp) == IS_NULL) {
params[i] = NULL;
} else {
- zval tmp_val;
- ZVAL_COPY(&tmp_val, tmp);
- convert_to_string(&tmp_val);
- if (Z_TYPE(tmp_val) != IS_STRING) {
- php_error_docref(NULL, E_WARNING,"Error converting parameter");
- zval_ptr_dtor(&tmp_val);
+ zend_string *tmp_str = zval_try_get_string(tmp);
+ if (UNEXPECTED(!tmp)) {
_php_pgsql_free_params(params, num_params);
- RETURN_FALSE;
+ return;
}
- params[i] = estrndup(Z_STRVAL(tmp_val), Z_STRLEN(tmp_val));
- zval_ptr_dtor(&tmp_val);
+ params[i] = estrndup(ZSTR_VAL(tmp_str), ZSTR_LEN(tmp_str));
+ zend_string_release(tmp_str);
}
i++;
@@ -5376,15 +5372,15 @@ PHP_FUNCTION(pg_get_pid)
}
/* }}} */
-static size_t php_pgsql_fd_write(php_stream *stream, const char *buf, size_t count) /* {{{ */
+static ssize_t php_pgsql_fd_write(php_stream *stream, const char *buf, size_t count) /* {{{ */
{
- return 0;
+ return -1;
}
/* }}} */
-static size_t php_pgsql_fd_read(php_stream *stream, char *buf, size_t count) /* {{{ */
+static ssize_t php_pgsql_fd_read(php_stream *stream, char *buf, size_t count) /* {{{ */
{
- return 0;
+ return -1;
}
/* }}} */
@@ -6109,8 +6105,7 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
break;
case IS_LONG:
- ZVAL_LONG(&new_val, Z_LVAL_P(val));
- convert_to_string_ex(&new_val);
+ ZVAL_STR(&new_val, zend_long_to_str(Z_LVAL_P(val)));
break;
case IS_DOUBLE:
@@ -6418,8 +6413,7 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
break;
case IS_LONG:
- ZVAL_LONG(&new_val, Z_LVAL_P(val));
- convert_to_string_ex(&new_val);
+ ZVAL_STR(&new_val, zend_long_to_str(Z_LVAL_P(val)));
break;
case IS_DOUBLE:
@@ -6570,38 +6564,32 @@ static int do_exec(smart_str *querystr, ExecStatusType expect, PGconn *pg_link,
static inline void build_tablename(smart_str *querystr, PGconn *pg_link, const char *table) /* {{{ */
{
- char *table_copy, *escaped, *tmp;
- const char *token;
- size_t len;
+ size_t table_len = strlen(table);
- /* schame.table should be "schame"."table" */
- table_copy = estrdup(table);
- token = php_strtok_r(table_copy, ".", &tmp);
- if (token == NULL) {
- token = table;
- }
- len = strlen(token);
- if (_php_pgsql_detect_identifier_escape(token, len) == SUCCESS) {
- smart_str_appendl(querystr, token, len);
+ /* schema.table should be "schema"."table" */
+ const char *dot = memchr(table, '.', table_len);
+ size_t len = dot ? dot - table : table_len;
+ if (_php_pgsql_detect_identifier_escape(table, len) == SUCCESS) {
+ smart_str_appendl(querystr, table, len);
} else {
- escaped = PGSQLescapeIdentifier(pg_link, token, len);
+ char *escaped = PGSQLescapeIdentifier(pg_link, table, len);
smart_str_appends(querystr, escaped);
PGSQLfree(escaped);
}
- if (tmp && *tmp) {
- len = strlen(tmp);
+ if (dot) {
+ const char *after_dot = dot + 1;
+ len = table_len - len - 1;
/* "schema"."table" format */
- if (_php_pgsql_detect_identifier_escape(tmp, len) == SUCCESS) {
+ if (_php_pgsql_detect_identifier_escape(after_dot, len) == SUCCESS) {
smart_str_appendc(querystr, '.');
- smart_str_appendl(querystr, tmp, len);
+ smart_str_appendl(querystr, after_dot, len);
} else {
- escaped = PGSQLescapeIdentifier(pg_link, tmp, len);
+ char *escaped = PGSQLescapeIdentifier(pg_link, after_dot, len);
smart_str_appendc(querystr, '.');
smart_str_appends(querystr, escaped);
PGSQLfree(escaped);
}
}
- efree(table_copy);
}
/* }}} */
@@ -7214,12 +7202,3 @@ PHP_FUNCTION(pg_select)
/* }}} */
#endif
-
-/*
- * Local variables:
- * tab-width: 4
- * c-basic-offset: 4
- * End:
- * vim600: sw=4 ts=4 fdm=marker
- * vim<600: sw=4 ts=4
- */