]> git.ipfire.org Git - thirdparty/libnftnl.git/commitdiff
utils: Fix nftnl_get_value() on big endian
authorPhil Sutter <phil@nwl.cc>
Fri, 22 Jun 2018 12:18:57 +0000 (14:18 +0200)
committerFlorian Westphal <fw@strlen.de>
Tue, 26 Jun 2018 18:19:28 +0000 (20:19 +0200)
This function basically did:

| memcpy(out, val, <len of requested type>);

which works only for little endian integer types. Fix this by assigning
the 64bit input value to a variable of the right size and use that as
input for above memcpy() call.

Signed-off-by: Phil Sutter <phil@nwl.cc>
src/utils.c

index 3e449609395e461d54123b8a50b02b907541d7b8..4d9ee782b56b39e15ff7b4103a982667926a5b18 100644 (file)
@@ -72,6 +72,15 @@ static struct {
 
 int nftnl_get_value(enum nftnl_type type, void *val, void *out)
 {
+       union {
+               uint8_t u8;
+               uint16_t u16;
+               uint32_t u32;
+               int8_t s8;
+               int16_t s16;
+               int32_t s32;
+       } values;
+       void *valuep = NULL;
        int64_t sval;
        uint64_t uval;
 
@@ -85,7 +94,6 @@ int nftnl_get_value(enum nftnl_type type, void *val, void *out)
                        errno = ERANGE;
                        return -1;
                }
-               memcpy(out, &uval, basetype[type].len);
                break;
        case NFTNL_TYPE_S8:
        case NFTNL_TYPE_S16:
@@ -97,10 +105,42 @@ int nftnl_get_value(enum nftnl_type type, void *val, void *out)
                        errno = ERANGE;
                        return -1;
                }
-               memcpy(out, &sval, basetype[type].len);
                break;
        }
 
+       switch (type) {
+       case NFTNL_TYPE_U8:
+               values.u8 = uval;
+               valuep = &values.u8;
+               break;
+       case NFTNL_TYPE_U16:
+               values.u16 = uval;
+               valuep = &values.u16;
+               break;
+       case NFTNL_TYPE_U32:
+               values.u32 = uval;
+               valuep = &values.u32;
+               break;
+       case NFTNL_TYPE_U64:
+               valuep = &uval;
+               break;
+       case NFTNL_TYPE_S8:
+               values.s8 = sval;
+               valuep = &values.s8;
+               break;
+       case NFTNL_TYPE_S16:
+               values.s16 = sval;
+               valuep = &values.s16;
+               break;
+       case NFTNL_TYPE_S32:
+               values.s32 = sval;
+               valuep = &values.s32;
+               break;
+       case NFTNL_TYPE_S64:
+               valuep = &sval;
+               break;
+       }
+       memcpy(out, valuep, basetype[type].len);
        return 0;
 }