Skip to content

Commit ac9392b

Browse files
committed
Merge branch 'PHP-8.4'
2 parents 7a7b388 + fed948d commit ac9392b

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

ext/dba/dba.c

+14-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
+----------------------------------------------------------------------+
2+
+----------------------------------------------------------------------+
33
| Copyright (c) The PHP Group |
44
+----------------------------------------------------------------------+
55
| This source file is subject to version 3.01 of the PHP license, |
@@ -857,9 +857,7 @@ static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, bool persistent)
857857
}
858858
if (!connection->info->lock.fp) {
859859
/* stream operation already wrote an error message */
860-
zend_string_release_ex(resource_key, /* persistent */ false);
861-
zval_ptr_dtor(return_value);
862-
RETURN_FALSE;
860+
goto fail;
863861
}
864862
if (!error && !php_stream_supports_lock(connection->info->lock.fp)) {
865863
error = "Stream does not support locking";
@@ -878,19 +876,15 @@ static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, bool persistent)
878876
}
879877
if (!connection->info->fp) {
880878
/* stream operation already wrote an error message */
881-
zend_string_release_ex(resource_key, /* persistent */ false);
882-
zval_ptr_dtor(return_value);
883-
RETURN_FALSE;
879+
goto fail;
884880
}
885881
if (hptr->flags & (DBA_NO_APPEND|DBA_CAST_AS_FD)) {
886882
/* Needed because some systems do not allow to write to the original
887883
* file contents with O_APPEND being set.
888884
*/
889885
if (SUCCESS != php_stream_cast(connection->info->fp, PHP_STREAM_AS_FD, (void*)&connection->info->fd, 1)) {
890886
php_error_docref(NULL, E_WARNING, "Could not cast stream");
891-
zend_string_release_ex(resource_key, /* persistent */ false);
892-
zval_ptr_dtor(return_value);
893-
RETURN_FALSE;
887+
goto fail;
894888
#ifdef F_SETFL
895889
} else if (modenr == DBA_CREAT) {
896890
int flags = fcntl(connection->info->fd, F_GETFL);
@@ -924,9 +918,7 @@ static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, bool persistent)
924918
php_error_docref(NULL, E_WARNING, "Driver initialization failed for handler: %s", hptr->name);
925919
}
926920
}
927-
zend_string_release_ex(resource_key, /* persistent */ false);
928-
zval_ptr_dtor(return_value);
929-
RETURN_FALSE;
921+
goto fail;
930922
}
931923

932924
connection->info->hnd = hptr;
@@ -935,13 +927,22 @@ static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, bool persistent)
935927
if (zend_register_persistent_resource_ex(connection->hash, connection->info, le_pdb) == NULL) {
936928
php_error_docref(NULL, E_WARNING, "Could not register persistent resource");
937929
zend_string_release_ex(resource_key, /* persistent */ false);
930+
dba_close_connection(connection);
938931
zval_ptr_dtor(return_value);
939932
RETURN_FALSE;
940933
}
941934
}
942935

943936
zend_hash_add_new(&DBA_G(connections), connection->hash, return_value);
944937
zend_string_release_ex(resource_key, /* persistent */ false);
938+
return;
939+
fail:
940+
zend_string_release_ex(resource_key, /* persistent */ false);
941+
zend_string_release_ex(connection->hash, persistent);
942+
dba_close_info(connection->info);
943+
connection->info = NULL;
944+
zval_ptr_dtor(return_value);
945+
RETURN_FALSE;
945946
}
946947
/* }}} */
947948

ext/dba/tests/gh18247.phpt

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
GH-18247: dba_open() memory leak on invalid path
3+
--EXTENSIONS--
4+
dba
5+
--FILE--
6+
<?php
7+
var_dump(dba_popen('/inexistent', 'r'));
8+
?>
9+
--EXPECTF--
10+
11+
Warning: dba_popen(/inexistent): Failed to open stream: No such file or directory in %s on line %d
12+
bool(false)

0 commit comments

Comments
 (0)