Skip to content

Commit

Permalink
log_event_encoder: log_event_decoder: enhanced byte order handling fo…
Browse files Browse the repository at this point in the history
…r timestamps (fluent#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 <[email protected]>
Co-authored-by: Bernhard Schmid <[email protected]>
  • Loading branch information
2 people authored and legendary-acp committed Oct 1, 2024
1 parent 1cd7767 commit 2512ab2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
12 changes: 12 additions & 0 deletions include/fluent-bit/flb_byteswap.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#define FLB_BYTESWAP_H

#include <stdint.h>
#include <fluent-bit/flb_endian.h>

#if defined(FLB_HAVE_WIN32_BYTESWAP)
#include <stdlib.h>
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions src/flb_log_event_decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
}
Expand Down
4 changes: 2 additions & 2 deletions src/flb_log_event_encoder_primitives.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 2512ab2

Please sign in to comment.