peek_message() peeks the messages from server in a loop, keeping the buffer and buffer_size from previous iteration. The message size returned from server for the message may be smaller than current buffer size. Then, unpack message gets the actual message size and may reallocate the buffer to actually smaller size. So while getting next message buffer_size used for server reply size may be bigger than actually allocated buffer.