# Negative values in PcgOneseq128XslRr64::jump($advance)

- Thread: https://externals.io/message/118328
- Original archive: https://news-web.php.net/php.internals/118328
- Started by Anton Smirnov on 2022-07-31T15:50:46+00:00
- Messages: 3
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #118328 — Anton Smirnov — 2022-07-31T15:50:46+00:00

URL: https://externals.io/message/118328#118328

Hi!

I'm writing a polyfill for ext-random and I noticed a small weirdness.
PcgOneseq128XslRr64::jump() accepts negative $advance where it
automagically becomes unsigned positive. Does it make sense or maybe
it's better to throw a ValueError there?

-- 
Anton

---

## #118330 — tim@bastelstu.be — 2022-07-31T17:52:21+00:00

URL: https://externals.io/message/118328#118330 · In reply to: #118328

Hi


> I'm writing a polyfill for ext-random and I noticed a small weirdness.
> PcgOneseq128XslRr64::jump() accepts negative $advance where it
> automagically becomes unsigned positive.

Yes, this is what happens when casting a negative signed integer to an 
unsigned integer. It will wrap around to the maximum positive number 
minus whatever the negative value was.

> Does it make sense or maybe it's better to throw a ValueError there?

No it doesn't, especially since:

$a = $pcg->generate();
$pcg->jump(-1); // jumps back 2^64 - 1 steps in a 64-bit system
var_dump($pcg->generate() === $a); // bool(false)

The PCG state is 128 Bits, so a negative jump does not work as expected 
and thus should throw.

Would you mind filing an issue in the tracker, so that it can be 
properly referenced (and thus you properly credited for this find)?

https://github.com/php/php-src/issues

Best regards
Tim Düsterhus

---

## #118333 — Anton Smirnov — 2022-07-31T20:52:08+00:00

URL: https://externals.io/message/118328#118333 · In reply to: #118330

> Hi
> 
> 
> > […]
> 
> Yes, this is what happens when casting a negative signed integer to
> an 
> unsigned integer. It will wrap around to the maximum positive number 
> minus whatever the negative value was.
> 
> > […]
> 
> No it doesn't, especially since:
> 
> $a = $pcg->generate();
> $pcg->jump(-1); // jumps back 2^64 - 1 steps in a 64-bit system
> var_dump($pcg->generate() === $a); // bool(false)
> 
> The PCG state is 128 Bits, so a negative jump does not work as
> expected 
> and thus should throw.
> 
> Would you mind filing an issue in the tracker, so that it can be 
> properly referenced (and thus you properly credited for this find)?
> 
> https://github.com/php/php-src/issues
> 
> Best regards
> Tim Düsterhus

Done, 
The issue: https://github.com/php/php-src/issues/9212
Proposed patch: https://github.com/php/php-src/pull/9213
