Question

I have an iOS app where I am using NSInputStream (based on a CFReadStreamRef) for reading from a network socket. I keep getting data from the server and I keep reading and processing it (using read:bytesBuffer maxLength:l). It works fine the first several times but on about the 20-25th read, this method reports that it read a HUGE number of bytes, e.g. 4,294,967,295 bytes when I really asked for a max of say 1-3MBytes. This is bizarre and seems like a bug in NSInputStream/CFReadStream API.

My app eventually crashes as it tries to load all these bytes into a buffer that is not allocated for the number of bytes returned (and the server is not returning this many bytes in the first place!)

Has anyone encountered this issue before?

Thanks!

Was it helpful?

Solution

The return value from read:maxLength: is an NSInteger which will be negative on failure. You probably are casting it to an unsigned integer type which will turn negative numbers into huge positive numbers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top