Skip to content

Commit ef87a14

Browse files
authored
pgsql: Don't allocate memory for default arguments (#20757)
1 parent 670d300 commit ef87a14

File tree

1 file changed

+2
-18
lines changed

1 file changed

+2
-18
lines changed

ext/pgsql/pgsql.c

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3350,9 +3350,8 @@ PHP_FUNCTION(pg_copy_to)
33503350
pgsql_link_handle *link;
33513351
zend_string *table_name;
33523352
zend_string *pg_delimiter = NULL;
3353-
char *pg_null_as = NULL;
3353+
char *pg_null_as = "\\\\N";
33543354
size_t pg_null_as_len = 0;
3355-
bool free_pg_null = false;
33563355
char *query;
33573356
PGconn *pgsql;
33583357
PGresult *pgsql_result;
@@ -3377,20 +3376,13 @@ PHP_FUNCTION(pg_copy_to)
33773376
zend_argument_value_error(3, "must be one character");
33783377
RETURN_THROWS();
33793378
}
3380-
if (!pg_null_as) {
3381-
pg_null_as = estrdup("\\\\N");
3382-
free_pg_null = true;
3383-
}
33843379

33853380
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);
33863381

33873382
while ((pgsql_result = PQgetResult(pgsql))) {
33883383
PQclear(pgsql_result);
33893384
}
33903385
pgsql_result = PQexec(pgsql, query);
3391-
if (free_pg_null) {
3392-
efree(pg_null_as);
3393-
}
33943386
efree(query);
33953387

33963388
if (pgsql_result) {
@@ -3475,9 +3467,8 @@ PHP_FUNCTION(pg_copy_from)
34753467
zval *value;
34763468
zend_string *table_name;
34773469
zend_string *pg_delimiter = NULL;
3478-
char *pg_null_as = NULL;
3470+
char *pg_null_as = "\\\\N";
34793471
size_t pg_null_as_len;
3480-
bool pg_null_as_free = false;
34813472
char *query;
34823473
PGconn *pgsql;
34833474
PGresult *pgsql_result;
@@ -3502,20 +3493,13 @@ PHP_FUNCTION(pg_copy_from)
35023493
zend_argument_value_error(4, "must be one character");
35033494
RETURN_THROWS();
35043495
}
3505-
if (!pg_null_as) {
3506-
pg_null_as = estrdup("\\\\N");
3507-
pg_null_as_free = true;
3508-
}
35093496

35103497
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);
35113498
while ((pgsql_result = PQgetResult(pgsql))) {
35123499
PQclear(pgsql_result);
35133500
}
35143501
pgsql_result = PQexec(pgsql, query);
35153502

3516-
if (pg_null_as_free) {
3517-
efree(pg_null_as);
3518-
}
35193503
efree(query);
35203504

35213505
if (pgsql_result) {

0 commit comments

Comments
 (0)