Bug #81119 [Ver->Csd]: GMP operators throw errors with wrong parameter names

From: Date: Thu, 10 Jun 2021 12:48:32 +0000
Subject: Bug #81119 [Ver->Csd]: GMP operators throw errors with wrong parameter names
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-234339@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=81119&edit=1

 ID:                 81119
 Updated by:         git@php.net
 Reported by:        lauri dot kentta+php-bugs at gmail dot com
 Summary:            GMP operators throw errors with wrong parameter
                     names
-Status:             Verified
+Status:             Closed
 Type:               Bug
 Package:            GNU MP related
 PHP Version:        PHP 8.0
 Assigned To:        nikic
 Block user comment: N
 Private report:     N

 New Comment:

Automatic comment on behalf of nikic
Revision: https://github.com/php/php-src/commit/087773879f29d1b256413c14f5561e0f2fe50f31
Log: Fix bug #81119


Previous Comments:
------------------------------------------------------------------------
[2021-06-09 15:51:16] cmb@php.net

<https://3v4l.org/J00BTp>

------------------------------------------------------------------------
[2021-06-09 15:28:03] lauri dot kentta+php-bugs at gmail dot com

Description:
------------
When using GMP class operators such as + or < with an invalid second value (such as non-numeric
string or array), the exception picks up parameter name from the outer scope. Operator parameter
probably doesn't have any meaningful name. The bad messages come from ext/gmp/gmp.c, function
convert_to_gmp, lines 623 and 632.

Just run (gmp_init(1) < "x") inside a function, and you'll find the second
function parameter name in the error message.

Test script:
---------------
<?php
function test($f) {
  try {
    $f();
    echo "No error?\n";
  } catch (TypeError|ValueError $e) {
    if (strpos($e->getMessage(), '($WRONG_SCOPE_')) {
      echo "Bad error! ", $e->getMessage(), "\n";
    } else {
      echo "Good error.\n";
    }
  }
}
test(fn($WRONG_SCOPE_1 = 0, $WRONG_SCOPE_2 = 0) => gmp_init(1) < "x");
test(fn($WRONG_SCOPE_1 = 0, $WRONG_SCOPE_2 = 0) => gmp_init(1) < []);
test(fn($WRONG_SCOPE_1 = 0, $WRONG_SCOPE_2 = 0) => gmp_init(1) + "x");
test(fn($WRONG_SCOPE_1 = 0, $WRONG_SCOPE_2 = 0) => gmp_init(1) + []);


Expected result:
----------------
Good error.
Good error.
Good error.
Good error.

Actual result:
--------------
Bad error! {closure}(): Argument #2 ($WRONG_SCOPE_2) is not an integer string
Bad error! {closure}(): Argument #2 ($WRONG_SCOPE_2) must be of type GMP|string|int, array given
Bad error! {closure}(): Argument #2 ($WRONG_SCOPE_2) is not an integer string
Bad error! {closure}(): Argument #2 ($WRONG_SCOPE_2) must be of type GMP|string|int, array given


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=81119&edit=1


Thread (3 messages)

« previous php.bugs (#234339) next »