Skip to content

Commit

Permalink
Fix wrong conversion and cast
Browse files Browse the repository at this point in the history
Conversions from `uint64_t`/`VALUE` to `long` may loose upper bits.
  • Loading branch information
nobu committed Aug 7, 2023
1 parent 2d11357 commit c26f650
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion ext/yarp/api_pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ static VALUE v3_2_0_symbol;
static VALUE pack_symbol;
static VALUE unpack_symbol;

#if SIZEOF_UINT64_T == SIZEOF_LONG_LONG
# define UINT64T2NUM(x) ULL2NUM(x)
# define NUM2UINT64T(x) (uint64_t)NUM2ULL(x)
#elif SIZEOF_UINT64_T == SIZEOF_LONG
# define UINT64T2NUM(x) ULONG2NUM(x)
# define NUM2UINT64T(x) (uint64_t)NUM2ULONG(x)
#else
// error No uint64_t conversion
#endif

static VALUE
pack_type_to_symbol(yp_pack_type type) {
switch (type) {
Expand Down Expand Up @@ -221,7 +231,7 @@ pack_parse(VALUE self, VALUE version_symbol, VALUE variant_symbol, VALUE format_
pack_endian_to_symbol(endian),
pack_size_to_symbol(size),
pack_length_type_to_symbol(length_type),
(long) LONG2NUM(length) };
UINT64T2NUM(length) };

rb_ary_push(directives_array, rb_class_new_instance(9, directive_args, rb_cYARPPackDirective));
}
Expand Down

0 comments on commit c26f650

Please sign in to comment.