diff options
author | Fred Sundvik <fsundvik@gmail.com> | 2016-03-12 19:41:34 +0200 |
---|---|---|
committer | Fred Sundvik <fsundvik@gmail.com> | 2016-03-12 19:41:34 +0200 |
commit | 32f0171d393211cc29f57a0ed29327b45e9d3747 (patch) | |
tree | 2e75b83822febae021f2a1f7b65390c26571baca | |
parent | 11bd4ba0dd39654318bd0a3ae495656c2b2187bb (diff) |
Fix crash when receiving unregistered remotes
-rw-r--r-- | serial_link/protocol/transport.c | 32 |
1 files changed, 17 insertions, 15 deletions
diff --git a/serial_link/protocol/transport.c b/serial_link/protocol/transport.c index fbcb040bf1..efc00e79e0 100644 --- a/serial_link/protocol/transport.c +++ b/serial_link/protocol/transport.c @@ -71,22 +71,24 @@ void add_remote_objects(remote_object_t** _remote_objects, uint32_t _num_remote_ void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size) { uint8_t id = data[size-1]; - remote_object_t* obj = remote_objects[id]; - uint8_t* start; - if (obj->object_type == MASTER_TO_ALL_SLAVES) { - start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size); - } - else if(obj->object_type == SLAVE_TO_MASTER) { - start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size); - start += (from - 1) * REMOTE_OBJECT_SIZE(obj->object_size); - } - else { - start = obj->buffer + NUM_SLAVES * LOCAL_OBJECT_SIZE(obj->object_size); + if (id < num_remote_objects) { + remote_object_t* obj = remote_objects[id]; + uint8_t* start; + if (obj->object_type == MASTER_TO_ALL_SLAVES) { + start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size); + } + else if(obj->object_type == SLAVE_TO_MASTER) { + start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size); + start += (from - 1) * REMOTE_OBJECT_SIZE(obj->object_size); + } + else { + start = obj->buffer + NUM_SLAVES * LOCAL_OBJECT_SIZE(obj->object_size); + } + triple_buffer_object_t* tb = (triple_buffer_object_t*)start; + void* ptr = triple_buffer_begin_write_internal(obj->object_size, tb); + memcpy(ptr, data, size -1); + triple_buffer_end_write_internal(tb); } - triple_buffer_object_t* tb = (triple_buffer_object_t*)start; - void* ptr = triple_buffer_begin_write_internal(obj->object_size, tb); - memcpy(ptr, data, size -1); - triple_buffer_end_write_internal(tb); } void update_transport(void) { |