Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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 2 changed files with 33 additions and 15 deletions. Show diff stats Hide diff stats

  1. +19 9 src/cameras.c
  2. +14 6 src/freenect_internal.h
28 src/cameras.c
@@ -910,15 +910,25 @@ static int freenect_fetch_zero_plane_info(freenect_device *dev)
910 910 }
911 911
912 912 memcpy(&(dev->registration.zero_plane_info), reply + 94, sizeof(dev->registration.zero_plane_info));
913   - uint32_t temp;
914   - temp = fn_le32(*((uint32_t*)(&dev->registration.zero_plane_info.dcmos_emitter_dist)));
915   - dev->registration.zero_plane_info.dcmos_emitter_dist = *((float*)(&temp));
916   - temp = fn_le32(*((uint32_t*)(&dev->registration.zero_plane_info.dcmos_rcmos_dist)));
917   - dev->registration.zero_plane_info.dcmos_rcmos_dist = *((float*)(&temp));
918   - temp = fn_le32(*((uint32_t*)(&dev->registration.zero_plane_info.reference_distance)));
919   - dev->registration.zero_plane_info.reference_distance = *((float*)(&temp));
920   - temp = fn_le32(*((uint32_t*)(&dev->registration.zero_plane_info.reference_pixel_size)));
921   - dev->registration.zero_plane_info.reference_pixel_size = *((float*)(&temp));
  913 + union {
  914 + uint32_t ui;
  915 + float f;
  916 + } conversion_union;
  917 + conversion_union.f = dev->registration.zero_plane_info.dcmos_emitter_dist;
  918 + conversion_union.ui = fn_le32(conversion_union.ui);
  919 + dev->registration.zero_plane_info.dcmos_emitter_dist = conversion_union.f;
  920 +
  921 + conversion_union.f = dev->registration.zero_plane_info.dcmos_rcmos_dist;
  922 + conversion_union.ui = fn_le32(conversion_union.ui);
  923 + dev->registration.zero_plane_info.dcmos_rcmos_dist = conversion_union.f;
  924 +
  925 + conversion_union.f = dev->registration.zero_plane_info.reference_distance;
  926 + conversion_union.ui = fn_le32(conversion_union.ui);
  927 + dev->registration.zero_plane_info.reference_distance = conversion_union.f;
  928 +
  929 + conversion_union.f = dev->registration.zero_plane_info.reference_pixel_size;
  930 + conversion_union.ui = fn_le32(conversion_union.ui);
  931 + dev->registration.zero_plane_info.reference_pixel_size = conversion_union.f;
922 932
923 933 // WTF is all this data? it's way bigger than sizeof(XnFixedParams)...
924 934 FN_SPEW("dcmos_emitter_distance: %f\n", dev->registration.zero_plane_info.dcmos_emitter_dist);
20 src/freenect_internal.h
@@ -96,16 +96,24 @@ static inline uint32_t fn_le32(uint32_t d)
96 96 static inline int16_t fn_le16s(int16_t s)
97 97 {
98 98 // reinterpret cast to unsigned, use the normal fn_le16, and then reinterpret cast back
99   - uint16_t temp = (*(uint16_t*)(&s));
100   - temp = fn_le16(temp);
101   - return *((int16_t*)(&temp));
  99 + union {
  100 + int16_t s;
  101 + uint16_t u;
  102 + } conversion_union;
  103 + conversion_union.s = s;
  104 + conversion_union.u = fn_le16(conversion_union.u);
  105 + return conversion_union.s;
102 106 }
103 107 static inline int32_t fn_le32s(int32_t s)
104 108 {
105 109 // reinterpret cast to unsigned, use the normal fn_le32, and then reinterpret cast back
106   - uint32_t temp = (*(uint32_t*)(&s));
107   - temp = fn_le32(temp);
108   - return *((int32_t*)(&temp));
  110 + union {
  111 + int32_t s;
  112 + uint32_t u;
  113 + } conversion_union;
  114 + conversion_union.s = s;
  115 + conversion_union.u = fn_le32(conversion_union.u);
  116 + return conversion_union.s;
109 117 }
110 118 #else
111 119 #define fn_le16(x) (x)

0 comments on commit 5795655

Please sign in to comment.
Something went wrong with that request. Please try again.