StreamDevice: assert preconditions in parsing functions

Be a bit more defensive about handling incoming messages from the stream
device. This also makes these functions consistent with
handle_msg_format(). These assertions are only enabled if
ENABLE_EXTRA_CHECKS is defined.

Signed-off-by: Jonathon Jongsma <jjongsma@redhat.com>
Acked-by: Frediano Ziglio <fziglio@redhat.com>
This commit is contained in:
Jonathon Jongsma 2017-11-30 11:53:01 -06:00 committed by Frediano Ziglio
parent 0b9e5e87e1
commit a337808fa4

View File

@ -131,6 +131,10 @@ handle_msg_invalid(StreamDevice *dev, SpiceCharDeviceInstance *sin, const char *
{
static const char default_error_msg[] = "Protocol error";
if (ENABLE_EXTRA_CHECKS) {
spice_assert(dev->hdr_pos >= sizeof(StreamDevHeader));
}
if (!error_msg) {
error_msg = default_error_msg;
}
@ -164,8 +168,10 @@ handle_msg_format(StreamDevice *dev, SpiceCharDeviceInstance *sin)
{
SpiceCharDeviceInterface *sif = spice_char_device_get_interface(sin);
spice_assert(dev->hdr_pos >= sizeof(StreamDevHeader));
spice_assert(dev->hdr.type == STREAM_TYPE_FORMAT);
if (ENABLE_EXTRA_CHECKS) {
spice_assert(dev->hdr_pos >= sizeof(StreamDevHeader));
spice_assert(dev->hdr.type == STREAM_TYPE_FORMAT);
}
int n = sif->read(sin, dev->msg.buf + dev->msg_pos, sizeof(StreamMsgFormat) - dev->msg_pos);
if (n < 0) {
@ -190,6 +196,11 @@ handle_msg_data(StreamDevice *dev, SpiceCharDeviceInstance *sin)
SpiceCharDeviceInterface *sif = spice_char_device_get_interface(sin);
int n;
if (ENABLE_EXTRA_CHECKS) {
spice_assert(dev->hdr_pos >= sizeof(StreamDevHeader));
spice_assert(dev->hdr.type == STREAM_TYPE_DATA);
}
while (1) {
uint8_t buf[16 * 1024];
n = sif->read(sin, buf, sizeof(buf));