From 22a3fa0b2e31e33665765bde630bc6c6f0dd475b Mon Sep 17 00:00:00 2001
From: "Christoph M. Becker" <cmbecker69@gmx.de>
Date: Thu, 6 Jun 2019 09:29:44 +0200
Subject: [PATCH] Fix #78114: segfault when calling sodium_* functions from
 eval

We must not follow the NULL pointer.
---
 NEWS                           |  3 +++
 ext/sodium/libsodium.c         |  6 ++++--
 ext/sodium/tests/bug78114.phpt | 16 ++++++++++++++++
 3 files changed, 23 insertions(+), 2 deletions(-)
 create mode 100644 ext/sodium/tests/bug78114.phpt

diff --git a/NEWS b/NEWS
index 65a2d67e73..107b73f73e 100644
--- a/NEWS
+++ b/NEWS
@@ -34,6 +34,9 @@ PHP                                                                        NEWS
   . Fixed bug #78038 (Socket_select fails when resource array contains
     references). (Nikita)
 
+- Sodium:
+  . Fixed bug #78114 (segfault when calling sodium_* functions from eval). (cmb)
+
 - Zip:
   . Fixed bug #76345 (zip.h not found). (Michael Maroszek)
 
diff --git a/ext/sodium/libsodium.c b/ext/sodium/libsodium.c
index 50a91198b6..1ee09e0dd5 100644
--- a/ext/sodium/libsodium.c
+++ b/ext/sodium/libsodium.c
@@ -387,8 +387,10 @@ static void sodium_remove_param_values_from_backtrace(zend_object *obj) {
 		ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(trace), frame) {
 			if (Z_TYPE_P(frame) == IS_ARRAY) {
 				zval *args = zend_hash_str_find(Z_ARRVAL_P(frame), "args", sizeof("args")-1);
-				zval_ptr_dtor(args);
-				ZVAL_EMPTY_ARRAY(args);
+				if (args) {
+					zval_ptr_dtor(args);
+					ZVAL_EMPTY_ARRAY(args);
+				}
 			}
 		} ZEND_HASH_FOREACH_END();
 	}
diff --git a/ext/sodium/tests/bug78114.phpt b/ext/sodium/tests/bug78114.phpt
new file mode 100644
index 0000000000..c697ea16f8
--- /dev/null
+++ b/ext/sodium/tests/bug78114.phpt
@@ -0,0 +1,16 @@
+--TEST--
+Bug #78114 (segfault when calling sodium_* functions from eval)
+--SKIPIF--
+<?php
+if (!extension_loaded('sodium')) die('skip sodium extension not available');
+?>
+--FILE--
+<?php
+try {
+    eval('sodium_bin2hex();');
+} catch (Throwable $ex) {
+    echo $ex->getMessage(), PHP_EOL;
+}
+?>
+--EXPECT--
+sodium_bin2hex() expects exactly 1 parameter, 0 given
-- 
2.40.0