@@ -273,6 +273,8 @@ 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+
276278static zend_string * _php_pgsql_trim_message (const char * message )
277279{
278280 size_t i = strlen (message );
@@ -3347,9 +3349,8 @@ PHP_FUNCTION(pg_copy_to)
33473349 pgsql_link_handle * link ;
33483350 zend_string * table_name ;
33493351 zend_string * pg_delimiter = NULL ;
3350- char * pg_null_as = "\\\\N" ;
3351- size_t pg_null_as_len = 0 ;
3352- char * query ;
3352+ char * pg_null_as = "\\N" ;
3353+ size_t pg_null_as_len = sizeof ("\\N" ) - 1 ;
33533354 PGconn * pgsql ;
33543355 PGresult * pgsql_result ;
33553356 ExecStatusType status ;
@@ -3373,14 +3374,40 @@ PHP_FUNCTION(pg_copy_to)
33733374 zend_argument_value_error (3 , "must be one character" );
33743375 RETURN_THROWS ();
33753376 }
3377+ smart_str querystr = {0 };
3378+ smart_str_appends (& querystr , "COPY " );
3379+ if (ZSTR_LEN (table_name ) > 0 && ZSTR_VAL (table_name )[0 ] == '(' ) {
3380+ smart_str_appendc (& querystr , '(' );
3381+ smart_str_append (& querystr , table_name );
3382+ smart_str_appendc (& querystr , ')' );
3383+ } else if (build_tablename (& querystr , pgsql , table_name ) == FAILURE ) {
3384+ smart_str_free (& querystr );
3385+ RETURN_FALSE ;
3386+ }
33763387
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 );
3388+ char * escaped_delimiter = PQescapeLiteral (pgsql , ZSTR_VAL (pg_delimiter ), 1 );
3389+ if (!escaped_delimiter ) {
3390+ php_error_docref (NULL , E_WARNING , "Failed to escape delimiter" );
3391+ smart_str_free (& querystr );
3392+ RETURN_FALSE ;
3393+ }
3394+ char * escaped_null_as = PQescapeLiteral (pgsql , pg_null_as , pg_null_as_len );
3395+ if (!escaped_null_as ) {
3396+ php_error_docref (NULL , E_WARNING , "Failed to escape null_as" );
3397+ PQfreemem (escaped_delimiter );
3398+ smart_str_free (& querystr );
3399+ RETURN_FALSE ;
3400+ }
3401+ smart_str_append_printf (& querystr , " TO STDOUT DELIMITER %s NULL AS %s" , escaped_delimiter , escaped_null_as );
3402+ smart_str_0 (& querystr );
3403+ PQfreemem (escaped_delimiter );
3404+ PQfreemem (escaped_null_as );
33783405
33793406 while ((pgsql_result = PQgetResult (pgsql ))) {
33803407 PQclear (pgsql_result );
33813408 }
3382- pgsql_result = PQexec (pgsql , query );
3383- efree ( query );
3409+ pgsql_result = PQexec (pgsql , ZSTR_VAL ( querystr . s ) );
3410+ smart_str_free ( & querystr );
33843411
33853412 if (pgsql_result ) {
33863413 status = PQresultStatus (pgsql_result );
@@ -3462,9 +3489,8 @@ PHP_FUNCTION(pg_copy_from)
34623489 zval * value ;
34633490 zend_string * table_name ;
34643491 zend_string * pg_delimiter = NULL ;
3465- char * pg_null_as = "\\\\N" ;
3466- size_t pg_null_as_len ;
3467- char * query ;
3492+ char * pg_null_as = "\\N" ;
3493+ size_t pg_null_as_len = sizeof ("\\N" ) - 1 ;
34683494 PGconn * pgsql ;
34693495 PGresult * pgsql_result ;
34703496 ExecStatusType status ;
@@ -3488,14 +3514,37 @@ PHP_FUNCTION(pg_copy_from)
34883514 zend_argument_value_error (4 , "must be one character" );
34893515 RETURN_THROWS ();
34903516 }
3517+ smart_str querystr = {0 };
3518+ smart_str_appends (& querystr , "COPY " );
3519+ if (build_tablename (& querystr , pgsql , table_name ) == FAILURE ) {
3520+ smart_str_free (& querystr );
3521+ RETURN_FALSE ;
3522+ }
3523+
3524+ char * escaped_delimiter = PQescapeLiteral (pgsql , ZSTR_VAL (pg_delimiter ), 1 );
3525+ if (!escaped_delimiter ) {
3526+ php_error_docref (NULL , E_WARNING , "Failed to escape delimiter" );
3527+ smart_str_free (& querystr );
3528+ RETURN_FALSE ;
3529+ }
3530+ char * escaped_null_as = PQescapeLiteral (pgsql , pg_null_as , pg_null_as_len );
3531+ if (!escaped_null_as ) {
3532+ php_error_docref (NULL , E_WARNING , "Failed to escape null_as" );
3533+ PQfreemem (escaped_delimiter );
3534+ smart_str_free (& querystr );
3535+ RETURN_FALSE ;
3536+ }
3537+ smart_str_append_printf (& querystr , " FROM STDIN DELIMITER %s NULL AS %s" , escaped_delimiter , escaped_null_as );
3538+ smart_str_0 (& querystr );
3539+ PQfreemem (escaped_delimiter );
3540+ PQfreemem (escaped_null_as );
34913541
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 );
34933542 while ((pgsql_result = PQgetResult (pgsql ))) {
34943543 PQclear (pgsql_result );
34953544 }
3496- pgsql_result = PQexec (pgsql , query );
3545+ pgsql_result = PQexec (pgsql , ZSTR_VAL ( querystr . s ) );
34973546
3498- efree ( query );
3547+ smart_str_free ( & querystr );
34993548
35003549 if (pgsql_result ) {
35013550 status = PQresultStatus (pgsql_result );
0 commit comments