0

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!

R.S
  • 321
  • 3
  • 15

1 Answers1

3

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.

Sven
  • 22,475
  • 4
  • 52
  • 71
  • 3
    In particular, signed 32-bit -1 = unsigned 32-bit 4,294,967,295. This (along with 255 and 65535) is a number every programmer should immediately recognize. (You don't have to memorize all the digits, but when you see an int somewhere around 4.2 billion, it should raise a red flag.) – abarnert Sep 28 '12 at 07:17
  • Thanks guys! This indeed was the problem. – R.S Sep 28 '12 at 16:52