]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
check: enforce correct alignment during SNMP tests
authorVincent Bernat <vincent@bernat.im>
Thu, 19 Feb 2015 16:28:00 +0000 (17:28 +0100)
committerVincent Bernat <vincent@bernat.im>
Thu, 19 Feb 2015 16:28:00 +0000 (17:28 +0100)
This should fix the following clang error, despite the fact that we know
that the alignment was correct:

```
check_snmp.c:900:9: fatal error: cast from 'u_char *' (aka 'unsigned char *') to
      'unsigned long *' increases required alignment from 1 to 8 [-Wcast-align]
                            *(unsigned long int *)result,
                             ^~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/check.h:220:22: note: expanded from macro 'fail_unless'
        _fail_unless(expr, __FILE__, __LINE__,\
                     ^
1 error generated.
```

tests/check_snmp.c

index c82ad15c2cafba971da67f2c33bae8b96ea8e9ca..f05cdce3b5dd3d379d751371f27a1e2ce6638811 100644 (file)
@@ -881,6 +881,7 @@ snmp_compare(struct tree_node *n,
             u_char *result, size_t varlen,
             oid *target, size_t targetlen, char *repr)
 {
+       unsigned long int value;
        fail_unless((targetlen == sizeof(lldp_oid)/sizeof(oid) + n->namelen) &&
                    !memcmp(target, lldp_oid, sizeof(lldp_oid)) &&
                    !memcmp(target + sizeof(lldp_oid)/sizeof(oid),
@@ -896,12 +897,12 @@ snmp_compare(struct tree_node *n,
                fail_unless(varlen == sizeof(unsigned long int),
                            "Inappropriate length for integer type for OID %s",
                            repr);
-               fail_unless(n->value.integer ==
-                           *(unsigned long int *)result,
+               memcpy(&value, result, sizeof(value));
+               fail_unless(n->value.integer == value,
                            "For OID %s, expected value %u but got %u instead",
                            repr,
                            n->value.integer,
-                           *(unsigned long int *)result);
+                           value);
                break;
        default:
                fail_unless(((n->value.string.len == varlen) &&