Skip to content

Commit

Permalink
math: simplify type conversion with casts and limits checks
Browse files Browse the repository at this point in the history
This PR aims to address some issues presented in Fluent Bit
repo:

 - fluent/fluent-bit#8083
 - fluent/fluent-bit#8762

Signed-off-by: Eduardo Silva <[email protected]>
  • Loading branch information
edsiper committed Apr 26, 2024
1 parent f4cf560 commit 66261b0
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions include/cmetrics/cmt_math.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,7 @@
#define CMT_MATH_H

#include <inttypes.h>
#include <string.h>

union val_union {
uint64_t u;
double d;
};
#include <math.h>

/*
* This is not rocket-science and to make things easier we assume that operating on
Expand All @@ -35,18 +30,24 @@ union val_union {

static inline uint64_t cmt_math_d64_to_uint64(double val)
{
union val_union u;
if (isnan(val)) {
return 0;
}

if (isinf(val)) {
return UINT64_MAX;
}

u.d = val;
return u.u;
if (val > (double) UINT64_MAX) {
return UINT64_MAX;
}

return (uint64_t) val;
}

static inline double cmt_math_uint64_to_d64(uint64_t val)
{
union val_union u;

u.u = val;
return u.d;
return (double) val;
}

#endif

0 comments on commit 66261b0

Please sign in to comment.