Bug 84491

Summary: MSVC warning C4244 on hb_be_xxx_put macros in hb-private.hh
Product: HarfBuzz Reporter: Peter Kasting <pkasting>
Component: srcAssignee: Behdad Esfahbod <freedesktop>
Status: RESOLVED FIXED QA Contact:
Severity: minor    
Priority: medium CC: freedesktop
Version: unspecified   
Hardware: x86 (IA32)   
OS: Windows (All)   
Whiteboard:
i915 platform: i915 features:

Description Peter Kasting 2014-09-29 22:04:04 UTC
When building harfbuzz with MSVC with level 4 warnings enabled, use of the hb_be_xxx_put macros in hb-private.hh can trigger warning C4244.

Here are the macro definitions on trunk (incidentally, why are these ordered 16, 32, 24?):

#define hb_be_uint16_put(v,V)	HB_STMT_START { v[0] = (V>>8); v[1] = (V); } HB_STMT_END
#define hb_be_uint32_put(v,V)	HB_STMT_START { v[0] = (V>>24); v[1] = (V>>16); v[2] = (V>>8); v[3] = (V); } HB_STMT_END
#define hb_be_uint24_put(v,V)	HB_STMT_START { v[0] = (V>>16); v[1] = (V>>8); v[2] = (V); } HB_STMT_END

The warnings can be fixed by either using "& 0xff"s or static_cast<uint8_t>()s to instruct the compiler that truncation of >8-bit values to 8 bits is intentional, e.g.:

#define hb_be_uint16_put(v,V)	HB_STMT_START { v[0] = (V>>8); v[1] = static_cast<uint8_t>(V); } HB_STMT_END
#define hb_be_uint32_put(v,V)	HB_STMT_START { v[0] = (V>>24); v[1] = static_cast<uint8_t>(V>>16); v[2] = static_cast<uint8_t>(V>>8); v[3] = static_cast<uint8_t>(V); } HB_STMT_END
#define hb_be_uint24_put(v,V)	HB_STMT_START { v[0] = static_cast<uint8_t>(V>>16); v[1] = static_cast<uint8_t>(V>>8); v[2] = static_cast<uint8_t>(V); } HB_STMT_END

(Note that if callers might pass larger types to the first two macros than uint16_t and uint32_t, respectively, it's necessary to add static_cast<uint8_t>()s on the v[0] assignments as well.)
Comment 1 Behdad Esfahbod 2014-09-30 14:15:43 UTC
Thanks.  I'll take a look.
Comment 2 Behdad Esfahbod 2014-10-15 04:26:59 UTC
Should be fixed in master.

Use of freedesktop.org services, including Bugzilla, is subject to our Code of Conduct. How we collect and use information is described in our Privacy Policy.