Centralize packet sanity checks, and bound one value that was overflowing.

This commit is contained in:
Ian Gulliver
2016-02-26 14:55:43 -08:00
parent 207cd1e3a1
commit d6cb3afd9d
4 changed files with 17 additions and 7 deletions

View File

@@ -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);

View File

@@ -1,3 +1,5 @@
#include <assert.h>
#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);
}

View File

@@ -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 *);

View File

@@ -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)) {