[PECL-CVS] [pecl-text-xdiff] main: Fix memory leak when $error on input is refcounted.

[email protected] (SATO Kentaro) Sun, 5 Apr 2026 17:07:46 +0000
Newsgroups php.pecl.cvs
Message-ID <[email protected]>
Author: SATO Kentaro (ranvis)
Pusher: rlerdorf
Date: 2023-03-10T01:33:29+09:00

Commit: https://github.com/php/pecl-text-xdiff/commit/99f5be0ad835bae9b49d18f8dfd80543b4bc8ca0
Raw diff: https://github.com/php/pecl-text-xdiff/commit/99f5be0ad835bae9b49d18f8dfd80543b4bc8ca0.diff

Fix memory leak when $error on input is refcounted.

Changed paths:
  M  xdiff.c


Diff:

diff --git a/xdiff.c b/xdiff.c
index 778ab22..9b0f8c3 100644
--- a/xdiff.c
+++ b/xdiff.c
@@ -57,6 +57,15 @@
 # pragma pack(pop)
 #endif
 
+#ifndef ZEND_TRY_ASSIGN_REF_STRINGL
+#define ZEND_TRY_ASSIGN_REF_STRINGL(zv, s, l)  do { \
+	zval *_zv = (zv); \
+	ZVAL_DEREF(_zv); \
+	zval_ptr_dtor(_zv); \
+	ZVAL_STRINGL(_zv, (s), (l)); \
+} while (0)
+#endif
+
 /* Not exported by header file */
 extern char libxdiff_version[];
 
@@ -505,8 +514,7 @@ PHP_FUNCTION(xdiff_string_patch)
 		goto out_free_error_string;
 
 	if (error_string.size > 0 && error_ref) {
-		ZVAL_DEREF(error_ref);
-		ZVAL_STRINGL(error_ref, error_string.ptr, error_string.size);
+		ZEND_TRY_ASSIGN_REF_STRINGL(error_ref, error_string.ptr, error_string.size);
 	}
 
 	if (output_string.size > 0) {
@@ -676,8 +684,7 @@ PHP_FUNCTION(xdiff_string_merge3)
 		goto out_free_error_string;
 
 	if (error_string.size > 0 && error_ref) {
-		ZVAL_DEREF(error_ref);
-		ZVAL_STRINGL(error_ref, error_string.ptr, error_string.size);
+		ZEND_TRY_ASSIGN_REF_STRINGL(error_ref, error_string.ptr, error_string.size);
 	}
 
 	if (output_string.size > 0) {