Thread overview
[Issue 10124] Array length increases on subtraction of a big number instead of throwing RangeError
Aug 31, 2014
yebblies
Sep 01, 2014
Marco Leise
Sep 01, 2014
yebblies
Aug 29, 2020
Walter Bright
August 31, 2014
https://issues.dlang.org/show_bug.cgi?id=10124

yebblies <yebblies@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |yebblies@gmail.com

--- Comment #1 from yebblies <yebblies@gmail.com> ---
I'm not sure it should fail.  This is how unsigned types usually work.

--
August 31, 2014
https://issues.dlang.org/show_bug.cgi?id=10124

monarchdodra@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |monarchdodra@gmail.com

--- Comment #2 from monarchdodra@gmail.com ---
(In reply to yebblies from comment #1)
> I'm not sure it should fail.  This is how unsigned types usually work.

Yeah... but it's not actually an arithmetic operations here. It's a setter operation. AFAIK, array resize detects overflow. I don't see why it wouldn't detect underflow?

Just saying.

--
September 01, 2014
https://issues.dlang.org/show_bug.cgi?id=10124

Marco Leise <Marco.Leise@gmx.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |Marco.Leise@gmx.de

--- Comment #3 from Marco Leise <Marco.Leise@gmx.de> ---
I see it like the others, Denis. Detecting overflow/underflow here is no different than on regular integer math in other places. The compiler's rewrite looks fine to me. If anything, we need a generic integer overflow check placed around integer math.

Same goes for `arr.length -= -1': Again, this is how the language works. Mixing signed and unsigned is allowed, as is `a - (-1)'. While I am positive on these features in general as a nice-to-have, I think this bug report on array.length is invalid and too narrow. Consider for example some custom struct exposing some .length similar to arr.length, which would not be affected by a fix to this bug. We would get different semantics where we are striving to unify the experience.

--
September 01, 2014
https://issues.dlang.org/show_bug.cgi?id=10124

--- Comment #4 from yebblies <yebblies@gmail.com> ---
Overflow looks the same:

void main()
{
    int[] arr = [1,2,3,4];
    arr.length += uint.max;
}

It's currently being rewritten to 'length = (length op arg)'.  I'm not sure how to detect this easily without adding inline code, or an overload for each operation.

I mean, for this:
    arr.length += -3;

the compiler sees this:
    arr.length = arr.length + 4294967293u;

--
August 29, 2020
https://issues.dlang.org/show_bug.cgi?id=10124

Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |backend
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla@digitalmars.com
         Resolution|---                         |WONTFIX

--- Comment #5 from Walter Bright <bugzilla@digitalmars.com> ---
This is an artifact of 2's complement arithmetic. Instrumenting the code to detect integer arithmetic overflows will have a large performance penalty, so is not done.

--