From 9a9a539396c622246a527ed0c12df36edf5ff807 Mon Sep 17 00:00:00 2001 From: Justin Berger Date: Fri, 23 Feb 2018 23:15:31 -0700 Subject: Fixed unaligned access issues --- src/ootx_decoder.c | 7 ++++++- src/survive_vive.c | 18 +++++++++++++----- 2 files changed, 19 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/ootx_decoder.c b/src/ootx_decoder.c index 7bf7d7e..ad55f5b 100644 --- a/src/ootx_decoder.c +++ b/src/ootx_decoder.c @@ -182,8 +182,13 @@ union iFloat { float f; }; + +struct __attribute__((__packed__)) unaligned_16_t { + uint16_t v; +}; + float _half_to_float(uint8_t* data) { - uint16_t x = *(uint16_t*)data; + uint16_t x = ((struct unaligned_16_t*)data)->v; union iFloat fnum; fnum.f = 0; diff --git a/src/survive_vive.c b/src/survive_vive.c index 934b5d3..a74d699 100755 --- a/src/survive_vive.c +++ b/src/survive_vive.c @@ -902,8 +902,16 @@ int survive_get_config( char ** config, SurviveViveData * sv, int devno, int ifa /////////////////////////////////////////////////////////////////////////////// #define POP1 (*(readdata++)) -#define POP2 (*(((uint16_t*)((readdata+=2)-2)))) -#define POP4 (*(((uint32_t*)((readdata+=4)-4)))) + + +struct __attribute__((__packed__)) unaligned_16_t { + uint16_t v; +}; +struct __attribute__((__packed__)) unaligned_32_t { + uint32_t v; +}; +#define POP2 ((((( struct unaligned_16_t*)((readdata+=2)-2))))->v) +#define POP4 ((((( struct unaligned_32_t*)((readdata+=4)-4))))->v) void calibrate_acc(SurviveObject* so, FLT* agm) { if (so->acc_bias != NULL) { @@ -1431,7 +1439,7 @@ void survive_data_cb( SurviveUSBInterface * si ) //printf( "%d -> ", size ); for( i = 0; i < 3; i++ ) { - int16_t * acceldata = (int16_t*)readdata; + struct unaligned_16_t * acceldata = (struct unaligned_16_t *)readdata; readdata += 12; uint32_t timecode = POP4; uint8_t code = POP1; @@ -1443,8 +1451,8 @@ void survive_data_cb( SurviveUSBInterface * si ) obj->oldcode = code; //XXX XXX BIG TODO!!! Actually recal gyro data. - FLT agm[9] = { acceldata[0], acceldata[1], acceldata[2], - acceldata[3], acceldata[4], acceldata[5], + FLT agm[9] = { acceldata[0].v, acceldata[1].v, acceldata[2].v, + acceldata[3].v, acceldata[4].v, acceldata[5].v, 0,0,0 }; agm[0]*=(float)(1./8192.0); -- cgit v1.2.3 From dabb0764a080ad219e1bb477e8cf7386a1a4dc7c Mon Sep 17 00:00:00 2001 From: Justin Berger Date: Fri, 23 Feb 2018 23:22:28 -0700 Subject: Fixed OOB memory access --- src/survive_cal.c | 1 + src/survive_cal.h | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/survive_cal.c b/src/survive_cal.c index 87d8c0b..f3a682a 100755 --- a/src/survive_cal.c +++ b/src/survive_cal.c @@ -15,6 +15,7 @@ #include #include #include +#include #include "survive_config.h" diff --git a/src/survive_cal.h b/src/survive_cal.h index ae644d1..f5ef6dc 100644 --- a/src/survive_cal.h +++ b/src/survive_cal.h @@ -51,14 +51,14 @@ struct SurviveCalData ootx_decoder_context ootx_decoders[NUM_LIGHTHOUSES]; //For statistics-gathering phase. (Stage 2/3) - FLT all_lengths[MAX_SENSORS_TO_CAL][NUM_LIGHTHOUSES][2][DRPTS]; - FLT all_angles[MAX_SENSORS_TO_CAL][NUM_LIGHTHOUSES][2][DRPTS]; + FLT all_lengths[MAX_SENSORS_TO_CAL][NUM_LIGHTHOUSES][2][DRPTS + 1]; + FLT all_angles[MAX_SENSORS_TO_CAL][NUM_LIGHTHOUSES][2][DRPTS + 1]; int16_t all_counts[MAX_SENSORS_TO_CAL][NUM_LIGHTHOUSES][2]; int16_t peak_counts; int8_t found_common; int8_t times_found_common; - FLT all_sync_times[MAX_SENSORS_TO_CAL][NUM_LIGHTHOUSES][DRPTS]; + FLT all_sync_times[MAX_SENSORS_TO_CAL][NUM_LIGHTHOUSES][DRPTS + 1]; int16_t all_sync_counts[MAX_SENSORS_TO_CAL][NUM_LIGHTHOUSES]; //For camfind (4+) -- cgit v1.2.3