# [RFC] ZPP Failure On Overflow

- Thread: https://externals.io/message/77492
- Original archive: https://news-web.php.net/php.internals/77492
- Started by Andrea Faulds on 2014-09-22T22:42:53+00:00
- Messages: 14
- RFC: ZPP Failure on Overflow (Implemented (PHP 7.0)) — https://externals.io/rfc/zpp_fail_on_overflow.md
- 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

---

## #77492 — Andrea Faulds — 2014-09-22T22:42:53+00:00

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

Good evening again,

Here’s a new RFC: https://wiki.php.net/rfc/zpp_fail_on_overflow

Thoughts appreciated, as is help with the patch, though I can probably manage on my own.

Thanks!
--
Andrea Faulds
http://ajf.me/

---

## #77560 — Stas Malyshev — 2014-09-24T00:22:12+00:00

URL: https://externals.io/message/77492#77560 · In reply to: #77492

Hi!

> Good evening again,
> 
> Here’s a new RFC: https://wiki.php.net/rfc/zpp_fail_on_overflow
> 
> Thoughts appreciated, as is help with the patch, though I can probably manage on my own.

It would be nice to describe why this change is good. So far the
motivation is "it is unintuitive" which is a fancy way of saying "I
don't like it". Could you list which use cases this functionality
improves, which real-life bugs it could fix, etc.?

If this is necessary for your BigInt RFC which would not work without it
for some reason (I have no idea if it is the case, but if it is) then
please state so explicitly and describe why. That may also help to find
alternatives in case somebody else sees any other solution that you may
have missed.

If there are some other arguments for it, please add them to the RFC.
Right now it looks kind of thin. I personally don't have any reason to
assume what you are proposing is better that what we have now, and BC
break is a cost that always must be offset by something that is worth
more. Especially a BC break in a form of "it worked before but now it
fails" - this can break code in so many hard to catch ways, where you
didn't actually care at the least if the function truncates the arg
(common situation in proxy/glue libraries, etc. - they'd be completely
fine with garbage in - garbage out) but need special code to handle
situations where the function fails to run altogether.
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/

---

## #77581 — Andrea Faulds — 2014-09-24T14:49:03+00:00

URL: https://externals.io/message/77492#77581 · In reply to: #77560

> It would be nice to describe why this change is good. So far the
> motivation is "it is unintuitive" which is a fancy way of saying "I
> don't like it". Could you list which use cases this functionality
> improves, which real-life bugs it could fix, etc.?

Basically, it would mean we fail safe rather than silently mangling data in the unusual case where some large float is passed to a function expecting an integer. There are actually quite a few bugs caused by truncation. Among the tests I have to update, one is for a date/time bug caused by a floating-point timestamp being truncated on 32-bit platforms resulting in a completely different date. Should this RFC pass, in these situations you just get an error rather than having your data mangled. If you want it to continue mangling it, that’s fine, (int) exists. If you want to handle it better, that’s also an option.

> If this is necessary for your BigInt RFC which would not work without it
> for some reason (I have no idea if it is the case, but if it is) then
> please state so explicitly and describe why. That may also help to find
> alternatives in case somebody else sees any other solution that you may
> have missed.

It’s not completely necessary, but I think bigints would make more sense with this RFC than without it. This RFC would make floats out of bounds cause an error when passed to functions expecting integers. Naturally, if this passes, the bigint patch would then do the same thing for bigints passed to functions which only support platform-native longs (32-bit or 64-bit). I want this to error, because otherwise bigints would truncate like floats, and I think that’s likely to trip people up. While PHP’s integer type would now have arbitrary precision, for obvious reasons most internal functions don’t need to handle integers larger than 32-bit or 64-bit (e.g. for a bit mask argument, or a string length). I’d much prefer if trying to do, say, str_split(“foobar”, 2 ** 128); would error rather than simply truncate silently, especially since passing such a large value here is almost certainly an error. If we truncate silently, it may seem like nothing has gone wrong. After all, if a function runs normally, raises no errors, and does not return an error value, it is usually safe to assume that everything is working fine.

