Skip to content

Commit d1bbc39

Browse files
committed
pg_unescape_bytea() can only fail on OOM
The implementation did not check for PQunescapeBytea failure correctly, because it checked for a null pointer after estrndup, which certainly cannot happen. Inspection of the PGunescapeBytea implementation has shown that this function can only fail on OOM, so let's check for that explicitly and remove false as a possible return type. While we're here, avoid an unnecessary copy of the result.
1 parent 5bb41fa commit d1bbc39

File tree

4 files changed

+11
-12
lines changed

4 files changed

+11
-12
lines changed

ext/opcache/Optimizer/zend_func_info.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ static const func_info_t func_infos[] = {
720720
F1("pg_copy_to", MAY_BE_FALSE | MAY_BE_ARRAY | MAY_BE_ARRAY_KEY_LONG | MAY_BE_ARRAY_OF_STRING),
721721
F1("pg_escape_string", MAY_BE_STRING),
722722
F1("pg_escape_bytea", MAY_BE_STRING),
723-
F1("pg_unescape_bytea", MAY_BE_FALSE | MAY_BE_STRING),
723+
F1("pg_unescape_bytea", MAY_BE_STRING),
724724
F1("pg_escape_literal", MAY_BE_FALSE | MAY_BE_STRING),
725725
F1("pg_escape_identifier", MAY_BE_FALSE | MAY_BE_STRING),
726726
F1("pg_result_error", MAY_BE_FALSE | MAY_BE_STRING),

ext/pgsql/pgsql.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3362,7 +3362,7 @@ PHP_FUNCTION(pg_escape_bytea)
33623362
/* {{{ Unescape binary for bytea type */
33633363
PHP_FUNCTION(pg_unescape_bytea)
33643364
{
3365-
char *from = NULL, *to = NULL, *tmp = NULL;
3365+
char *from, *tmp;
33663366
size_t to_len;
33673367
size_t from_len;
33683368
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s!",
@@ -3371,14 +3371,13 @@ PHP_FUNCTION(pg_unescape_bytea)
33713371
}
33723372

33733373
tmp = (char *)PQunescapeBytea((unsigned char*)from, &to_len);
3374-
to = estrndup(tmp, to_len);
3375-
PQfreemem(tmp);
3376-
if (!to) {
3377-
php_error_docref(NULL, E_WARNING,"Invalid parameter");
3378-
RETURN_FALSE;
3374+
if (!tmp) {
3375+
zend_error(E_ERROR, "Out of memory");
3376+
return;
33793377
}
3380-
RETVAL_STRINGL(to, to_len);
3381-
efree(to);
3378+
3379+
RETVAL_STRINGL(tmp, to_len);
3380+
PQfreemem(tmp);
33823381
}
33833382
/* }}} */
33843383

ext/pgsql/pgsql.stub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ function pg_escape_string($connection, string $data = UNKNOWN): string {}
425425
/** @param resource|string $connection */
426426
function pg_escape_bytea($connection, string $data = UNKNOWN): string {}
427427

428-
function pg_unescape_bytea(?string $data = null): string|false {}
428+
function pg_unescape_bytea(?string $data = null): string {}
429429

430430
/** @param resource|string $connection */
431431
function pg_escape_literal($connection, string $data = UNKNOWN): string|false {}

ext/pgsql/pgsql_arginfo.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: e5f2c8b3b23876a05a48500f626e81549e5d2ab1 */
2+
* Stub hash: 87152e947ab7bfb3a9d7df30dd6fbccac504504e */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_connect, 0, 0, 1)
55
ZEND_ARG_TYPE_INFO(0, connection_string, IS_STRING, 0)
@@ -325,7 +325,7 @@ ZEND_END_ARG_INFO()
325325

326326
#define arginfo_pg_escape_bytea arginfo_pg_escape_string
327327

328-
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_pg_unescape_bytea, 0, 0, MAY_BE_STRING|MAY_BE_FALSE)
328+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_pg_unescape_bytea, 0, 0, IS_STRING, 0)
329329
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, data, IS_STRING, 1, "null")
330330
ZEND_END_ARG_INFO()
331331

0 commit comments

Comments
 (0)