Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Use unions to perform safe type-punning.

Signed-off-by: Drew Fisher <drew.m.fisher@gmail.com>
  • Loading branch information...
commit 57956559635d679d7f7a1a10d9bc22252246c25b 1 parent 713c715
Drew Fisher zarvox authored
Showing with 33 additions and 15 deletions.
  1. +19 −9 src/cameras.c
  2. +14 −6 src/freenect_internal.h
28 src/cameras.c
View
@@ -910,15 +910,25 @@ static int freenect_fetch_zero_plane_info(freenect_device *dev)
}
memcpy(&(dev->registration.zero_plane_info), reply + 94, sizeof(dev->registration.zero_plane_info));
- uint32_t temp;
- temp = fn_le32(*((uint32_t*)(&dev->registration.zero_plane_info.dcmos_emitter_dist)));
- dev->registration.zero_plane_info.dcmos_emitter_dist = *((float*)(&temp));
- temp = fn_le32(*((uint32_t*)(&dev->registration.zero_plane_info.dcmos_rcmos_dist)));
- dev->registration.zero_plane_info.dcmos_rcmos_dist = *((float*)(&temp));
- temp = fn_le32(*((uint32_t*)(&dev->registration.zero_plane_info.reference_distance)));
- dev->registration.zero_plane_info.reference_distance = *((float*)(&temp));
- temp = fn_le32(*((uint32_t*)(&dev->registration.zero_plane_info.reference_pixel_size)));
- dev->registration.zero_plane_info.reference_pixel_size = *((float*)(&temp));
+ union {
+ uint32_t ui;
+ float f;
+ } conversion_union;
+ conversion_union.f = dev->registration.zero_plane_info.dcmos_emitter_dist;
+ conversion_union.ui = fn_le32(conversion_union.ui);
+ dev->registration.zero_plane_info.dcmos_emitter_dist = conversion_union.f;
+
+ conversion_union.f = dev->registration.zero_plane_info.dcmos_rcmos_dist;
+ conversion_union.ui = fn_le32(conversion_union.ui);
+ dev->registration.zero_plane_info.dcmos_rcmos_dist = conversion_union.f;
+
+ conversion_union.f = dev->registration.zero_plane_info.reference_distance;
+ conversion_union.ui = fn_le32(conversion_union.ui);
+ dev->registration.zero_plane_info.reference_distance = conversion_union.f;
+
+ conversion_union.f = dev->registration.zero_plane_info.reference_pixel_size;
+ conversion_union.ui = fn_le32(conversion_union.ui);
+ dev->registration.zero_plane_info.reference_pixel_size = conversion_union.f;
// WTF is all this data? it's way bigger than sizeof(XnFixedParams)...
FN_SPEW("dcmos_emitter_distance: %f\n", dev->registration.zero_plane_info.dcmos_emitter_dist);
20 src/freenect_internal.h
View
@@ -96,16 +96,24 @@ static inline uint32_t fn_le32(uint32_t d)
static inline int16_t fn_le16s(int16_t s)
{
// reinterpret cast to unsigned, use the normal fn_le16, and then reinterpret cast back
- uint16_t temp = (*(uint16_t*)(&s));
- temp = fn_le16(temp);
- return *((int16_t*)(&temp));
+ union {
+ int16_t s;
+ uint16_t u;
+ } conversion_union;
+ conversion_union.s = s;
+ conversion_union.u = fn_le16(conversion_union.u);
+ return conversion_union.s;
}
static inline int32_t fn_le32s(int32_t s)
{
// reinterpret cast to unsigned, use the normal fn_le32, and then reinterpret cast back
- uint32_t temp = (*(uint32_t*)(&s));
- temp = fn_le32(temp);
- return *((int32_t*)(&temp));
+ union {
+ int32_t s;
+ uint32_t u;
+ } conversion_union;
+ conversion_union.s = s;
+ conversion_union.u = fn_le32(conversion_union.u);
+ return conversion_union.s;
}
#else
#define fn_le16(x) (x)
Please sign in to comment.
Something went wrong with that request. Please try again.