From d6cb3afd9dd90c0b151e7197561b7593e20445e7 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Fri, 26 Feb 2016 14:55:43 -0800 Subject: [PATCH] Centralize packet sanity checks, and bound one value that was overflowing. --- adsbus/json.c | 2 -- adsbus/packet.c | 11 ++++++++++- adsbus/packet.h | 9 +++++---- adsbus/send.c | 2 ++ 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/adsbus/json.c b/adsbus/json.c index d739cd2..80b8019 100644 --- a/adsbus/json.c +++ b/adsbus/json.c @@ -57,7 +57,6 @@ static void json_add_common(struct packet *packet, json_t *obj) { } static void json_serialize_mode_s_short(struct packet *packet, struct buf *buf) { - assert(packet->mlat_timestamp < PACKET_MLAT_MAX); uint8_t hexbuf[14]; hex_from_bin_upper(hexbuf, packet->payload, 7); json_t *out = json_pack("{ss#}", "payload", hexbuf, 14); @@ -66,7 +65,6 @@ static void json_serialize_mode_s_short(struct packet *packet, struct buf *buf) } static void json_serialize_mode_s_long(struct packet *packet, struct buf *buf) { - assert(packet->mlat_timestamp < PACKET_MLAT_MAX); uint8_t hexbuf[28]; hex_from_bin_upper(hexbuf, packet->payload, 14); json_t *out = json_pack("{ss#}", "payload", hexbuf, 28); diff --git a/adsbus/packet.c b/adsbus/packet.c index 284e688..296b015 100644 --- a/adsbus/packet.c +++ b/adsbus/packet.c @@ -1,3 +1,5 @@ +#include + #include "packet.h" char *packet_type_names[] = { @@ -29,7 +31,7 @@ static uint64_t packet_mlat_timestamp_scale_width_out(uint64_t timestamp, uint64 } uint64_t packet_mlat_timestamp_scale_in(uint64_t timestamp, uint64_t max, uint16_t mhz, struct packet_mlat_state *state) { - return packet_mlat_timestamp_scale_mhz_in(packet_mlat_timestamp_scale_width_in(timestamp, max, state), mhz); + return packet_mlat_timestamp_scale_mhz_in(packet_mlat_timestamp_scale_width_in(timestamp, max, state), mhz) % PACKET_MLAT_MAX; } uint64_t packet_mlat_timestamp_scale_out(uint64_t timestamp, uint64_t max, uint16_t mhz) { @@ -43,3 +45,10 @@ uint32_t packet_rssi_scale_in(uint32_t value, uint32_t max) { uint32_t packet_rssi_scale_out(uint32_t value, uint32_t max) { return value / (PACKET_RSSI_MAX / max); } + +void packet_sanity_check(const struct packet *packet) { + assert(packet->source_id); + assert(packet->type > PACKET_TYPE_NONE && packet->type < NUM_TYPES); + assert(packet->mlat_timestamp <= PACKET_MLAT_MAX); + assert(packet->rssi <= PACKET_RSSI_MAX); +} diff --git a/adsbus/packet.h b/adsbus/packet.h index 4941603..731c350 100644 --- a/adsbus/packet.h +++ b/adsbus/packet.h @@ -27,9 +27,10 @@ struct packet_mlat_state { uint64_t timestamp_generation; }; -uint64_t packet_mlat_timestamp_scale_in(uint64_t, uint64_t, uint16_t, struct packet_mlat_state *); -uint64_t packet_mlat_timestamp_scale_out(uint64_t, uint64_t, uint16_t); +uint64_t __attribute__ ((warn_unused_result)) packet_mlat_timestamp_scale_in(uint64_t, uint64_t, uint16_t, struct packet_mlat_state *); +uint64_t __attribute__ ((warn_unused_result)) packet_mlat_timestamp_scale_out(uint64_t, uint64_t, uint16_t); -uint32_t packet_rssi_scale_in(uint32_t, uint32_t); -uint32_t packet_rssi_scale_out(uint32_t, uint32_t); +uint32_t __attribute__ ((warn_unused_result)) packet_rssi_scale_in(uint32_t, uint32_t); +uint32_t __attribute__ ((warn_unused_result)) packet_rssi_scale_out(uint32_t, uint32_t); +void packet_sanity_check(const struct packet *); diff --git a/adsbus/send.c b/adsbus/send.c index 9400afd..de05ca2 100644 --- a/adsbus/send.c +++ b/adsbus/send.c @@ -14,6 +14,7 @@ #include "buf.h" #include "json.h" #include "list.h" +#include "packet.h" #include "peer.h" #include "proto.h" #include "raw.h" @@ -146,6 +147,7 @@ void send_new_wrapper(int fd, void *passthrough, struct peer *on_close) { } void send_write(struct packet *packet) { + packet_sanity_check(packet); for (size_t i = 0; i < NUM_SERIALIZERS; i++) { struct serializer *serializer = &serializers[i]; if (list_is_empty(&serializer->send_head)) {