> If there are some other arguments for it, please add them to the RFC.
> Right now it looks kind of thin. I personally don't have any reason to
> assume what you are proposing is better that what we have now, and BC
> break is a cost that always must be offset by something that is worth
> more. Especially a BC break in a form of "it worked before but now it
> fails" - this can break code in so many hard to catch ways, where you
> didn't actually care at the least if the function truncates the arg
> (common situation in proxy/glue libraries, etc. - they'd be completely
> fine with garbage in - garbage out) but need special code to handle
> situations where the function fails to run altogether.

How do these libraries you speak of handle passing other types of arguments that fail? Surely this isn’t a new phenomenon.

--
Andrea Faulds
http://ajf.me/

---

## #77583 — Andrea Faulds — 2014-09-24T15:07:04+00:00

URL: https://externals.io/message/77492#77583

> That said, most cases of "garbage in, garbage out" would presumably remain so, since most ZPP failures result in a return of `NULL` or FALSE, which would probably end up cast back to the expected type (int(0), string(''), etc) by the surrounding code.

Right. It’s not an E_RECOVERABLE_ERROR, you’d just get an E_WARNING.

--
Andrea Faulds
http://ajf.me/

---

## #77584 — Stas Malyshev — 2014-09-24T17:40:25+00:00

URL: https://externals.io/message/77492#77584 · In reply to: #77583

Hi!

> > […]
> 
> Right. It’s not an E_RECOVERABLE_ERROR, you’d just get an E_WARNING.

No, you'd get `E_WARNING` *and* function would not run. Where before it
did run. That is the problem, not the warning - you add more cases where
the function does not run when it did before, and that can have profound
consequences on the code that depends on it.
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/

---

## #77600 — Rowan Collins — 2014-09-24T21:49:36+00:00

URL: https://externals.io/message/77492#77600 · In reply to: #77584

> > […]
> No, you'd get `E_WARNING` *and* function would not run. Where before it
> did run. That is the problem, not the warning - you add more cases where
> the function does not run when it did before, and that can have profound
> consequences on the code that depends on it.

So, the problem comes with built-in functions, which have some side 
effect, which can be usefully run with a bogus value for an integer 
argument. In contrast, any function which has some side effect which is 
actively harmful given a bogus value would be a beneficiary of the change.

A function with no side effects will simply go from "garbage in, garbage 
out" to "garbage in, `NULL` out".

I wonder how many functions actually fall into each category.

-- 
Rowan Collins
[IMSoP]

---

## #77601 — Stas Malyshev — 2014-09-25T01:17:10+00:00

URL: https://externals.io/message/77492#77601 · In reply to: #77600

Hi!

> So, the problem comes with built-in functions, which have some side 
> effect, which can be usefully run with a bogus value for an integer 
> argument. In contrast, any function which has some side effect which is 
> actively harmful given a bogus value would be a beneficiary of the change.

No bogus value ever gets to a function - it always gets INT_MAX on
overflow. If INT_MAX is harmful for this function, this change does not
help as you could still pass INT_MAX and this change would not do anything.

The thing is relying on this would not really improve your code - it is
very rare that INT_MAX+1 is harmful for your function but INT_MAX-1 is
not. But it may be useful to know that you can pass any value and it
will be capped at INT_MAX.
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/

---

## #77602 — Andrea Faulds — 2014-09-25T02:00:18+00:00

URL: https://externals.io/message/77492#77602 · In reply to: #77601

> 
> 
> Hi!
> 
> > […]
> 
> No bogus value ever gets to a function - it always gets INT_MAX on
> overflow. If INT_MAX is harmful for this function, this change does not
> help as you could still pass INT_MAX and this change would not do anything.
> 
> The thing is relying on this would not really improve your code - it is
> very rare that INT_MAX+1 is harmful for your function but INT_MAX-1 is
> not. But it may be useful to know that you can pass any value and it
> will be capped at INT_MAX.

No it won't. Normally it truncates (module), only some functions cap.

--
Andrea Faulds
http://ajf.me/

---

## #77628 — Rowan Collins — 2014-09-25T14:43:24+00:00

URL: https://externals.io/message/77492#77628 · In reply to: #77602

