get rid of undefined behavior
diff --git a/skeletons/OBJECT_IDENTIFIER.c b/skeletons/OBJECT_IDENTIFIER.c
index ffca36e..b2c97ae 100644
--- a/skeletons/OBJECT_IDENTIFIER.c
+++ b/skeletons/OBJECT_IDENTIFIER.c
@@ -80,7 +80,6 @@
return 0;
}
-
int
OBJECT_IDENTIFIER_get_single_arc(const uint8_t *arcbuf, unsigned int arclen, signed int add, void *rvbufp, unsigned int rvsize) {
unsigned LE GCC_NOTUSED = 1; /* Little endian (x86) */
@@ -93,6 +92,8 @@
rvsize *= CHAR_BIT; /* bytes to bits */
arclen *= 7; /* bytes to bits */
+ assert(add <= 0);
+
/*
* The arc has the number of bits
* cannot be represented using supplied return value type.
@@ -133,11 +134,13 @@
/* Gather all bits into the accumulator */
for(accum = cache; arcbuf < arcend; arcbuf++)
accum = (accum << 7) | (*arcbuf & ~0x80);
- if(accum < (unsigned)-add) {
+ if(accum < (unsigned)-add
+ || accum > (ULONG_MAX-(unsigned long)(-add))) {
errno = ERANGE; /* Overflow */
return -1;
}
- *(unsigned long *)(void *)rvbuf = accum + add; /* alignment OK! */
+ *(unsigned long *)(void *)rvbuf =
+ accum - (unsigned long)(-add); /* alignment OK! */
return 0;
}
diff --git a/tests/tests-skeletons/check-OIDs.c b/tests/tests-skeletons/check-OIDs.c
index 92c27d5..dac2193 100644
--- a/tests/tests-skeletons/check-OIDs.c
+++ b/tests/tests-skeletons/check-OIDs.c
@@ -13,14 +13,14 @@
}
static void
-check_OID(uint8_t *buf, size_t len, int *ck_buf, int ck_len) {
+check_OID(int lineno, uint8_t *buf, size_t len, int *ck_buf, int ck_len) {
OBJECT_IDENTIFIER_t *oid;
asn_dec_rval_t rval;
unsigned long arcs[10];
int alen;
int i;
- printf("Checking {");
+ printf("%03d: Checking {", lineno);
for(i = 0; i < (int)len; i++) { printf("%s%02x", i?" ":"", buf[i]); }
printf("} against {");
for(i = 0; i < ck_len; i++) { printf("%s%d", i?" ":"", ck_buf[i]); }
@@ -60,14 +60,14 @@
}
static void
-check_ROID(uint8_t *buf, size_t len, int *ck_buf, int ck_len) {
+check_ROID(int lineno, uint8_t *buf, size_t len, int *ck_buf, int ck_len) {
RELATIVE_OID_t *oid;
asn_dec_rval_t rval;
unsigned long arcs[10];
int alen;
int i;
- printf("Checking {");
+ printf("%03d: Checking {", lineno);
for(i = 0; i < (ssize_t)len; i++) { printf("%s%02x", i?" ":"", buf[i]); }
printf("} against {");
for(i = 0; i < ck_len; i++) { printf("%s%d", i?" ":"", ck_buf[i]); }
@@ -107,7 +107,7 @@
* Encode the specified array of arcs as RELATIVE-OID, decode it and compare.
*/
static void
-check_REGEN(int *arcs, int acount) {
+check_REGEN(int lineno, int *arcs, int acount) {
static RELATIVE_OID_t oid;
unsigned long tmp_arcs[10];
int tmp_alen = 10;
@@ -116,7 +116,7 @@
int i;
if(0) {
- fprintf(stderr, "Encoding (R) {");
+ fprintf(stderr, "%03d: Encoding (R) {", lineno);
for(i = 0; i < acount; i++) {
fprintf(stderr, " %u", arcs[i]);
}
@@ -149,7 +149,7 @@
* decode it and compare.
*/
static void
-check_REGEN_OID(int *arcs, int acount) {
+check_REGEN_OID(int lineno, int *arcs, int acount) {
static OBJECT_IDENTIFIER_t oid;
unsigned long tmp_arcs[10];
int tmp_alen = 10;
@@ -158,7 +158,7 @@
int i;
if(0) {
- fprintf(stderr, "Encoding (O) {");
+ fprintf(stderr, "%03d: Encoding (O) {", lineno);
for(i = 0; i < acount; i++) {
fprintf(stderr, " %u", arcs[i]);
}
@@ -274,16 +274,18 @@
assert(ret == expect_arcs);
}
-#define CHECK_OID(n) check_OID(buf ## n, sizeof(buf ## n), \
- buf ## n ## _check, \
- sizeof(buf ## n ## _check)/sizeof(buf ## n ## _check[0]))
-#define CHECK_ROID(n) check_ROID(buf ## n, sizeof(buf ## n), \
- buf ## n ## _check, \
- sizeof(buf ## n ## _check)/sizeof(buf ## n ## _check[0]))
-#define CHECK_REGEN(n) check_REGEN(buf ## n ## _check, \
- sizeof(buf ## n ## _check)/sizeof(buf ## n ## _check[0]))
-#define CHECK_REGEN_OID(n) check_REGEN_OID(buf ## n ## _check, \
- sizeof(buf ## n ## _check)/sizeof(buf ## n ## _check[0]))
+#define CHECK_OID(n) \
+ check_OID(__LINE__, buf##n, sizeof(buf##n), buf##n##_check, \
+ sizeof(buf##n##_check) / sizeof(buf##n##_check[0]))
+#define CHECK_ROID(n) \
+ check_ROID(__LINE__, buf##n, sizeof(buf##n), buf##n##_check, \
+ sizeof(buf##n##_check) / sizeof(buf##n##_check[0]))
+#define CHECK_REGEN(n) \
+ check_REGEN(__LINE__, buf##n##_check, \
+ sizeof(buf##n##_check) / sizeof(buf##n##_check[0]))
+#define CHECK_REGEN_OID(n) \
+ check_REGEN_OID(__LINE__, buf##n##_check, \
+ sizeof(buf##n##_check) / sizeof(buf##n##_check[0]))
int
main() {