php / php/php-src

intval() with negative base prefixed string does not detect base

Open
#13,243 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Extension: standard Status: Verified
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

Description

Description

The following code:

<?php
$s = "-0b11111111111111111111111111111111111111111111111111111111111111111111111111111";
$v1 = intval($s, 2);
var_dump($v1);
$v2 = intval($s);
var_dump($v2);
var_dump((int) $s);

Resulted in this output:

int(-9223372036854775808)
int(0)
int(0)

But I expected this output instead:

int(-9223372036854775808)
int(-9223372036854775808)
int(0)

Similarly, if we use $base = 16

Note the 0o prefix is not supported (which is what initially led me down this rabbit hole).

PHP Version

PHP 8.3

Operating System

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the PHP 8.3 example with intval() on the negative 0b-prefixed string and compare explicit base 2, implicit base detection, and integer casting. Done means deciding and testing whether implicit base detection should produce the same saturated negative integer as the explicit base case, while preserving the documented lack of 0o support.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.