[PECL-CVS] [pecl-file_formats-yaml] fix-memory-leaks: Fix some edge-case memory leaks and outstanding TODOs
[email protected] (Rasmus Lerdorf) Sun, 5 Apr 2026 03:24:09 +0000
| Newsgroups | php.pecl.cvs |
|---|---|
| Message-ID | <[email protected]> |
Author: Rasmus Lerdorf (rlerdorf)
Date: 2026-04-04T23:23:49-04:00
Commit: https://github.com/php/pecl-file_formats-yaml/commit/efc8cbd27e55b52132faf0329ea868d06a42feba
Raw diff: https://github.com/php/pecl-file_formats-yaml/commit/efc8cbd27e55b52132faf0329ea868d06a42feba.diff
Fix some edge-case memory leaks and outstanding TODOs
Changed paths:
M emit.c
M parse.c
M yaml.c
Diff:
diff --git a/emit.c b/emit.c
index ab7e46c..50407a7 100644
--- a/emit.c
+++ b/emit.c
@@ -580,18 +580,18 @@ static int y_write_array(
/* emit key */
status = y_write_zval(state, &key_zval, NULL);
if (SUCCESS != status) {
- return FAILURE;
+ goto cleanup_ht;
}
}
status = y_write_zval(state, elm, NULL);
-
if (SUCCESS != status) {
- return FAILURE;
+ goto cleanup_ht;
}
} ZEND_HASH_FOREACH_END();
+cleanup_ht:
#if PHP_VERSION_ID >= 70300
if (!(GC_FLAGS(ht) & GC_IMMUTABLE)) {
GC_UNPROTECT_RECURSION(ht);
@@ -602,6 +602,10 @@ static int y_write_array(
}
#endif
+ if (FAILURE == status) {
+ return FAILURE;
+ }
+
if (Y_ARRAY_SEQUENCE == array_type) {
status = yaml_sequence_end_event_initialize(&event);
} else {
@@ -747,6 +751,7 @@ y_write_object_callback (
" to contain a key named 'tag' with a string value",
clazz_name);
zend_string_release(str_key);
+ zval_ptr_dtor(&zret);
return FAILURE;
}
zend_string_release(str_key);
@@ -758,6 +763,7 @@ y_write_object_callback (
" to contain a key named 'data'",
clazz_name);
zend_string_release(str_key);
+ zval_ptr_dtor(&zret);
return FAILURE;
}
zend_string_release(str_key);
diff --git a/parse.c b/parse.c
index 17f70ba..9369fc3 100644
--- a/parse.c
+++ b/parse.c
@@ -149,8 +149,7 @@ void php_yaml_read_all(parser_state_t *state, zend_long *ndocs, zval *retval)
}
if (Y_PARSER_FAILURE == code) {
- //TODO sdubois
- //zval_ptr_dtor(&retval);
+ zval_ptr_dtor(retval);
ZVAL_UNDEF(retval);
}
}
@@ -201,8 +200,8 @@ void php_yaml_read_partial(
}
if (Y_PARSER_FAILURE == code) {
- //TODO sdubois
if (Z_TYPE_P(retval) != IS_UNDEF) {
+ zval_ptr_dtor(retval);
ZVAL_UNDEF(retval);
}
}
@@ -401,6 +400,8 @@ void handle_mapping(parser_state_t *state, zval *retval)
get_next_element(state, &value);
if (Z_TYPE(value) == IS_UNDEF) {
+ zval_ptr_dtor(retval);
+ ZVAL_UNDEF(retval);
yaml_event_delete(&src_event);
yaml_event_delete(&key_event);
zval_ptr_dtor(&key);
@@ -477,15 +478,15 @@ void handle_mapping(parser_state_t *state, zval *retval)
}
if (YAML_MAPPING_END_EVENT != state->event.type) {
- //TODO Sean-Der
+ zval_ptr_dtor(retval);
ZVAL_UNDEF(retval);
}
- if (NULL != retval && NULL != state->callbacks) {
+ if (Z_TYPE_P(retval) != IS_UNDEF && NULL != state->callbacks) {
/* apply callbacks to the collected node */
if (Y_FILTER_FAILURE == apply_filter(
retval, src_event, state->callbacks)) {
- //TODO Sean-Der
+ zval_ptr_dtor(retval);
ZVAL_UNDEF(retval);
}
}
diff --git a/yaml.c b/yaml.c
index a91d4f1..7fb0a02 100644
--- a/yaml.c
+++ b/yaml.c
@@ -528,7 +528,7 @@ PHP_FUNCTION(yaml_parse_url)
if (zndocs != NULL) {
/* copy document count to var user sent in */
- zval_dtor(zndocs);
+ zval_ptr_dtor(zndocs);
ZVAL_LONG(zndocs, ndocs);
}