简体   繁体   中英

NSInputStream read: maxlength: returning way more bytes than maxlength

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, eg 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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM