@@ -273,6 +273,60 @@ static void pgsql_lob_free_obj(zend_object *obj)
273273
274274/* Compatibility definitions */
275275
276+ static inline zend_result build_tablename (smart_str * querystr , PGconn * pg_link , const zend_string * table );
277+
278+ static bool pgsql_copy_table_name_is_simple (const char * s , size_t len )
279+ {
280+ if (len == 0 ) {
281+ return false;
282+ }
283+ size_t i = 0 ;
284+ if (!(isalpha ((unsigned char ) s [i ]) || s [i ] == '_' )) {
285+ return false;
286+ }
287+ i ++ ;
288+ while (i < len && (isalnum ((unsigned char ) s [i ]) || s [i ] == '_' )) {
289+ i ++ ;
290+ }
291+ if (i == len ) {
292+ return true;
293+ }
294+ if (s [i ] != '.' ) {
295+ return false;
296+ }
297+ i ++ ;
298+ if (i >= len || !(isalpha ((unsigned char ) s [i ]) || s [i ] == '_' )) {
299+ return false;
300+ }
301+ i ++ ;
302+ while (i < len && (isalnum ((unsigned char ) s [i ]) || s [i ] == '_' )) {
303+ i ++ ;
304+ }
305+ return i == len ;
306+ }
307+
308+ static bool pgsql_copy_query_form_balanced (const char * s , size_t len )
309+ {
310+ if (len < 2 || s [0 ] != '(' || s [len - 1 ] != ')' ) {
311+ return false;
312+ }
313+ int depth = 0 ;
314+ for (size_t i = 0 ; i < len ; i ++ ) {
315+ if (s [i ] == '(' ) {
316+ depth ++ ;
317+ } else if (s [i ] == ')' ) {
318+ depth -- ;
319+ if (depth < 0 ) {
320+ return false;
321+ }
322+ if (depth == 0 && i != len - 1 ) {
323+ return false;
324+ }
325+ }
326+ }
327+ return depth == 0 ;
328+ }
329+
276330static zend_string * _php_pgsql_trim_message (const char * message )
277331{
278332 size_t i = strlen (message );
@@ -3347,9 +3401,8 @@ PHP_FUNCTION(pg_copy_to)
33473401 pgsql_link_handle * link ;
33483402 zend_string * table_name ;
33493403 zend_string * pg_delimiter = NULL ;
3350- char * pg_null_as = "\\\\N" ;
3351- size_t pg_null_as_len = 0 ;
3352- char * query ;
3404+ char * pg_null_as = "\\N" ;
3405+ size_t pg_null_as_len = sizeof ("\\N" ) - 1 ;
33533406 PGconn * pgsql ;
33543407 PGresult * pgsql_result ;
33553408 ExecStatusType status ;
@@ -3373,14 +3426,56 @@ PHP_FUNCTION(pg_copy_to)
33733426 zend_argument_value_error (3 , "must be one character" );
33743427 RETURN_THROWS ();
33753428 }
3429+ smart_str querystr = {0 };
3430+ smart_str_appends (& querystr , "COPY " );
3431+ if (ZSTR_LEN (table_name ) > 0 && ZSTR_VAL (table_name )[0 ] == '(' ) {
3432+ if (!pgsql_copy_query_form_balanced (ZSTR_VAL (table_name ), ZSTR_LEN (table_name ))) {
3433+ php_error_docref (NULL , E_WARNING , "Invalid query source '%s': must be a single balanced parenthesised expression" , ZSTR_VAL (table_name ));
3434+ smart_str_free (& querystr );
3435+ RETURN_FALSE ;
3436+ }
3437+ smart_str_appendc (& querystr , '(' );
3438+ smart_str_append (& querystr , table_name );
3439+ smart_str_appendc (& querystr , ')' );
3440+ } else {
3441+ if (!pgsql_copy_table_name_is_simple (ZSTR_VAL (table_name ), ZSTR_LEN (table_name ))) {
3442+ php_error_docref (NULL , E_WARNING , "Invalid table_name '%s': must be a plain identifier or schema.table" , ZSTR_VAL (table_name ));
3443+ smart_str_free (& querystr );
3444+ RETURN_FALSE ;
3445+ }
3446+ if (build_tablename (& querystr , pgsql , table_name ) == FAILURE ) {
3447+ smart_str_free (& querystr );
3448+ RETURN_FALSE ;
3449+ }
3450+ }
33763451
3377- spprintf (& query , 0 , "COPY %s TO STDOUT DELIMITER E'%c' NULL AS E'%s'" , ZSTR_VAL (table_name ), * ZSTR_VAL (pg_delimiter ), pg_null_as );
3452+ char * escaped_delimiter = PQescapeLiteral (pgsql , ZSTR_VAL (pg_delimiter ), 1 );
3453+ if (!escaped_delimiter ) {
3454+ zend_string * msgbuf = _php_pgsql_trim_message (PQerrorMessage (pgsql ));
3455+ php_error_docref (NULL , E_WARNING , "Failed to escape delimiter '%c': %s" , * ZSTR_VAL (pg_delimiter ), ZSTR_VAL (msgbuf ));
3456+ zend_string_release (msgbuf );
3457+ smart_str_free (& querystr );
3458+ RETURN_FALSE ;
3459+ }
3460+ char * escaped_null_as = PQescapeLiteral (pgsql , pg_null_as , pg_null_as_len );
3461+ if (!escaped_null_as ) {
3462+ zend_string * msgbuf = _php_pgsql_trim_message (PQerrorMessage (pgsql ));
3463+ php_error_docref (NULL , E_WARNING , "Failed to escape null_as '%s': %s" , pg_null_as , ZSTR_VAL (msgbuf ));
3464+ zend_string_release (msgbuf );
3465+ PQfreemem (escaped_delimiter );
3466+ smart_str_free (& querystr );
3467+ RETURN_FALSE ;
3468+ }
3469+ smart_str_append_printf (& querystr , " TO STDOUT DELIMITER %s NULL AS %s" , escaped_delimiter , escaped_null_as );
3470+ smart_str_0 (& querystr );
3471+ PQfreemem (escaped_delimiter );
3472+ PQfreemem (escaped_null_as );
33783473
33793474 while ((pgsql_result = PQgetResult (pgsql ))) {
33803475 PQclear (pgsql_result );
33813476 }
3382- pgsql_result = PQexec (pgsql , query );
3383- efree ( query );
3477+ pgsql_result = PQexec (pgsql , ZSTR_VAL ( querystr . s ) );
3478+ smart_str_free ( & querystr );
33843479
33853480 if (pgsql_result ) {
33863481 status = PQresultStatus (pgsql_result );
@@ -3462,9 +3557,8 @@ PHP_FUNCTION(pg_copy_from)
34623557 zval * value ;
34633558 zend_string * table_name ;
34643559 zend_string * pg_delimiter = NULL ;
3465- char * pg_null_as = "\\\\N" ;
3466- size_t pg_null_as_len ;
3467- char * query ;
3560+ char * pg_null_as = "\\N" ;
3561+ size_t pg_null_as_len = sizeof ("\\N" ) - 1 ;
34683562 PGconn * pgsql ;
34693563 PGresult * pgsql_result ;
34703564 ExecStatusType status ;
@@ -3488,14 +3582,46 @@ PHP_FUNCTION(pg_copy_from)
34883582 zend_argument_value_error (4 , "must be one character" );
34893583 RETURN_THROWS ();
34903584 }
3585+ smart_str querystr = {0 };
3586+ smart_str_appends (& querystr , "COPY " );
3587+ if (!pgsql_copy_table_name_is_simple (ZSTR_VAL (table_name ), ZSTR_LEN (table_name ))) {
3588+ php_error_docref (NULL , E_WARNING , "Invalid table_name '%s': must be a plain identifier or schema.table" , ZSTR_VAL (table_name ));
3589+ smart_str_free (& querystr );
3590+ RETURN_FALSE ;
3591+ }
3592+ if (build_tablename (& querystr , pgsql , table_name ) == FAILURE ) {
3593+ smart_str_free (& querystr );
3594+ RETURN_FALSE ;
3595+ }
3596+
3597+ char * escaped_delimiter = PQescapeLiteral (pgsql , ZSTR_VAL (pg_delimiter ), 1 );
3598+ if (!escaped_delimiter ) {
3599+ zend_string * msgbuf = _php_pgsql_trim_message (PQerrorMessage (pgsql ));
3600+ php_error_docref (NULL , E_WARNING , "Failed to escape delimiter '%c': %s" , * ZSTR_VAL (pg_delimiter ), ZSTR_VAL (msgbuf ));
3601+ zend_string_release (msgbuf );
3602+ smart_str_free (& querystr );
3603+ RETURN_FALSE ;
3604+ }
3605+ char * escaped_null_as = PQescapeLiteral (pgsql , pg_null_as , pg_null_as_len );
3606+ if (!escaped_null_as ) {
3607+ zend_string * msgbuf = _php_pgsql_trim_message (PQerrorMessage (pgsql ));
3608+ php_error_docref (NULL , E_WARNING , "Failed to escape null_as '%s': %s" , pg_null_as , ZSTR_VAL (msgbuf ));
3609+ zend_string_release (msgbuf );
3610+ PQfreemem (escaped_delimiter );
3611+ smart_str_free (& querystr );
3612+ RETURN_FALSE ;
3613+ }
3614+ smart_str_append_printf (& querystr , " FROM STDIN DELIMITER %s NULL AS %s" , escaped_delimiter , escaped_null_as );
3615+ smart_str_0 (& querystr );
3616+ PQfreemem (escaped_delimiter );
3617+ PQfreemem (escaped_null_as );
34913618
3492- spprintf (& query , 0 , "COPY %s FROM STDIN DELIMITER E'%c' NULL AS E'%s'" , ZSTR_VAL (table_name ), * ZSTR_VAL (pg_delimiter ), pg_null_as );
34933619 while ((pgsql_result = PQgetResult (pgsql ))) {
34943620 PQclear (pgsql_result );
34953621 }
3496- pgsql_result = PQexec (pgsql , query );
3622+ pgsql_result = PQexec (pgsql , ZSTR_VAL ( querystr . s ) );
34973623
3498- efree ( query );
3624+ smart_str_free ( & querystr );
34993625
35003626 if (pgsql_result ) {
35013627 status = PQresultStatus (pgsql_result );
@@ -5574,7 +5700,9 @@ static inline zend_result build_tablename(smart_str *querystr, PGconn *pg_link,
55745700 } else {
55755701 char * escaped = PQescapeIdentifier (pg_link , ZSTR_VAL (table ), len );
55765702 if (escaped == NULL ) {
5577- php_error_docref (NULL , E_NOTICE , "Failed to escape table name '%s'" , ZSTR_VAL (table ));
5703+ zend_string * msgbuf = _php_pgsql_trim_message (PQerrorMessage (pg_link ));
5704+ php_error_docref (NULL , E_WARNING , "Failed to escape table name '%s': %s" , ZSTR_VAL (table ), ZSTR_VAL (msgbuf ));
5705+ zend_string_release (msgbuf );
55785706 return FAILURE ;
55795707 }
55805708 smart_str_appends (querystr , escaped );
@@ -5590,7 +5718,9 @@ static inline zend_result build_tablename(smart_str *querystr, PGconn *pg_link,
55905718 } else {
55915719 char * escaped = PQescapeIdentifier (pg_link , after_dot , len );
55925720 if (escaped == NULL ) {
5593- php_error_docref (NULL , E_NOTICE , "Failed to escape table name '%s'" , ZSTR_VAL (table ));
5721+ zend_string * msgbuf = _php_pgsql_trim_message (PQerrorMessage (pg_link ));
5722+ php_error_docref (NULL , E_WARNING , "Failed to escape table name '%s': %s" , ZSTR_VAL (table ), ZSTR_VAL (msgbuf ));
5723+ zend_string_release (msgbuf );
55945724 return FAILURE ;
55955725 }
55965726 smart_str_appendc (querystr , '.' );
0 commit comments