From 2512ab222d46d627fa9c03cebb97901d219e7585 Mon Sep 17 00:00:00 2001 From: Mirko Lazarevic Date: Tue, 27 Aug 2024 17:15:27 +0200 Subject: [PATCH] log_event_encoder: log_event_decoder: enhanced byte order handling for timestamps (#9196) This change ensures correct byte order conversions for timestamp fields within log event decoder and encoder. Added FLB_TO_NATIVE_UINT32 in flb_endian.h. This function checks the host machine's byte order and applies the necessary conversion for 32-bit unsigned integers. Signed-off-by: Mirko Lazarevic Co-authored-by: Bernhard Schmid --- include/fluent-bit/flb_byteswap.h | 12 ++++++++++++ src/flb_log_event_decoder.c | 4 ++-- src/flb_log_event_encoder_primitives.c | 4 ++-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/include/fluent-bit/flb_byteswap.h b/include/fluent-bit/flb_byteswap.h index b166378b53f..84e27aff91a 100644 --- a/include/fluent-bit/flb_byteswap.h +++ b/include/fluent-bit/flb_byteswap.h @@ -21,6 +21,7 @@ #define FLB_BYTESWAP_H #include +#include #if defined(FLB_HAVE_WIN32_BYTESWAP) #include @@ -102,4 +103,15 @@ static inline uint64_t FLB_BSWAP_64(uint64_t value) #endif +static inline uint32_t FLB_UINT32_TO_HOST_BYTE_ORDER(uint32_t value) +{ + #if FLB_BYTE_ORDER == FLB_LITTLE_ENDIAN + return FLB_BSWAP_32(value); + #else + return value; + #endif +} + +#define FLB_UINT32_TO_NETWORK_BYTE_ORDER(value) FLB_UINT32_TO_HOST_BYTE_ORDER(value) + #endif diff --git a/src/flb_log_event_decoder.c b/src/flb_log_event_decoder.c index 3e8ca6e56d4..a63d674a12a 100644 --- a/src/flb_log_event_decoder.c +++ b/src/flb_log_event_decoder.c @@ -181,12 +181,12 @@ int flb_log_event_decoder_decode_timestamp(msgpack_object *input, } output->tm.tv_sec = - (int32_t) FLB_BSWAP_32( + (int32_t) FLB_UINT32_TO_HOST_BYTE_ORDER( FLB_ALIGNED_DWORD_READ( (unsigned char *) &input->via.ext.ptr[0])); output->tm.tv_nsec = - (int32_t) FLB_BSWAP_32( + (int32_t) FLB_UINT32_TO_HOST_BYTE_ORDER( FLB_ALIGNED_DWORD_READ( (unsigned char *) &input->via.ext.ptr[4])); } diff --git a/src/flb_log_event_encoder_primitives.c b/src/flb_log_event_encoder_primitives.c index dbcb4a4b808..7b44bd8ea11 100644 --- a/src/flb_log_event_encoder_primitives.c +++ b/src/flb_log_event_encoder_primitives.c @@ -519,8 +519,8 @@ int flb_log_event_encoder_append_forward_v1_timestamp( { uint32_t value[2]; - value[0] = FLB_BSWAP_32((uint32_t) timestamp->tm.tv_sec); - value[1] = FLB_BSWAP_32((uint32_t) timestamp->tm.tv_nsec); + value[0] = FLB_UINT32_TO_NETWORK_BYTE_ORDER((uint32_t) timestamp->tm.tv_sec); + value[1] = FLB_UINT32_TO_NETWORK_BYTE_ORDER((uint32_t) timestamp->tm.tv_nsec); return flb_log_event_encoder_append_ext(context, target_field, 0, (char *) value, 8);