[PHP-CVS] [php-src] master: ext/sodium: Fix parameter name in the length-mismatch errors (#23396)

[email protected] (Louis-Arnaud via GitHub)
Newsgroups php.cvs
Message-ID <[email protected]>
Author: Louis-Arnaud (lacatoire)
Committer: GitHub (web-flow)
Pusher: TimWolla
Date: 2026-08-23T16:07:28+02:00

Commit: https://github.com/php/php-src/commit/0e8d46239162e3f6822acb8e57b5ec74ab28192a
Raw diff: https://github.com/php/php-src/commit/0e8d46239162e3f6822acb8e57b5ec74ab28192a.diff

ext/sodium: Fix parameter name in the length-mismatch errors (#23396)

sodium_add(), sodium_memcmp() and sodium_compare() all cross-reference
their second argument as $string_2, a name none of them declares; the
stub calls it $string2.

Co-authored-by: NickSdot <[email protected]>

Changed paths:
  A  ext/sodium/tests/sodium_length_mismatch_error.phpt
  M  ext/sodium/libsodium.c


Diff:

diff --git a/ext/sodium/libsodium.c b/ext/sodium/libsodium.c
index 8c85991150b5..7b8f41f2f4bb 100644
--- a/ext/sodium/libsodium.c
+++ b/ext/sodium/libsodium.c
@@ -257,7 +257,7 @@ PHP_FUNCTION(sodium_add)
 	val = (unsigned char *) Z_STRVAL(*val_zv);
 	val_len = Z_STRLEN(*val_zv);
 	if (val_len != addv_len) {
-		zend_argument_error(sodium_exception_ce, 1, "and argument #2 ($string_2) must have the same length");
+		zend_argument_error(sodium_exception_ce, 1, "and argument #2 ($string2) must have the same length");
 		RETURN_THROWS();
 	}
 	sodium_add(val, addv, val_len);
@@ -277,7 +277,7 @@ PHP_FUNCTION(sodium_memcmp)
 		RETURN_THROWS();
 	}
 	if (len1 != len2) {
-		zend_argument_error(sodium_exception_ce, 1, "and argument #2 ($string_2) must have the same length");
+		zend_argument_error(sodium_exception_ce, 1, "and argument #2 ($string2) must have the same length");
 		RETURN_THROWS();
 	}
 	RETURN_LONG(sodium_memcmp(buf1, buf2, len1));
@@ -3038,7 +3038,7 @@ PHP_FUNCTION(sodium_compare)
 		RETURN_THROWS();
 	}
 	if (len1 != len2) {
-		zend_argument_error(sodium_exception_ce, 1, "and argument #2 ($string_2) must have the same length");
+		zend_argument_error(sodium_exception_ce, 1, "and argument #2 ($string2) must have the same length");
 		RETURN_THROWS();
 	} else {
 		RETURN_LONG(sodium_compare((const unsigned char *) buf1,
diff --git a/ext/sodium/tests/sodium_length_mismatch_error.phpt b/ext/sodium/tests/sodium_length_mismatch_error.phpt
new file mode 100644
index 000000000000..098f309f5dab
--- /dev/null
+++ b/ext/sodium/tests/sodium_length_mismatch_error.phpt
@@ -0,0 +1,31 @@
+--TEST--
+The length-mismatch errors name the real second parameter
+--EXTENSIONS--
+sodium
+--FILE--
+<?php
+
+$short = str_repeat("\x01", 4);
+$long = str_repeat("\x01", 8);
+
+foreach (['sodium_add', 'sodium_memcmp', 'sodium_compare'] as $function) {
+    try {
+        $first = $short;
+        $function($first, $long);
+    } catch (Throwable $e) {
+        echo $e::class, ': ', $e->getMessage(), "\n";
+    }
+}
+
+/* the messages name argument #2, so that name has to be the real one */
+foreach ((new ReflectionFunction('sodium_add'))->getParameters() as $parameter) {
+    echo '$', $parameter->getName(), "\n";
+}
+
+?>
+--EXPECT--
+SodiumException: sodium_add(): Argument #1 ($string1) and argument #2 ($string2) must have the same length
+SodiumException: sodium_memcmp(): Argument #1 ($string1) and argument #2 ($string2) must have the same length
+SodiumException: sodium_compare(): Argument #1 ($string1) and argument #2 ($string2) must have the same length
+$string1
+$string2
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.