Skip to content

Overflow in pow() #16502

Closed
Closed

Description

@YuanchengJiang

Description

The following code:

<?php
$a = gmp_init(10000000000);
var_dump(pow($a, $a)); // or var_dump($a**$a);

Resulted in this output:

gmp: overflow in mpz type
Aborted (core dumped)

But I expected this output instead:

float(INF)

PHP Version

nightly

Operating System

ubuntu 22.04

Activity

  1. cmb69 commented on Oct 19, 2024

    @cmb69
    Member

    Apparently, the library aborts, instead of signaling failure by some other means. At least mpir 3.0.0, which we use on Windows (and which is no longer maintained), aborts even on memory allocation failures. If gmplib behaves the same, it might not something we can fix.

  2. cmb69 commented on Oct 19, 2024

    @cmb69
    Member

    I think we should call mp_set_memory_functions() to install ZendMM.

    PS: see #16507.

  3. cmb69 commented on Oct 19, 2024

    @cmb69
    Member

    Possibly partial fix for this issue on Windows: BrianGladman/mpir@33be900

  4. added theissue type on Feb 5, 2025
  5. LamentXU123 commented on Jul 19, 2026

    @LamentXU123
    Member

    This is now solved. Closing.

    <?php
    $a = gmp_init(10000000000);
    var_dump(pow($a, $a)); // or var_dump($a**$a);
    PHP Fatal error:  Uncaught ValueError: Exponent must be between 0 and 4294967295 in C:\Users\admin\Desktop\bench.php:3
    Stack trace:
    #0 C:\Users\admin\Desktop\bench.php(3): pow(Object(GMP), Object(GMP))
    #1 {main}
      thrown in C:\Users\admin\Desktop\bench.php on line 3
    
    Fatal error: Uncaught ValueError: Exponent must be between 0 and 4294967295 in C:\Users\admin\Desktop\bench.php:3
    Stack trace:
    #0 C:\Users\admin\Desktop\bench.php(3): pow(Object(GMP), Object(GMP))
    #1 {main}
      thrown in C:\Users\admin\Desktop\bench.php on line 3
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions