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.c297
1 files changed, 157 insertions, 140 deletions
diff --git a/ext/pgsql/pgsql.c b/ext/pgsql/pgsql.c
index 1f88bf4205..d22b6dfc59 100644
--- a/ext/pgsql/pgsql.c
+++ b/ext/pgsql/pgsql.c
@@ -594,7 +594,7 @@ ZEND_END_ARG_INFO()
/* {{{ pgsql_functions[]
*/
-const zend_function_entry pgsql_functions[] = {
+static const zend_function_entry pgsql_functions[] = {
/* connection functions */
PHP_FE(pg_connect, arginfo_pg_connect)
PHP_FE(pg_pconnect, arginfo_pg_pconnect)
@@ -811,7 +811,6 @@ static int le_link, le_plink, le_result, le_lofp, le_string;
static char *php_pgsql_PQescapeInternal(PGconn *conn, const char *str, size_t len, int escape_literal, int safe) /* {{{ */
{
char *result, *rp, *s;
- size_t tmp_len;
if (!conn) {
return NULL;
@@ -821,9 +820,8 @@ static char *php_pgsql_PQescapeInternal(PGconn *conn, const char *str, size_t le
rp = result = (char *)safe_emalloc(len, 2, 5); /* leading " E" needs extra 2 bytes + quote_chars on both end for 2 bytes + NULL */
if (escape_literal) {
- size_t new_len;
-
if (safe) {
+ size_t new_len;
char *tmp = (char *)safe_emalloc(len, 2, 1);
*rp++ = '\'';
/* PQescapeString does not escape \, but it handles multibyte chars safely.
@@ -834,6 +832,7 @@ static char *php_pgsql_PQescapeInternal(PGconn *conn, const char *str, size_t le
rp += new_len;
} else {
char *encoding;
+ size_t tmp_len;
/* This is compatible with PQescapeLiteral, but it cannot handle multbyte chars
such as SJIS, BIG5. Raise warning and return NULL by checking
client_encoding. */
@@ -923,7 +922,7 @@ static inline char * _php_pgsql_trim_result(PGconn * pgsql, char **buf)
*/
static void php_pgsql_set_default_link(zend_resource *res)
{
- GC_REFCOUNT(res)++;
+ GC_ADDREF(res);
if (PGG(default_link) != NULL) {
zend_list_delete(PGG(default_link));
@@ -996,7 +995,6 @@ static int _rollback_transactions(zval *el)
{
PGconn *link;
PGresult *res;
- int orig;
zend_resource *rsrc = Z_RES_P(el);
if (rsrc->type != le_plink)
@@ -1016,7 +1014,7 @@ static int _rollback_transactions(zval *el)
if ((PQprotocolVersion(link) >= 3 && PQtransactionStatus(link) != PQTRANS_IDLE) || PQprotocolVersion(link) < 3)
#endif
{
- orig = PGG(ignore_notices);
+ int orig = PGG(ignore_notices);
PGG(ignore_notices) = 1;
#if HAVE_PGTRANSACTIONSTATUS && HAVE_PQPROTOCOLVERSION
res = PQexec(link,"ROLLBACK;");
@@ -1055,14 +1053,14 @@ static void _free_result(zend_resource *rsrc)
static int _php_pgsql_detect_identifier_escape(const char *identifier, size_t len) /* {{{ */
{
- size_t i;
-
/* Handle edge case. Cannot be a escaped string */
if (len <= 2) {
return FAILURE;
}
/* Detect double qoutes */
if (identifier[0] == '"' && identifier[len-1] == '"') {
+ size_t i;
+
/* Detect wrong format of " inside of escaped string */
for (i = 1; i < len-1; i++) {
if (identifier[i] == '"' && (identifier[++i] != '"' || i == len-1)) {
@@ -1194,6 +1192,24 @@ PHP_MINIT_FUNCTION(pgsql)
REGISTER_LONG_CONSTANT("PGSQL_DIAG_SOURCE_FILE", PG_DIAG_SOURCE_FILE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PGSQL_DIAG_SOURCE_LINE", PG_DIAG_SOURCE_LINE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PGSQL_DIAG_SOURCE_FUNCTION", PG_DIAG_SOURCE_FUNCTION, CONST_CS | CONST_PERSISTENT);
+#ifdef PG_DIAG_SCHEMA_NAME
+ REGISTER_LONG_CONSTANT("PGSQL_DIAG_SCHEMA_NAME", PG_DIAG_SCHEMA_NAME, CONST_CS | CONST_PERSISTENT);
+#endif
+#ifdef PG_DIAG_TABLE_NAME
+ REGISTER_LONG_CONSTANT("PGSQL_DIAG_TABLE_NAME", PG_DIAG_TABLE_NAME, CONST_CS | CONST_PERSISTENT);
+#endif
+#ifdef PG_DIAG_COLUMN_NAME
+ REGISTER_LONG_CONSTANT("PGSQL_DIAG_COLUMN_NAME", PG_DIAG_COLUMN_NAME, CONST_CS | CONST_PERSISTENT);
+#endif
+#ifdef PG_DIAG_DATATYPE_NAME
+ REGISTER_LONG_CONSTANT("PGSQL_DIAG_DATATYPE_NAME", PG_DIAG_DATATYPE_NAME, CONST_CS | CONST_PERSISTENT);
+#endif
+#ifdef PG_DIAG_CONSTRAINT_NAME
+ REGISTER_LONG_CONSTANT("PGSQL_DIAG_CONSTRAINT_NAME", PG_DIAG_CONSTRAINT_NAME, CONST_CS | CONST_PERSISTENT);
+#endif
+#ifdef PG_DIAG_SEVERITY_NONLOCALIZED
+ REGISTER_LONG_CONSTANT("PGSQL_DIAG_SEVERITY_NONLOCALIZED", PG_DIAG_SEVERITY_NONLOCALIZED, CONST_CS | CONST_PERSISTENT);
+#endif
#endif
/* pg_convert options */
REGISTER_LONG_CONSTANT("PGSQL_CONV_IGNORE_DEFAULT", PGSQL_CONV_IGNORE_DEFAULT, CONST_CS | CONST_PERSISTENT);
@@ -1317,8 +1333,7 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
connstring = Z_STRVAL(args[0]);
} else if (ZEND_NUM_ARGS() == 2 ) { /* Safe to add conntype_option, since 2 args was illegal */
connstring = Z_STRVAL(args[0]);
- convert_to_long_ex(&args[1]);
- connect_type = (int)Z_LVAL(args[1]);
+ connect_type = (int)zval_get_long(&args[1]);
} else {
host = Z_STRVAL(args[0]);
port = Z_STRVAL(args[1]);
@@ -1339,8 +1354,6 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
/* try to find if we already have this link in our persistent list */
if ((le = zend_hash_find_ptr(&EG(persistent_list), str.s)) == NULL) { /* we don't */
- zend_resource new_le;
-
if (PGG(max_links) != -1 && PGG(num_links) >= PGG(max_links)) {
php_error_docref(NULL, E_WARNING,
"Cannot create new link. Too many open links (" ZEND_LONG_FMT ")", PGG(num_links));
@@ -1367,9 +1380,7 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
}
/* hash it up */
- new_le.type = le_plink;
- new_le.ptr = pgsql;
- if (zend_hash_str_update_mem(&EG(persistent_list), ZSTR_VAL(str.s), ZSTR_LEN(str.s), &new_le, sizeof(zend_resource)) == NULL) {
+ if (zend_register_persistent_resource(ZSTR_VAL(str.s), ZSTR_LEN(str.s), pgsql, le_plink) == NULL) {
goto err;
}
PGG(num_links)++;
@@ -1433,7 +1444,7 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
link = (zend_resource *)index_ptr->ptr;
if (link->ptr && (link->type == le_link || link->type == le_plink)) {
php_pgsql_set_default_link(link);
- GC_REFCOUNT(link)++;
+ GC_ADDREF(link);
RETVAL_RES(link);
goto cleanup;
} else {
@@ -1481,9 +1492,7 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
/* add it to the hash */
new_index_ptr.ptr = (void *) Z_RES_P(return_value);
new_index_ptr.type = le_index_ptr;
- if (zend_hash_update_mem(&EG(regular_list), str.s, (void *) &new_index_ptr, sizeof(zend_resource)) == NULL) {
- goto err;
- }
+ zend_hash_update_mem(&EG(regular_list), str.s, (void *) &new_index_ptr, sizeof(zend_resource));
PGG(num_links)++;
}
/* set notice processor */
@@ -1494,7 +1503,7 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
cleanup:
for (i = 0; i < ZEND_NUM_ARGS(); i++) {
- zval_dtor(&args[i]);
+ zval_ptr_dtor(&args[i]);
}
efree(args);
smart_str_free(&str);
@@ -1502,7 +1511,7 @@ cleanup:
err:
for (i = 0; i < ZEND_NUM_ARGS(); i++) {
- zval_dtor(&args[i]);
+ zval_ptr_dtor(&args[i]);
}
efree(args);
smart_str_free(&str);
@@ -1817,7 +1826,6 @@ PHP_FUNCTION(pg_query)
PGconn *pgsql;
PGresult *pgsql_result;
ExecStatusType status;
- pgsql_result_handle *pg_result;
if (argc == 1) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &query, &query_len) == FAILURE) {
@@ -1872,7 +1880,7 @@ PHP_FUNCTION(pg_query)
case PGRES_COMMAND_OK: /* successful command that did not return rows */
default:
if (pgsql_result) {
- pg_result = (pgsql_result_handle *) emalloc(sizeof(pgsql_result_handle));
+ pgsql_result_handle *pg_result = (pgsql_result_handle *) emalloc(sizeof(pgsql_result_handle));
pg_result->conn = pgsql;
pg_result->result = pgsql_result;
pg_result->row = 0;
@@ -2126,13 +2134,13 @@ PHP_FUNCTION(pg_execute)
pgsql_result_handle *pg_result;
if (argc == 2) {
- if (zend_parse_parameters(argc, "sa/", &stmtname, &stmtname_len, &pv_param_arr)==FAILURE) {
+ if (zend_parse_parameters(argc, "sa", &stmtname, &stmtname_len, &pv_param_arr)==FAILURE) {
return;
}
link = FETCH_DEFAULT_LINK();
CHECK_DEFAULT_LINK(link);
} else {
- if (zend_parse_parameters(argc, "rsa/", &pgsql_link, &stmtname, &stmtname_len, &pv_param_arr) == FAILURE) {
+ if (zend_parse_parameters(argc, "rsa", &pgsql_link, &stmtname, &stmtname_len, &pv_param_arr) == FAILURE) {
return;
}
link = Z_RES_P(pgsql_link);
@@ -2164,18 +2172,11 @@ PHP_FUNCTION(pg_execute)
if (Z_TYPE_P(tmp) == IS_NULL) {
params[i] = NULL;
} else {
- zval tmp_val;
+ zend_string *tmp_str;
+ zend_string *str = zval_get_tmp_string(tmp, &tmp_str);
- 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);
- _php_pgsql_free_params(params, num_params);
- RETURN_FALSE;
- }
- params[i] = estrndup(Z_STRVAL(tmp_val), Z_STRLEN(tmp_val));
- zval_ptr_dtor(&tmp_val);
+ params[i] = estrndup(ZSTR_VAL(str), ZSTR_LEN(str));
+ zend_tmp_string_release(tmp_str);
}
i++;
@@ -2295,7 +2296,7 @@ PHP_FUNCTION(pg_affected_rows)
/* }}} */
#endif
-/* {{{ proto mixed pg_last_notice(resource connection [, long option])
+/* {{{ proto mixed pg_last_notice(resource connection [, int option])
Returns the last notice set by the backend */
PHP_FUNCTION(pg_last_notice)
{
@@ -2352,7 +2353,6 @@ PHP_FUNCTION(pg_last_notice)
*/
static char *get_field_name(PGconn *pgsql, Oid oid, HashTable *list)
{
- PGresult *result;
smart_str str = {0};
zend_resource *field_type;
char *ret=NULL;
@@ -2369,6 +2369,7 @@ static char *get_field_name(PGconn *pgsql, Oid oid, HashTable *list)
int oid_offset,name_offset;
char *tmp_oid, *end_ptr, *tmp_name;
zend_resource new_oid_entry;
+ PGresult *result;
if ((result = PQexec(pgsql, "select oid,typname from pg_type")) == NULL || PQresultStatus(result) != PGRES_TUPLES_OK) {
if (result) {
@@ -2828,9 +2829,7 @@ static void php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, zend_long result_
}
}
- fcc.initialized = 1;
fcc.function_handler = ce->constructor;
- fcc.calling_scope = zend_get_executed_scope();
fcc.called_scope = Z_OBJCE_P(return_value);
fcc.object = Z_OBJ_P(return_value);
@@ -2912,7 +2911,7 @@ PHP_FUNCTION(pg_fetch_all)
pgsql_result = pg_result->result;
array_init(return_value);
if (php_pgsql_result2array(pgsql_result, return_value, result_type) == FAILURE) {
- zval_dtor(return_value);
+ zend_array_destroy(Z_ARR_P(return_value));
RETURN_FALSE;
}
}
@@ -3531,7 +3530,7 @@ PHP_FUNCTION(pg_lo_read)
buf = zend_string_alloc(buf_len, 0);
if ((nbytes = lo_read((PGconn *)pgsql->conn, pgsql->lofd, ZSTR_VAL(buf), ZSTR_LEN(buf)))<0) {
- zend_string_free(buf);
+ zend_string_efree(buf);
RETURN_FALSE;
}
@@ -3559,7 +3558,7 @@ PHP_FUNCTION(pg_lo_write)
if (argc > 2) {
if (z_len > (zend_long)str_len) {
- php_error_docref(NULL, E_WARNING, "Cannot write more than buffer size %d. Tried to write " ZEND_LONG_FMT, str_len, z_len);
+ php_error_docref(NULL, E_WARNING, "Cannot write more than buffer size %zu. Tried to write " ZEND_LONG_FMT, str_len, z_len);
RETURN_FALSE;
}
if (z_len < 0) {
@@ -4086,12 +4085,7 @@ PHP_FUNCTION(pg_copy_to)
PGconn *pgsql;
PGresult *pgsql_result;
ExecStatusType status;
- int copydone = 0;
-#if !HAVE_PQGETCOPYDATA
- char copybuf[COPYBUFSIZ];
-#endif
char *csv = (char *)NULL;
- int ret;
int argc = ZEND_NUM_ARGS();
if (zend_parse_parameters(argc, "rs|ss",
@@ -4132,12 +4126,17 @@ PHP_FUNCTION(pg_copy_to)
switch (status) {
case PGRES_COPY_OUT:
if (pgsql_result) {
+ int copydone = 0;
+#if !HAVE_PQGETCOPYDATA
+ char copybuf[COPYBUFSIZ];
+#endif
+
PQclear(pgsql_result);
array_init(return_value);
#if HAVE_PQGETCOPYDATA
while (!copydone)
{
- ret = PQgetCopyData(pgsql, &csv, 0);
+ int ret = PQgetCopyData(pgsql, &csv, 0);
switch (ret) {
case -1:
copydone = 1;
@@ -4279,12 +4278,12 @@ PHP_FUNCTION(pg_copy_from)
}
if (PQputCopyData(pgsql, query, (int)strlen(query)) != 1) {
efree(query);
- zval_dtor(&tmp);
+ zval_ptr_dtor_str(&tmp);
PHP_PQ_ERROR("copy failed: %s", pgsql);
RETURN_FALSE;
}
efree(query);
- zval_dtor(&tmp);
+ zval_ptr_dtor_str(&tmp);
} ZEND_HASH_FOREACH_END();
if (PQputCopyEnd(pgsql, NULL) != 1) {
@@ -4303,12 +4302,12 @@ PHP_FUNCTION(pg_copy_from)
}
if (PQputline(pgsql, query)==EOF) {
efree(query);
- zval_dtor(&tmp);
+ zval_ptr_dtor_str(&tmp);
PHP_PQ_ERROR("copy failed: %s", pgsql);
RETURN_FALSE;
}
efree(query);
- zval_dtor(&tmp);
+ zval_ptr_dtor_str(&tmp);
} ZEND_HASH_FOREACH_END();
if (PQputline(pgsql, "\\.\n") == EOF) {
@@ -4955,7 +4954,7 @@ PHP_FUNCTION(pg_send_query_params)
int is_non_blocking;
int ret;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "rsa/", &pgsql_link, &query, &query_len, &pv_param_arr) == FAILURE) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "rsa", &pgsql_link, &query, &query_len, &pv_param_arr) == FAILURE) {
return;
}
@@ -4985,17 +4984,11 @@ PHP_FUNCTION(pg_send_query_params)
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);
- _php_pgsql_free_params(params, num_params);
- RETURN_FALSE;
- }
- params[i] = estrndup(Z_STRVAL(tmp_val), Z_STRLEN(tmp_val));
- zval_ptr_dtor(&tmp_val);
+ zend_string *tmp_str;
+ zend_string *str = zval_get_tmp_string(tmp, &tmp_str);
+
+ params[i] = estrndup(ZSTR_VAL(str), ZSTR_LEN(str));
+ zend_tmp_string_release(tmp_str);
}
i++;
@@ -5251,7 +5244,7 @@ PHP_FUNCTION(pg_get_result)
}
/* }}} */
-/* {{{ proto mixed pg_result_status(resource result[, long result_type])
+/* {{{ proto mixed pg_result_status(resource result[, int result_type])
Get status of query result */
PHP_FUNCTION(pg_result_status)
{
@@ -5404,14 +5397,13 @@ static int php_pgsql_fd_set_option(php_stream *stream, int option, int value, vo
static int php_pgsql_fd_cast(php_stream *stream, int cast_as, void **ret) /* {{{ */
{
PGconn *pgsql = (PGconn *) stream->abstract;
- int fd_number;
switch (cast_as) {
case PHP_STREAM_AS_FD_FOR_SELECT:
case PHP_STREAM_AS_FD:
case PHP_STREAM_AS_SOCKETD:
if (ret) {
- fd_number = PQsocket(pgsql);
+ int fd_number = PQsocket(pgsql);
if (fd_number == -1) {
return FAILURE;
}
@@ -5644,7 +5636,7 @@ PHP_FUNCTION(pg_meta_data)
array_init(return_value);
if (php_pgsql_meta_data(pgsql, table_name, return_value, extended) == FAILURE) {
- zval_dtor(return_value); /* destroy array */
+ zend_array_destroy(Z_ARR_P(return_value)); /* destroy array */
RETURN_FALSE;
}
}
@@ -5743,13 +5735,14 @@ static php_pgsql_data_type php_pgsql_get_data_type(const char *type_name, size_t
/* {{{ php_pgsql_convert_match
* test field value with regular expression specified.
*/
-static int php_pgsql_convert_match(const char *str, size_t str_len, const char *regex , int icase)
+static int php_pgsql_convert_match(const char *str, size_t str_len, const char *regex , size_t regex_len, int icase)
{
- pcre *re;
- const char *err_msg;
- int err_offset;
- int options = PCRE_NO_AUTO_CAPTURE, res;
+ pcre2_code *re;
+ PCRE2_SIZE err_offset;
+ int res, errnumber;
+ uint32_t options = PCRE2_NO_AUTO_CAPTURE;
size_t i;
+ pcre2_match_data *match_data;
/* Check invalid chars for POSIX regex */
for (i = 0; i < str_len; i++) {
@@ -5761,20 +5754,30 @@ static int php_pgsql_convert_match(const char *str, size_t str_len, const char *
}
if (icase) {
- options |= PCRE_CASELESS;
+ options |= PCRE2_CASELESS;
}
- if ((re = pcre_compile(regex, options, &err_msg, &err_offset, NULL)) == NULL) {
- php_error_docref(NULL, E_WARNING, "Cannot compile regex");
+ re = pcre2_compile((PCRE2_SPTR)regex, regex_len, options, &errnumber, &err_offset, php_pcre_cctx());
+ if (NULL == re) {
+ PCRE2_UCHAR err_msg[128];
+ pcre2_get_error_message(errnumber, err_msg, sizeof(err_msg));
+ php_error_docref(NULL, E_WARNING, "Cannot compile regex: '%s'", err_msg);
return FAILURE;
}
- res = pcre_exec(re, NULL, str, str_len, 0, 0, NULL, 0);
- pcre_free(re);
+ match_data = php_pcre_create_match_data(0, re);
+ if (NULL == match_data) {
+ pcre2_code_free(re);
+ php_error_docref(NULL, E_WARNING, "Cannot allocate match data");
+ return FAILURE;
+ }
+ res = pcre2_match(re, (PCRE2_SPTR)str, str_len, 0, 0, match_data, php_pcre_mctx());
+ php_pcre_free_match_data(match_data);
+ pcre2_code_free(re);
- if (res == PCRE_ERROR_NOMATCH) {
+ if (res == PCRE2_ERROR_NOMATCH) {
return FAILURE;
- } else if (res) {
+ } else if (res < 0) {
php_error_docref(NULL, E_WARNING, "Cannot exec regex");
return FAILURE;
}
@@ -5823,7 +5826,7 @@ static int php_pgsql_add_quotes(zval *src, zend_bool should_free)
}
/* {{{ php_pgsql_convert
- * check and convert array values (fieldname=>vlaue pair) for sql
+ * check and convert array values (fieldname=>value pair) for sql
*/
PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, const zval *values, zval *result, zend_ulong opt)
{
@@ -5964,12 +5967,14 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
}
else {
/* FIXME: better regex must be used */
- if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), "^([+-]{0,1}[0-9]+)$", 0) == FAILURE) {
+#define REGEX0 "^([+-]{0,1}[0-9]+)$"
+ if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), REGEX0, sizeof(REGEX0)-1, 0) == FAILURE) {
err = 1;
}
else {
ZVAL_STRINGL(&new_val, Z_STRVAL_P(val), Z_STRLEN_P(val));
}
+#undef REGEX0
}
break;
@@ -6005,9 +6010,11 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
ZVAL_STRINGL(&new_val, "NULL", sizeof("NULL")-1);
}
else {
+#define REGEX0 "^[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?$"
+#define REGEX1 "^[+-]{0,1}(inf)(inity){0,1}$"
/* better regex? */
- if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), "^[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?$", 0) == FAILURE) {
- if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), "^[+-]{0,1}(inf)(inity){0,1}$", 1) == FAILURE) {
+ if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), REGEX0, sizeof(REGEX0)-1, 0) == FAILURE) {
+ if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), REGEX1, sizeof(REGEX1)-1, 1) == FAILURE) {
err = 1;
} else {
ZVAL_STRING(&new_val, Z_STRVAL_P(val));
@@ -6017,6 +6024,8 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
else {
ZVAL_STRING(&new_val, Z_STRVAL_P(val));
}
+#undef REGEX0
+#undef REGEX1
}
break;
@@ -6114,7 +6123,7 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
}
else {
/* better regex? */
- if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), "^[0-9]+$", 0) == FAILURE) {
+ if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), "^[0-9]+$", sizeof("^[0-9]+$")-1, 0) == FAILURE) {
err = 1;
}
else {
@@ -6154,17 +6163,21 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
ZVAL_STRINGL(&new_val, "NULL", sizeof("NULL")-1);
}
else {
+#define REGEX0 "^((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])(\\/[0-9]{1,3})?$"
+#define REGEX1 "^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))(\\/[0-9]{1,3})?$"
/* The inet type holds an IPv4 or IPv6 host address, and optionally its subnet, all in one field. See more in the doc.
The regex might still be not perfect, but catches the most of IP variants. We might decide to remove the regex
at all though and let the server side to handle it.*/
- if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), "^((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])(\\/[0-9]{1,3})?$", 0) == FAILURE
- && php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), "^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))(\\/[0-9]{1,3})?$", 0) == FAILURE) {
+ if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), REGEX0, sizeof(REGEX0)-1, 0) == FAILURE
+ && php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), REGEX1, sizeof(REGEX1)-1, 0) == FAILURE) {
err = 1;
}
else {
ZVAL_STRINGL(&new_val, Z_STRVAL_P(val), Z_STRLEN_P(val));
php_pgsql_add_quotes(&new_val, 1);
}
+#undef REGEX0
+#undef REGEX1
}
break;
@@ -6191,13 +6204,15 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
} else if (!strcasecmp(Z_STRVAL_P(val), "now()")) {
ZVAL_STRINGL(&new_val, "NOW()", sizeof("NOW()")-1);
} else {
+#define REGEX0 "^([0-9]{4}[/-][0-9]{1,2}[/-][0-9]{1,2})(([ \\t]+|T)(([0-9]{1,2}:[0-9]{1,2}){1}(:[0-9]{1,2}){0,1}(\\.[0-9]+){0,1}([ \\t]*([+-][0-9]{1,4}(:[0-9]{1,2}){0,1}|[-a-zA-Z_/+]{1,50})){0,1})){0,1}$"
/* better regex? */
- if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), "^([0-9]{4}[/-][0-9]{1,2}[/-][0-9]{1,2})(([ \\t]+|T)(([0-9]{1,2}:[0-9]{1,2}){1}(:[0-9]{1,2}){0,1}(\\.[0-9]+){0,1}([ \\t]*([+-][0-9]{1,4}(:[0-9]{1,2}){0,1}|[-a-zA-Z_/+]{1,50})){0,1})){0,1}$", 1) == FAILURE) {
+ if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), REGEX0, sizeof(REGEX0)-1, 1) == FAILURE) {
err = 1;
} else {
ZVAL_STRING(&new_val, Z_STRVAL_P(val));
php_pgsql_add_quotes(&new_val, 1);
}
+#undef REGEX0
}
break;
@@ -6221,14 +6236,16 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
ZVAL_STRINGL(&new_val, "NULL", sizeof("NULL")-1);
}
else {
+#define REGEX0 "^([0-9]{4}[/-][0-9]{1,2}[/-][0-9]{1,2})$"
/* FIXME: better regex must be used */
- if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), "^([0-9]{4}[/-][0-9]{1,2}[/-][0-9]{1,2})$", 1) == FAILURE) {
+ if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), REGEX0, sizeof(REGEX0)-1, 1) == FAILURE) {
err = 1;
}
else {
ZVAL_STRINGL(&new_val, Z_STRVAL_P(val), Z_STRLEN_P(val));
php_pgsql_add_quotes(&new_val, 1);
}
+#undef REGEX0
}
break;
@@ -6252,14 +6269,16 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
ZVAL_STRINGL(&new_val, "NULL", sizeof("NULL")-1);
}
else {
+#define REGEX0 "^(([0-9]{1,2}:[0-9]{1,2}){1}(:[0-9]{1,2}){0,1})){0,1}$"
/* FIXME: better regex must be used */
- if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), "^(([0-9]{1,2}:[0-9]{1,2}){1}(:[0-9]{1,2}){0,1})){0,1}$", 1) == FAILURE) {
+ if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), REGEX0, sizeof(REGEX0)-1, 1) == FAILURE) {
err = 1;
}
else {
ZVAL_STRINGL(&new_val, Z_STRVAL_P(val), Z_STRLEN_P(val));
php_pgsql_add_quotes(&new_val, 1);
}
+#undef REGEX0
}
break;
@@ -6300,45 +6319,45 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
unit markings. For example, '1 12:59:10' is read the same as '1 day 12 hours 59 min 10
sec'.
*/
- if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val),
- "^(@?[ \\t]+)?("
-
- /* Textual time units and their abbreviations: */
- "(([-+]?[ \\t]+)?"
- "[0-9]+(\\.[0-9]*)?[ \\t]*"
- "(millenniums|millennia|millennium|mil|mils|"
- "centuries|century|cent|c|"
- "decades|decade|dec|decs|"
- "years|year|y|"
- "months|month|mon|"
- "weeks|week|w|"
- "days|day|d|"
- "hours|hour|hr|hrs|h|"
- "minutes|minute|mins|min|m|"
- "seconds|second|secs|sec|s))+|"
-
- /* Textual time units plus (dd)* hh[:mm[:ss]] */
- "((([-+]?[ \\t]+)?"
- "[0-9]+(\\.[0-9]*)?[ \\t]*"
- "(millenniums|millennia|millennium|mil|mils|"
- "centuries|century|cent|c|"
- "decades|decade|dec|decs|"
- "years|year|y|"
- "months|month|mon|"
- "weeks|week|w|"
- "days|day|d))+"
- "([-+]?[ \\t]+"
- "([0-9]+[ \\t]+)+" /* dd */
- "(([0-9]{1,2}:){0,2}[0-9]{0,2})" /* hh:[mm:[ss]] */
- ")?))"
- "([ \\t]+ago)?$",
- 1) == FAILURE) {
+#define REGEX0 \
+ "^(@?[ \\t]+)?(" \
+ /* Textual time units and their abbreviations: */ \
+ "(([-+]?[ \\t]+)?" \
+ "[0-9]+(\\.[0-9]*)?[ \\t]*" \
+ "(millenniums|millennia|millennium|mil|mils|" \
+ "centuries|century|cent|c|" \
+ "decades|decade|dec|decs|" \
+ "years|year|y|" \
+ "months|month|mon|" \
+ "weeks|week|w|" \
+ "days|day|d|" \
+ "hours|hour|hr|hrs|h|" \
+ "minutes|minute|mins|min|m|" \
+ "seconds|second|secs|sec|s))+|" \
+ /* Textual time units plus (dd)* hh[:mm[:ss]] */ \
+ "((([-+]?[ \\t]+)?" \
+ "[0-9]+(\\.[0-9]*)?[ \\t]*" \
+ "(millenniums|millennia|millennium|mil|mils|" \
+ "centuries|century|cent|c|" \
+ "decades|decade|dec|decs|" \
+ "years|year|y|" \
+ "months|month|mon|" \
+ "weeks|week|w|" \
+ "days|day|d))+" \
+ "([-+]?[ \\t]+" \
+ "([0-9]+[ \\t]+)+" /* dd */ \
+ "(([0-9]{1,2}:){0,2}[0-9]{0,2})" /* hh:[mm:[ss]] */ \
+ ")?))" \
+ "([ \\t]+ago)?$"
+
+ if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), REGEX0, sizeof(REGEX0)-1, 1) == FAILURE) {
err = 1;
}
else {
ZVAL_STRING(&new_val, Z_STRVAL_P(val));
php_pgsql_add_quotes(&new_val, 1);
}
+#undef REGEX0
}
break;
@@ -6411,13 +6430,15 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
ZVAL_STRINGL(&new_val, "NULL", sizeof("NULL")-1);
}
else {
- if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), "^([0-9a-f]{2,2}:){5,5}[0-9a-f]{2,2}$", 1) == FAILURE) {
+#define REGEX0 "^([0-9a-f]{2,2}:){5,5}[0-9a-f]{2,2}$"
+ if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), REGEX0, sizeof(REGEX0)-1, 1) == FAILURE) {
err = 1;
}
else {
ZVAL_STRINGL(&new_val, Z_STRVAL_P(val), Z_STRLEN_P(val));
php_pgsql_add_quotes(&new_val, 1);
}
+#undef REGEX0
}
break;
@@ -6447,12 +6468,10 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
}
/* If field is NULL and HAS DEFAULT, should be skipped */
if (!skip_field) {
- char *escaped;
-
if (_php_pgsql_detect_identifier_escape(ZSTR_VAL(field), ZSTR_LEN(field)) == SUCCESS) {
zend_hash_update(Z_ARRVAL_P(result), field, &new_val);
} else {
- escaped = PGSQLescapeIdentifier(pg_link, ZSTR_VAL(field), ZSTR_LEN(field));
+ char *escaped = PGSQLescapeIdentifier(pg_link, ZSTR_VAL(field), ZSTR_LEN(field));
add_assoc_zval(result, escaped, &new_val);
PGSQLfree(escaped);
}
@@ -6501,7 +6520,7 @@ PHP_FUNCTION(pg_convert)
}
array_init(return_value);
if (php_pgsql_convert(pg_link, table_name, values, return_value, option) == FAILURE) {
- zval_dtor(return_value);
+ zend_array_destroy(Z_ARR_P(return_value));
RETURN_FALSE;
}
}
@@ -6695,7 +6714,6 @@ PHP_FUNCTION(pg_insert)
PGconn *pg_link;
PGresult *pg_result;
ExecStatusType status;
- pgsql_result_handle *pgsql_handle;
zend_string *sql = NULL;
int argc = ZEND_NUM_ARGS();
@@ -6748,7 +6766,7 @@ PHP_FUNCTION(pg_insert)
case PGRES_COMMAND_OK: /* successful command that did not return rows */
default:
if (pg_result) {
- pgsql_handle = (pgsql_result_handle *) emalloc(sizeof(pgsql_result_handle));
+ pgsql_result_handle *pgsql_handle = (pgsql_result_handle *) emalloc(sizeof(pgsql_result_handle));
pgsql_handle->conn = pg_link;
pgsql_handle->result = pg_result;
pgsql_handle->row = 0;
@@ -6772,8 +6790,6 @@ PHP_FUNCTION(pg_insert)
static inline int build_assignment_string(PGconn *pg_link, smart_str *querystr, HashTable *ht, int where_cond, const char *pad, int pad_len, zend_ulong opt) /* {{{ */
{
- char *tmp;
- char buf[256];
zend_string *fld;
zval *val;
@@ -6783,7 +6799,7 @@ static inline int build_assignment_string(PGconn *pg_link, smart_str *querystr,
return -1;
}
if (opt & PGSQL_DML_ESCAPE) {
- tmp = PGSQLescapeIdentifier(pg_link, ZSTR_VAL(fld), ZSTR_LEN(fld) + 1);
+ char *tmp = PGSQLescapeIdentifier(pg_link, ZSTR_VAL(fld), ZSTR_LEN(fld) + 1);
smart_str_appends(querystr, tmp);
PGSQLfree(tmp);
} else {
@@ -6798,9 +6814,8 @@ static inline int build_assignment_string(PGconn *pg_link, smart_str *querystr,
switch (Z_TYPE_P(val)) {
case IS_STRING:
if (opt & PGSQL_DML_ESCAPE) {
- size_t new_len;
- tmp = (char *)safe_emalloc(Z_STRLEN_P(val), 2, 1);
- new_len = PQescapeStringConn(pg_link, tmp, Z_STRVAL_P(val), Z_STRLEN_P(val), NULL);
+ char *tmp = (char *)safe_emalloc(Z_STRLEN_P(val), 2, 1);
+ size_t new_len = PQescapeStringConn(pg_link, tmp, Z_STRVAL_P(val), Z_STRLEN_P(val), NULL);
smart_str_appendc(querystr, '\'');
smart_str_appendl(querystr, tmp, new_len);
smart_str_appendc(querystr, '\'');
@@ -6812,8 +6827,10 @@ static inline int build_assignment_string(PGconn *pg_link, smart_str *querystr,
case IS_LONG:
smart_str_append_long(querystr, Z_LVAL_P(val));
break;
- case IS_DOUBLE:
- smart_str_appendl(querystr, buf, MIN(snprintf(buf, sizeof(buf), "%F", Z_DVAL_P(val)), sizeof(buf)-1));
+ case IS_DOUBLE: {
+ char buf[256];
+ smart_str_appendl(querystr, buf, MIN(snprintf(buf, sizeof(buf), "%F", Z_DVAL_P(val)), sizeof(buf) - 1));
+ }
break;
case IS_NULL:
smart_str_appendl(querystr, "NULL", sizeof("NULL")-1);