user4388479
user4388479

Reputation: 19

[(NSInputStream *)stream read:buf maxLength:1024]; returns very huge value

In code line

->NSInteger len = [(NSInputStream *)stream read:buf   maxLength:1024]; 

I am getting very huge value of len from this method like:(18446744073709551615)

and crashes

Terminating app due to uncaught exception 'NSMallocException', reason: -[NSConcreteMutableData appendBytes:length:]: unable to allocate memory for length (18446744073709551615)

case NSStreamEventHasBytesAvailable:

{



NSMutableData* lobjReadData = [[NSMutableData alloc] init];

NSNumber* lnumBytesRead;

uint8_t buf[1024];



NSUInteger lintReadingBufferLength = 0;

NSUInteger lintTotalBufferReadedlength = 0;

NSUInteger lintPreviousBufferReadedlength = 0;

NSUInteger lintSeenIndex = 0;



while ([(NSInputStream*)stream hasBytesAvailable])

{



    lintReadingBufferLength = [(NSInputStream *)stream read:buf

                                                  maxLength:1024];



    // some times i am getting very huge vaqlue of lintReadingBufferLength like

    //18446744073709551615

    //crashes here with crash log -> Terminating app due to uncaught exception 'NSMallocException', reason: '*** -[NSConcreteMutableData appendBytes:length:]: unable to allocate memory for length (18446744073709551615)'



    lintTotalBufferReadedlength += lintReadingBufferLength;



    if(lintReadingBufferLength)

    {

        [lobjReadData appendBytes:(const void *)buf

                           length:lintReadingBufferLength];



        // bytesRead is an instance variable of type NSNumber.

        lnumBytesRead = [NSNumber numberWithInteger:

                         [lnumBytesRead integerValue]+lintReadingBufferLength];





        NSArray* larrayOfBytes = [self arrayOfBytesFromData:lobjReadData];







        for (NSInteger lintIndexCounter = lintPreviousBufferReadedlength; lintIndexCounter < lintTotalBufferReadedlength;

             lintIndexCounter++)

        {

            NSObject* lobjByte = [larrayOfBytes objectAtIndex:lintIndexCounter];



            NSString* lstrMessage = [NSString stringWithFormat:@"%@",lobjByte];



            //doing some stuff here

        }



        lintPreviousBufferReadedlength = lintTotalBufferReadedlength;

    }

    else if(0 == lintReadingBufferLength)

    {



    }

    else

    {

        SLog(@"no buffer!");

    }



}



// SLog(@"--------------------------------------");



break;

}

Upvotes: 0

Views: 739

Answers (2)

gnasher729
gnasher729

Reputation: 52538

What is the return type of the read: method? Is it NSUInteger? It isn't. It is NSInteger. So why does it return a signed integer and not an unsigned integer? That's in the documentation of the read: method. Read that documentation, then you should know that the unreasonably large number is actually a bug in your code created by using NSUInteger instead of NSInteger.

Upvotes: 0

trojanfoe
trojanfoe

Reputation: 122381

18446744073709551615 is 0xffffffffffff which is the maximum unsigned 64-bit integer value but it's also the equivalent of -1 as a 64-bit signed integer.

If you look at the reference for [NSInputStream read:maxLength:] it says:

Return Value

A number indicating the outcome of the operation:

  • A positive number indicates the number of bytes read;

  • 0 indicates that the end of the buffer was reached;

  • A negative number means that the operation failed.

So the operation failed and you are viewing the value as an unsigned value.

Upvotes: 1

Related Questions