From 9929504266c1c72df24a1db37769fb71709ff10a Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Sat, 30 Dec 2023 20:33:20 -0600 Subject: [PATCH] Update: Explicitly cast UTF conversion to/from and simple packet bit operations to a uint32_t. If the type is changed or the defines are used with different types, then the shift operators may become problematic. Prevent this potential problem from happening through explicit casts to uint32_t. --- level_0/f_fss/c/fss/simple_packet.c | 64 ++++++++++++++++++------------------- level_0/f_utf/c/utf/common.h | 32 +++++++++---------- 2 files changed, 48 insertions(+), 48 deletions(-) diff --git a/level_0/f_fss/c/fss/simple_packet.c b/level_0/f_fss/c/fss/simple_packet.c index 0c321e0..5a2c70b 100644 --- a/level_0/f_fss/c/fss/simple_packet.c +++ b/level_0/f_fss/c/fss/simple_packet.c @@ -17,32 +17,32 @@ extern "C" { #ifdef _is_F_endian_little // Big Endian. if (packet->control & F_fss_simple_packet_endian_d) { - packet->size = ((uint8_t) buffer.string[1]); - packet->size += ((uint8_t) buffer.string[2]) << 8; - packet->size += ((uint8_t) buffer.string[3]) << 16; - packet->size += ((uint8_t) buffer.string[4]) << 24; + packet->size = (uint32_t) ((uint8_t) buffer.string[1]); + packet->size += ((uint32_t) ((uint8_t) buffer.string[2])) << 8; + packet->size += ((uint32_t) ((uint8_t) buffer.string[3])) << 16; + packet->size += ((uint32_t) ((uint8_t) buffer.string[4])) << 24; } // Little Endian. else { - packet->size = ((uint8_t) buffer.string[1]) << 24; - packet->size += ((uint8_t) buffer.string[2]) << 16; - packet->size += ((uint8_t) buffer.string[3]) << 8; - packet->size += ((uint8_t) buffer.string[4]); + packet->size = ((uint32_t) ((uint8_t) buffer.string[1])) << 24; + packet->size += ((uint32_t) ((uint8_t) buffer.string[2])) << 16; + packet->size += ((uint32_t) ((uint8_t) buffer.string[3])) << 8; + packet->size += ((uint32_t) ((uint8_t) buffer.string[4])); } #else // Big Endian. if (packet->control & F_fss_simple_packet_endian_d) { - packet->size = ((uint8_t) buffer.string[1]) << 24; - packet->size += ((uint8_t) buffer.string[2]) << 16; - packet->size += ((uint8_t) buffer.string[3]) << 8; - packet->size += ((uint8_t) buffer.string[4]); + packet->size = ((uint32_t) ((uint8_t) buffer.string[1])) << 24; + packet->size += ((uint32_t) ((uint8_t) buffer.string[2])) << 16; + packet->size += ((uint32_t) ((uint8_t) buffer.string[3])) << 8; + packet->size += ((uint32_t) ((uint8_t) buffer.string[4])); } // Little Endian. else { - packet->size = ((uint8_t) buffer.string[1]); - packet->size += ((uint8_t) buffer.string[2]) << 8; - packet->size += ((uint8_t) buffer.string[3]) << 16; - packet->size += ((uint8_t) buffer.string[4]) << 24; + packet->size = ((uint32_t) ((uint8_t) buffer.string[1])); + packet->size += ((uint32_t) ((uint8_t) buffer.string[2])) << 8; + packet->size += ((uint32_t) ((uint8_t) buffer.string[3])) << 16; + packet->size += ((uint32_t) ((uint8_t) buffer.string[4])) << 24; } #endif // _is_F_endian_little @@ -63,32 +63,32 @@ extern "C" { #ifdef _is_F_endian_little // Big Endian. if (packet->control & F_fss_simple_packet_endian_d) { - packet->size = ((uint8_t) buffer.string[1]); - packet->size += ((uint8_t) buffer.string[2]) << 8; - packet->size += ((uint8_t) buffer.string[3]) << 16; - packet->size += ((uint8_t) buffer.string[4]) << 24; + packet->size = ((uint32_t) ((uint8_t) buffer.string[1])); + packet->size += ((uint32_t) ((uint8_t) buffer.string[2])) << 8; + packet->size += ((uint32_t) ((uint8_t) buffer.string[3])) << 16; + packet->size += ((uint32_t) ((uint8_t) buffer.string[4])) << 24; } // Little Endian. else { - packet->size = ((uint8_t) buffer.string[1]) << 24; - packet->size += ((uint8_t) buffer.string[2]) << 16; - packet->size += ((uint8_t) buffer.string[3]) << 8; - packet->size += ((uint8_t) buffer.string[4]); + packet->size = ((uint32_t) ((uint8_t) buffer.string[1])) << 24; + packet->size += ((uint32_t) ((uint8_t) buffer.string[2])) << 16; + packet->size += ((uint32_t) ((uint8_t) buffer.string[3])) << 8; + packet->size += ((uint32_t) ((uint8_t) buffer.string[4])); } #else // Big Endian. if (packet->control & F_fss_simple_packet_endian_d) { - packet->size = ((uint8_t) buffer.string[1]) << 24; - packet->size += ((uint8_t) buffer.string[2]) << 16; - packet->size += ((uint8_t) buffer.string[3]) << 8; - packet->size += ((uint8_t) buffer.string[4]); + packet->size = ((uint32_t) ((uint8_t) buffer.string[1])) << 24; + packet->size += ((uint32_t) ((uint8_t) buffer.string[2])) << 16; + packet->size += ((uint32_t) ((uint8_t) buffer.string[3])) << 8; + packet->size += ((uint32_t) ((uint8_t) buffer.string[4])); } // Little Endian. else { - packet->size = ((uint8_t) buffer.string[1]); - packet->size += ((uint8_t) buffer.string[2]) << 8; - packet->size += ((uint8_t) buffer.string[3]) << 16; - packet->size += ((uint8_t) buffer.string[4]) << 24; + packet->size = ((uint32_t) ((uint8_t) buffer.string[1])); + packet->size += ((uint32_t) ((uint8_t) buffer.string[2])) << 8; + packet->size += ((uint32_t) ((uint8_t) buffer.string[3])) << 16; + packet->size += ((uint32_t) ((uint8_t) buffer.string[4])) << 24; } #endif // _is_F_endian_little diff --git a/level_0/f_utf/c/utf/common.h b/level_0/f_utf/c/utf/common.h index 3cf696b..5f65f6f 100644 --- a/level_0/f_utf/c/utf/common.h +++ b/level_0/f_utf/c/utf/common.h @@ -261,15 +261,15 @@ extern "C" { #define F_utf_char_mask_char_3_be_d 0x0000ff00 // 0000 0000, 0000 0000, 1111 1111, 0000 0000 #define F_utf_char_mask_char_4_be_d 0x000000ff // 0000 0000, 0000 0000, 0000 0000, 1111 1111 - #define macro_f_utf_char_t_to_char_1_be(sequence) (((sequence) & F_utf_char_mask_char_1_be_d) >> 24) // Grab first byte. - #define macro_f_utf_char_t_to_char_2_be(sequence) (((sequence) & F_utf_char_mask_char_2_be_d) >> 16) // Grab second byte. - #define macro_f_utf_char_t_to_char_3_be(sequence) (((sequence) & F_utf_char_mask_char_3_be_d) >> 8) // Grab third byte. - #define macro_f_utf_char_t_to_char_4_be(sequence) ((sequence) & F_utf_char_mask_char_4_be_d) // Grab fourth byte. + #define macro_f_utf_char_t_to_char_1_be(sequence) ((uint32_t) ((sequence) & F_utf_char_mask_char_1_be_d) >> 24) // Grab first byte. + #define macro_f_utf_char_t_to_char_2_be(sequence) ((uint32_t) ((sequence) & F_utf_char_mask_char_2_be_d) >> 16) // Grab second byte. + #define macro_f_utf_char_t_to_char_3_be(sequence) ((uint32_t) ((sequence) & F_utf_char_mask_char_3_be_d) >> 8) // Grab third byte. + #define macro_f_utf_char_t_to_char_4_be(sequence) ((uint32_t) (sequence) & F_utf_char_mask_char_4_be_d) // Grab fourth byte. - #define macro_f_utf_char_t_from_char_1_be(sequence) (((sequence) << 24) & F_utf_char_mask_char_1_be_d) // Shift to first byte. - #define macro_f_utf_char_t_from_char_2_be(sequence) (((sequence) << 16) & F_utf_char_mask_char_2_be_d) // Shift to second byte. - #define macro_f_utf_char_t_from_char_3_be(sequence) (((sequence) << 8) & F_utf_char_mask_char_3_be_d) // Shift to third byte. - #define macro_f_utf_char_t_from_char_4_be(sequence) ((sequence) & F_utf_char_mask_char_4_be_d) // Shift to fourth byte. + #define macro_f_utf_char_t_from_char_1_be(sequence) ((((uint32_t) (sequence)) << 24) & F_utf_char_mask_char_1_be_d) // Shift to first byte. + #define macro_f_utf_char_t_from_char_2_be(sequence) ((((uint32_t) (sequence)) << 16) & F_utf_char_mask_char_2_be_d) // Shift to second byte. + #define macro_f_utf_char_t_from_char_3_be(sequence) ((((uint32_t) (sequence)) << 8) & F_utf_char_mask_char_3_be_d) // Shift to third byte. + #define macro_f_utf_char_t_from_char_4_be(sequence) (((uint32_t) (sequence)) & F_utf_char_mask_char_4_be_d) // Shift to fourth byte. // Little Endian. #define F_utf_char_mask_byte_1_le_d 0x000000ff // 0000 0000, 0000 0000, 0000 0000, 1111 1111 @@ -282,15 +282,15 @@ extern "C" { #define F_utf_char_mask_char_3_le_d 0x00ff0000 // 0000 0000, 1111 1111, 0000 0000, 0000 0000 #define F_utf_char_mask_char_4_le_d 0xff000000 // 1111 1111, 0000 0000, 0000 0000, 0000 0000 - #define macro_f_utf_char_t_to_char_1_le(sequence) ((sequence) & F_utf_char_mask_char_1_le_d) // Grab first byte. - #define macro_f_utf_char_t_to_char_2_le(sequence) (((sequence) & F_utf_char_mask_char_2_le_d) >> 8) // Grab second byte. - #define macro_f_utf_char_t_to_char_3_le(sequence) (((sequence) & F_utf_char_mask_char_3_le_d) >> 16) // Grab third byte. - #define macro_f_utf_char_t_to_char_4_le(sequence) (((sequence) & F_utf_char_mask_char_4_le_d) >> 24) // Grab fourth byte. + #define macro_f_utf_char_t_to_char_1_le(sequence) ((uint32_t) ((sequence) & F_utf_char_mask_char_1_le_d)) // Grab first byte. + #define macro_f_utf_char_t_to_char_2_le(sequence) (((uint32_t) ((sequence) & F_utf_char_mask_char_2_le_d)) >> 8) // Grab second byte. + #define macro_f_utf_char_t_to_char_3_le(sequence) (((uint32_t) ((sequence) & F_utf_char_mask_char_3_le_d)) >> 16) // Grab third byte. + #define macro_f_utf_char_t_to_char_4_le(sequence) (((uint32_t) ((sequence) & F_utf_char_mask_char_4_le_d)) >> 24) // Grab fourth byte. - #define macro_f_utf_char_t_from_char_1_le(sequence) ((sequence) & F_utf_char_mask_char_1_le_d) // Shift to first byte. - #define macro_f_utf_char_t_from_char_2_le(sequence) (((sequence) << 8) & F_utf_char_mask_char_2_le_d) // Shift to second byte. - #define macro_f_utf_char_t_from_char_3_le(sequence) (((sequence) << 16) & F_utf_char_mask_char_3_le_d) // Shift to third byte. - #define macro_f_utf_char_t_from_char_4_le(sequence) (((sequence) << 24) & F_utf_char_mask_char_4_le_d) // Shift to fourth byte. + #define macro_f_utf_char_t_from_char_1_le(sequence) (((uint32_t) (sequence) & F_utf_char_mask_char_1_le_d)) // Shift to first byte. + #define macro_f_utf_char_t_from_char_2_le(sequence) ((((uint32_t) (sequence) << 8) & F_utf_char_mask_char_2_le_d)) // Shift to second byte. + #define macro_f_utf_char_t_from_char_3_le(sequence) ((((uint32_t) (sequence) << 16) & F_utf_char_mask_char_3_le_d)) // Shift to third byte. + #define macro_f_utf_char_t_from_char_4_le(sequence) ((((uint32_t) (sequence) << 24) & F_utf_char_mask_char_4_le_d)) // Shift to fourth byte. #define F_utf_char_mask_byte_1_d F_utf_char_mask_byte_1_be_d #define F_utf_char_mask_byte_2_d F_utf_char_mask_byte_2_be_d -- 1.8.3.1