get rid of undefined behavior

This commit is contained in:
Lev Walkin 2017-09-17 23:16:38 -07:00
parent beedbdeb6e
commit 0995f351ec
2 changed files with 26 additions and 21 deletions

View File

@ -80,7 +80,6 @@ OBJECT_IDENTIFIER_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
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 @@ OBJECT_IDENTIFIER_get_single_arc(const uint8_t *arcbuf, unsigned int arclen, sig
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 @@ OBJECT_IDENTIFIER_get_single_arc(const uint8_t *arcbuf, unsigned int arclen, sig
/* 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;
}

View File

@ -13,14 +13,14 @@ _print(const void *buffer, size_t size, void *app_key) {
}
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 @@ check_OID(uint8_t *buf, size_t len, int *ck_buf, int ck_len) {
}
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 @@ check_ROID(uint8_t *buf, size_t len, int *ck_buf, int ck_len) {
* 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 @@ check_REGEN(int *arcs, int acount) {
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 @@ check_REGEN(int *arcs, int acount) {
* 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 @@ check_REGEN_OID(int *arcs, int acount) {
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 @@ static void check_xer(int expect_arcs, char *xer) {
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() {