[squeak-dev] Flaw in SocketStream>>peek detected

Igor Stasenko siguctua at gmail.com
Wed Jan 13 19:18:13 UTC 2010


Peek should answer a next item to read , but do not advance the stream position.
Therefore a following test should yield true:

a := anyStream peek.
b := anyStream next.

self assert: (a = b).

In a SocketStream, however , #next, first advances the lastRead ivar,
then reads a next char
answering  inBuffer at: pos+1  ( where pos is the lastRead  value
before sending #next )

SocketStream>>next
	"Return next byte, if inBuffer is empty
	we recieve some more data and try again."

	self atEnd ifTrue: [^nil].
	self isInBufferEmpty ifTrue:
		[self receiveData.
		self atEnd ifTrue: [^nil]].
	lastRead := lastRead + 1.
	^inBuffer at: lastRead

But peek, answering a char from inBuffer at: pos !!!

peek
	"Return next byte, if inBuffer is empty
	we recieve some more data and try again.
	Do not consume the byte."

	self atEnd ifTrue: [^nil].
	self isInBufferEmpty ifTrue:
		[self receiveData.
		self atEnd ifTrue: [^nil]].
	^inBuffer at: lastRead

The fix is:

---	^inBuffer at: lastRead
+++	^inBuffer at: lastRead+1


but i'm not sure if something else won't break because of it ;)

-- 
Best regards,
Igor Stasenko AKA sig.



More information about the Squeak-dev mailing list