primitive value overflow

Regan Heath regan at netmail.co.nz
Fri May 17 01:43:26 PDT 2013


On Thu, 16 May 2013 23:23:20 +0100, Marco Leise <Marco.Leise at gmx.de> wrote:

> Am Thu, 16 May 2013 22:39:16 +0200
> schrieb luka8088 <luka8088 at owave.net>:
>
>> On 16.5.2013. 22:29, Andrej Mitrovic wrote:
>> > On Thursday, 16 May 2013 at 20:24:31 UTC, luka8088 wrote:
>> >> Hello everyone.
>> >>
>> >> Today I ran into a interesting issue. I wrote
>> >>
>> >> auto offset = text1.length - text2.length;
>> >
>> > Yeah, I don't like these bugs either. In the meantime you can swap  
>> auto
>> > with 'sizediff_t' or 'ptrdiff_t', and then you can check if it's
>> > non-negative.
>>
>> Yes, thanks for the advice, I did something similar. =)
>
> Now that doesn't work if you deal with some text2 that is over
> 2 GiB longer than text1.

This is not a problem I worry about in the general case.  I can honestly  
say I have never had to write any code which had to handle strings that  
long.. in fact the longest string length would probably have fit in ushort  
TBH.

This is why, for better or worse, I cast all C strlen() calls from size_t  
to int as a matter of course.  Sure, length cannot be negative, but for  
all practical purposes it will not be larger than signed int either.  I  
have been doing this for ages now and have had exactly 0 bugs as a result,  
vs the few I have had using unsigned types and subtraction.

Of course, were I coding something that was intended to specifically  
handle super sized strings, it would be a different story.

R

-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/


More information about the Digitalmars-d mailing list