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;
}