php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #81119 GMP operators throw errors with wrong parameter names
Submitted: 2021-06-09 15:28 UTC Modified: 2021-06-10 12:33 UTC
From: lauri dot kentta+php-bugs at gmail dot com Assigned: nikic (profile)
Status: Closed Package: GNU MP related
PHP Version: PHP 8.0 OS:
Private report: No CVE-ID: None
 [2021-06-09 15:28 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-06-09 15:51 UTC] cmb@php.net
-Status: Open +Status: Verified -PHP Version: master-Git-2021-06-09 (Git) +PHP Version: PHP 8.0
 [2021-06-09 15:51 UTC] cmb@php.net
<https://3v4l.org/J00BTp>
 [2021-06-10 12:33 UTC] nikic@php.net
-Assigned To: +Assigned To: nikic
 [2021-06-10 12:48 UTC] git@php.net
Automatic comment on behalf of nikic
Revision: https://github.com/php/php-src/commit/087773879f29d1b256413c14f5561e0f2fe50f31
Log: Fix bug #81119
 [2021-06-10 12:48 UTC] git@php.net
-Status: Verified +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Dec 03 17:01:29 2024 UTC