1

I have a script that divides file into 10MB chunks. Haven't had a problem with this script until I tried to do it on a 6GB file. Getting negative values on ranges even if they are uint64_t. Any suggestions on where is the error?

NSData *conData = [NSURLConnection sendSynchronousRequest:fileSizeRequest returningResponse:&response error:&error];

if (conData)
{
    NSDictionary *headers = [response allHeaderFields];
    NSString *fileSizeString = [headers objectForKey:@"Content-Length"];
    uint64_t fileSize = strtoull([fileSizeString UTF8String], NULL, 0);
    self.size += fileSize;

    uint64_t amountOfRanges = fileSize / 10485760;
    for (int i = 0; i <= amountOfRanges; i++)
    {
        uint64_t rangeMin = 0;
        uint64_t rangeMax = 0;

        if (i != amountOfRanges)
        {
            rangeMin = i * 10485760;
            rangeMax = (i + 1) * 10485760 - 1;
        }
        else
        {
            if (i == 0)
            {
                rangeMin = 0;
                rangeMax = fileSize - 1;
            }
            else
            {
                rangeMin = i * 10485760;
                rangeMax = i * 10485760 - 1 + (fileSize - rangeMin);
            }
        }
    }
}
4

1 回答 1

3

您对这样的表达式有疑问:

        rangeMin = i * 10485760;

请注意,andi是文字,因此结果表达式很容易溢出。理想情况下,您应该制作和/或使用文字,例如int10485760intintiuint64_tunsigned long long

        rangeMin = i * 10485760ULL;
于 2013-06-24T08:12:22.503 回答