Thread overview
[Issue 15041] Pointer slice with one negative index throws 'Range violation'
Sep 28, 2015
Vladimir Panteleev
Nov 05, 2015
Cauterite
Dec 06, 2018
Basile B.
Dec 06, 2018
Vladimir Panteleev
Dec 07, 2018
anonymous4
Mar 21, 2020
Basile-z
Dec 17, 2022
Iain Buclaw
September 28, 2015
https://issues.dlang.org/show_bug.cgi?id=15041

Vladimir Panteleev <thecybershadow@gmail.com> changed:

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

--- Comment #1 from Vladimir Panteleev <thecybershadow@gmail.com> ---
> 	assert(p[-1 .. 1] == "aa"); // 'Range violation'

What happens here is that -1 gets implicitly casted to size_t, which is unsigned, so this becomes p[size_t.max .. 1]. Slicing p[a..b] when a>b is nonsense, hence the RangeError.

The check is removed when -release is present.

I'm not sure this is a bug.

--
October 01, 2015
https://issues.dlang.org/show_bug.cgi?id=15041

--- Comment #2 from anoneuron@gmail.com ---
Hmm yes, it seems all operations with array indices are treated as unsigned, and just happen to do the right thing in most cases because the integers are modular (e.g. adding size_t.max is the same as subtracting 1).

I propose instead of silently casting signed values to unsigned, the compiler should explicitly disallow use of signed values as array indices (or at least emit a warning, so as to not break existing code).

(In reply to Vladimir Panteleev from comment #1)
> > 	assert(p[-1 .. 1] == "aa"); // 'Range violation'
> 
> What happens here is that -1 gets implicitly casted to size_t, which is unsigned, so this becomes p[size_t.max .. 1]. Slicing p[a..b] when a>b is nonsense, hence the RangeError.
> 
> The check is removed when -release is present.
> 
> I'm not sure this is a bug.

--
November 05, 2015
https://issues.dlang.org/show_bug.cgi?id=15041

Cauterite <cauterite@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Depends on|                            |12919

--
December 06, 2018
https://issues.dlang.org/show_bug.cgi?id=15041

Basile B. <b2.temp@gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |b2.temp@gmx.com

--- Comment #3 from Basile B. <b2.temp@gmx.com> ---
The warning would only work for literals and CTFE-ed IntExp so i don't think this is very useful

--
December 06, 2018
https://issues.dlang.org/show_bug.cgi?id=15041

--- Comment #4 from Vladimir Panteleev <dlang-bugzilla@thecybershadow.net> ---
(In reply to Basile B. from comment #3)
> The warning would only work for literals and CTFE-ed IntExp so i don't think this is very useful

It would be useful because negative indexes are used in some languages to indicate an offset from the end of the string.

--
December 07, 2018
https://issues.dlang.org/show_bug.cgi?id=15041

--- Comment #5 from anonymous4 <dfj1esp02@sneakemail.com> ---
The example uses negative index to indicate offset from the beginning of the string, not from the end. Pointers have no notion of the end of the string anyway.

--
March 21, 2020
https://issues.dlang.org/show_bug.cgi?id=15041

Basile-z <b2.temp@gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|b2.temp@gmx.com             |

--
December 17, 2022
https://issues.dlang.org/show_bug.cgi?id=15041

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P1                          |P3

--