Andrea Faulds wrote (on 25/09/2014):
> > […]
does not
> > […]
anything.
> > […]
 > No it won't. Normally it truncates (module), only some functions cap.

I saw that in the RFC, and thought it rather odd that a more sane 
implementation already exists, but functions have to opt in to use it.

I thought perhaps it was a side-effect of something else, but 
README.PARAMETER_PARSING_API doesn't list any other difference between 
"l" and "L":

 > l  - long (long)
 > L  - long, limits out-of-range numbers to LONG_MAX/LONG_MIN (long)

What was the original motivation for functions to have that choice?
-- 
Rowan Collins
[IMSoP]

---

## #77643 — Dmitry Stogov — 2014-09-26T06:57:27+00:00

URL: https://externals.io/message/77492#77643 · In reply to: #77628

May be this overflow checks should be optional?
In old ages compilers for Wirth family languages had option to enable or
disable overflow checks.
If option is enabled we may throw an exception, if no - work as today.
Any user would be able to chose between compatibility and safety.

Thanks. Dmitry.

On Thu, Sep 25, 2014 at 6:43 PM, Rowan Collins <rowan.collins@gmail.com>
wrote:

> Andrea Faulds wrote (on 25/09/2014):
> > […]
> not
> > […]
> anything.
> > […]
>
> I saw that in the RFC, and thought it rather odd that a more sane
> implementation already exists, but functions have to opt in to use it.
>
> I thought perhaps it was a side-effect of something else, but
> README.PARAMETER_PARSING_API doesn't list any other difference between "l"
> and "L":
>
> > […]
>
> What was the original motivation for functions to have that choice?
> --
> Rowan Collins
> [IMSoP]

---

## #77688 — Andrea Faulds — 2014-09-28T02:24:31+00:00

URL: https://externals.io/message/77492#77688 · In reply to: #77643

> May be this overflow checks should be optional?
> In old ages compilers for Wirth family languages had option to enable or
> disable overflow checks.
> If option is enabled we may throw an exception, if no - work as today.
> Any user would be able to chose between compatibility and safety.

I really, really don’t like this idea. PHP needs less INI-dependant behaviour, not more.
--
Andrea Faulds
http://ajf.me/

---

## #77691 — Stas Malyshev — 2014-09-29T01:10:11+00:00

URL: https://externals.io/message/77492#77691 · In reply to: #77643

Hi!

> May be this overflow checks should be optional?
> In old ages compilers for Wirth family languages had option to enable or
> disable overflow checks.
> If option is enabled we may throw an exception, if no - work as today.
> Any user would be able to chose between compatibility and safety.

Given that we have l and L in parameter parsing API, it is essentially
optional, but the choice is on internals developer. I'm not sure having
it as ini value would be the best idea, since behavior-modifying options
in the past proved more trouble than they're worth - one more dimension
one has to test against.

-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/

---

## #77694 — Andrea Faulds — 2014-09-29T08:29:29+00:00

URL: https://externals.io/message/77492#77694 · In reply to: #77691

> Hi!
> 
> > […]
> 
> Given that we have l and L in parameter parsing API, it is essentially
> optional, but the choice is on internals developer. I'm not sure having
> it as ini value would be the best idea, since behavior-modifying options
> in the past proved more trouble than they're worth - one more dimension
> one has to test against.

This patch preserves the cap-and-not-error behaviour for L, though NaN would continue to error.

If there’s a need for it, a new wrap-and-not-error character could be added, basically to match what l currently does.

--
Andrea Faulds
http://ajf.me/

---

## #79304 — Andrea Faulds — 2014-11-29T03:11:40+00:00

URL: https://externals.io/message/77492#79304 · In reply to: #77492

> 
> 
> Good evening again,
> 
> Here’s a new RFC: https://wiki.php.net/rfc/zpp_fail_on_overflow
> 
> Thoughts appreciated, as is help with the patch, though I can probably manage on my own.
> 
> Thanks!

At long last, we are at the point where I have a complete patch, as I’ve managed to finish fixing tests. This means it can go to a vote sometime soon. It’s been more than two months since this was first proposed, so I’ll wait a bit.

--
Andrea Faulds
http://ajf.me/
