'int' is enough for 'length' to migrate code from x86 to x64

Walter Bright via Digitalmars-d digitalmars-d at puremagic.com
Fri Nov 21 00:48:00 PST 2014


On 11/21/2014 12:31 AM, Daniel Murphy wrote:
> Here's a simple program to calculate the relative size of two files, that will
> not work correctly with unsigned lengths.
>
> module sizediff
>
> import std.file;
> import std.stdio;
>
> void main(string[] args)
> {
>     assert(args.length == 3, "Usage: sizediff file1 file2");
>     auto l1 = args[1].read().length;
>     auto l2 = args[2].read().length;
>     writeln("Difference: ", l1 - l2);
> }
>
> The two ways this can fail (that I want to highlight) are:
> 1. If either file is too large to fit in a size_t the result will (probably) be
> wrong

Presumably read() will throw if the size is larger than it can handle. If it 
doesn't, this code is not buggy, but read() is.



More information about the Digitalmars-d mailing list