Wrong code gen / missing warning / my mistake?
Ali Çehreli
acehreli at yahoo.com
Mon Sep 10 10:29:42 PDT 2012
On 09/10/2012 10:08 AM, Benjamin Thaut wrote:
> The following code
>
>
> bool endsWith(string str, string end)
> {
> size_t to = str.length - end.length;
size_t is an unsigned type. That expression above is very dangerous.
> for(sizediff_t i = str.length - 1; i >= to; --i)
Yes, sizediff_t is signed, but...
> {
> if(str[i] != end[i-to])
to is unsigned so 'i-to' is unsigned.
> return false;
> }
> return true;
> }
>
> int main(string[] args)
> {
> return cast(int)endsWith("blub", "blub");
> }
>
> compiled with dmd 2.060 gives me a range violation. (with i = -1)
> although it shouldn't. If I change to from size_t "to" sizediff_t
> everything is fine. The comparison between the unsigned "to" and the
> signed "i" is not done correctly.
>
> Is this a code gen bug? Or is it missing a compiler warning / error? Or
> is this entierly my fault?
>
> Kind Regards
> Benjamin Thaut
Ali
More information about the Digitalmars-d
mailing list