[英]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. 我有一个iOS应用程序,其中使用NSInputStream(基于CFReadStreamRef)从网络套接字读取。 I keep getting data from the server and I keep reading and processing it (using read:bytesBuffer maxLength:l ).
我不断从服务器获取数据,并继续读取和处理数据(使用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 .
它在前几次工作正常,但是在读取20-25时,此方法报告它读取了巨大的字节数,例如,当我真正要求最大1-3MBytes时,读取了4,294,967,295字节。 This is bizarre and seems like a bug in NSInputStream/CFReadStream API.
这很奇怪,似乎是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. read:maxLength:
的返回值是一个NSInteger
,失败时将为负。 You probably are casting it to an unsigned integer type which will turn negative numbers into huge positive numbers. 您可能将其转换为无符号整数类型,它将把负数变成巨大的正数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.