A strange div bug on Linux x86_64, (both dmd & ldc2): long -5000 / size_t 2 = 9223372036854773308

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Mon Aug 17 11:11:22 UTC 2020


On 8/13/20 6:13 AM, FeepingCreature wrote:
> On Thursday, 13 August 2020 at 10:10:34 UTC, H. S. Teoh wrote:
>> On Thu, Aug 13, 2020 at 07:22:18AM +0000, mw via Digitalmars-d wrote: 
>> [...]
>>> void main() {
>>>   long   a = -5000;
>>>   size_t b = 2;
>>>   long   c = a / b;
>>>   writeln(c);
>>> }
>>
>> You're mixing signed and unsigned values. That's generally dangerous 
>> territory where integer promotion rules inherited from C/C++ take over 
>> and cause sometimes weird effects, like here. Changing integer 
>> promotion rules will probably never happen now, because it will cause 
>> massive *silent* breakage of existing code. So, in the spirit of 
>> defensive programming, I recommend avoiding mixing signed/unsigned 
>> values in this way.
>>
>>
>> T
> 
> Changing integer promotion rules to disallow promotion of signed to 
> unsigned for division will not cause massive silent breakage. - But it 
> will cause massive visible breakage; Phobos uses this all over.

I wonder if such a change would uncover any bugs in phobos.


More information about the Digitalmars-d mailing list