Skip to content

Commit

Permalink
fixed memory leaks in test code
Browse files Browse the repository at this point in the history
  • Loading branch information
vlm committed Sep 19, 2017
1 parent dcae9ce commit 229ad00
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 22 deletions.
1 change: 1 addition & 0 deletions tests/tests-skeletons/check-GeneralizedTime.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ encode(time_t tloc, const char *expect, int force_gmt) {
} else {
assert(!expect);
}
ASN_STRUCT_FREE(asn_DEF_GeneralizedTime, gt);
}

#define RECODE(foo, bar) recode(__LINE__, foo, bar)
Expand Down
3 changes: 3 additions & 0 deletions tests/tests-skeletons/check-INTEGER.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ check(uint8_t *buf, size_t size, long check_long, int check_ret) {
ret = asn_INTEGER2long(&val, &rlong2);
assert(ret == 0);
assert(rlong == rlong2);
ASN_STRUCT_RESET(asn_DEF_INTEGER, &val2);
}

shared_scratch_start = scratch;
Expand Down Expand Up @@ -119,6 +120,7 @@ check_unsigned(uint8_t *buf, int size, unsigned long check_long, int check_ret)
ret = asn_INTEGER2ulong(&val, &rlong2);
assert(ret == 0);
assert(rlong == rlong2);
ASN_STRUCT_RESET(asn_DEF_INTEGER, &val2);
}

return;
Expand Down Expand Up @@ -155,6 +157,7 @@ check_xer(int tofail, char *xmldata, long orig_value) {
if(rc.code != RC_OK) {
assert(tofail);
printf("\tfailed, as expected\n");
ASN_STRUCT_FREE(asn_DEF_INTEGER, st);
return;
}
assert(!tofail);
Expand Down
3 changes: 3 additions & 0 deletions tests/tests-skeletons/check-OCTET_STRING.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ check_impl(int lineno, enum encoding_type type, char *tagname, char *xmlbuf, cha
} else {
assert(rc.code != RC_OK);
}

ASN_STRUCT_FREE(*td, st);
}

static char buf[1024];
Expand Down Expand Up @@ -78,6 +80,7 @@ encode(char *orig, char *encoded) {
printf("Orig: [%s], encoded: [%s], check [%s]\n",
orig, buf, encoded);
assert(strcmp(buf, encoded) == 0);
ASN_STRUCT_RESET(asn_DEF_OCTET_STRING, &os);
}

int
Expand Down
2 changes: 2 additions & 0 deletions tests/tests-skeletons/check-OER-INTEGER.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ check_decode(int lineno, enum asn_dec_rval_code_e code, intmax_t control, const
lineno, control, size);
if(ret.code == code) {
fprintf(stderr, " (That was expected)\n");
ASN_STRUCT_FREE(asn_DEF_INTEGER, st);
return;
} else {
fprintf(
Expand Down Expand Up @@ -76,6 +77,7 @@ check_decode(int lineno, enum asn_dec_rval_code_e code, intmax_t control, const
}

fprintf(stderr, "%d: Decode result %" PRIdMAX "\n", lineno, control);
ASN_STRUCT_FREE(asn_DEF_INTEGER, st);
}

static void
Expand Down
12 changes: 10 additions & 2 deletions tests/tests-skeletons/check-OIDs.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ check_OID(int lineno, uint8_t *buf, size_t len, int *ck_buf, int ck_len) {
printf(" }\n");
assert(alen == ck_len);

ASN_STRUCT_FREE(asn_DEF_OBJECT_IDENTIFIER, oid);
}

static void
Expand Down Expand Up @@ -101,6 +102,7 @@ check_ROID(int lineno, uint8_t *buf, size_t len, int *ck_buf, int ck_len) {
assert(arcs[i] == (unsigned long)ck_buf[i]);
}
printf(" }\n");
ASN_STRUCT_FREE(asn_DEF_RELATIVE_OID, oid);
}

/*
Expand Down Expand Up @@ -142,6 +144,7 @@ check_REGEN(int lineno, int *arcs, int acount) {
fprintf(stderr, "}\n");
}

ASN_STRUCT_RESET(asn_DEF_RELATIVE_OID, &oid);
}

/*
Expand Down Expand Up @@ -183,7 +186,10 @@ check_REGEN_OID(int lineno, int *arcs, int acount) {
}
fprintf(stderr, "}\n");
}

ASN_STRUCT_RESET(asn_DEF_RELATIVE_OID, &oid);
}

static int
check_speed() {
uint8_t buf[] = { 0x80 | 7, 0x80 | 2, 0x80 | 3, 0x80 | 4, 13 };
Expand Down Expand Up @@ -240,18 +246,18 @@ static void check_parse(const char *oid_txt, int retval) {
static void check_xer(int expect_arcs, char *xer) {
asn_dec_rval_t rc;
RELATIVE_OID_t *st = 0;
RELATIVE_OID_t **stp = &st;
long arcs[10];
int ret;
int i;

printf("[%s] => ", xer); fflush(stdout);
rc = asn_DEF_RELATIVE_OID.op->xer_decoder(0,
&asn_DEF_RELATIVE_OID, (void **)stp, "t",
&asn_DEF_RELATIVE_OID, (void **)&st, "t",
xer, strlen(xer));
if(expect_arcs == -1) {
if(rc.code != RC_OK) {
printf("-1\n");
ASN_STRUCT_FREE(asn_DEF_RELATIVE_OID, st);
return;
}
}
Expand All @@ -262,6 +268,7 @@ static void check_xer(int expect_arcs, char *xer) {
assert(ret < 10);
if(expect_arcs == -1) {
assert(ret == -1);
ASN_STRUCT_FREE(asn_DEF_RELATIVE_OID, st);
return;
}
for(i = 0; i < ret; i++) {
Expand All @@ -272,6 +279,7 @@ static void check_xer(int expect_arcs, char *xer) {
}
printf(": %d == %d\n", ret, expect_arcs);
assert(ret == expect_arcs);
ASN_STRUCT_FREE(asn_DEF_RELATIVE_OID, st);
}

#define CHECK_OID(n) \
Expand Down
2 changes: 2 additions & 0 deletions tests/tests-skeletons/check-PER-INTEGER.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ check_per_encode_constrained(int lineno, int unsigned_, long value, long lbound,
asn_INTEGER2long(reconstructed_st, &reconstructed_value);
assert(reconstructed_value == value);
}
ASN_STRUCT_RESET(asn_DEF_INTEGER, &st);
ASN_STRUCT_FREE(asn_DEF_INTEGER, reconstructed_st);
}

#define CHECK(u, v, l, r, b) \
Expand Down
49 changes: 29 additions & 20 deletions tests/tests-skeletons/check-REAL.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,9 @@ check_xer(int fuzzy, double orig_value) {

reconstr_lens[0] = 0;
reconstr_lens[1] = 0;
er = xer_encode(&asn_DEF_REAL, &st,
XER_F_BASIC, callback, 0);
er = xer_encode(&asn_DEF_REAL, &st, XER_F_BASIC, callback, 0);
assert(er.encoded == reconstr_lens[0]);
er = xer_encode(&asn_DEF_REAL, &st,
XER_F_CANONICAL, callback, (void *)1);
er = xer_encode(&asn_DEF_REAL, &st, XER_F_CANONICAL, callback, (void *)1);
assert(er.encoded == reconstr_lens[1]);
reconstructed[0][reconstr_lens[0]] = 0;
reconstructed[1][reconstr_lens[1]] = 0;
Expand Down Expand Up @@ -164,6 +162,9 @@ check_xer(int fuzzy, double orig_value) {
assert(newst1->size == st.size);
assert(fuzzy || memcmp(newst0->buf, st.buf, st.size) == 0);
assert(memcmp(newst1->buf, st.buf, st.size) == 0);
ASN_STRUCT_RESET(asn_DEF_REAL, &st);
ASN_STRUCT_FREE(asn_DEF_REAL, newst0);
ASN_STRUCT_FREE(asn_DEF_REAL, newst1);
}

static void
Expand Down Expand Up @@ -200,49 +201,56 @@ check_ber_buffer_twoway(double d, const char *sample, const char *canonical_samp
assert((size_t)rn.size == outsize);
}
assert(memcmp(rn.buf, outbuf, rn.size) == 0);
ASN_STRUCT_RESET(asn_DEF_REAL, &rn);

check_str_representation(d, sample, canonical_sample, lineno);
}

static void
check_ber_buffer_oneway(double d, const char *sample, const char *canonical_sample, uint8_t *buf, size_t bufsize, int lineno) {
REAL_t rn;
double val;
REAL_t rn0;
REAL_t rn1;
double val0;
double val1;
uint8_t *p, *end;
int ret;

memset(&rn, 0, sizeof(rn));
memset(&rn0, 0, sizeof(rn0));
memset(&rn1, 0, sizeof(rn1));

printf("verify double value %.12f [", d);
for(p = (uint8_t *)&d, end = p + sizeof(double); p < end ; p++)
printf("%02x", *p);
printf("] (ilogb %d)\n", ilogb(d));


ret = asn_double2REAL(&rn, d);
ret = asn_double2REAL(&rn0, d);
assert(ret == 0);

printf("canonical DER: [");
for(p = rn.buf, end = p + rn.size; p < end; p++)
for(p = rn0.buf, end = p + rn0.size; p < end; p++)
printf("%02x", *p);
printf("]\n");
ret = asn_REAL2double(&rn0, &val0);
assert(ret == 0);
printf("] => %f\n", val0);

rn.buf = buf;
rn.size = bufsize;
rn1.buf = buf;
rn1.size = bufsize;

printf("received as: [");
for(p = rn.buf, end = p + rn.size; p < end; p++)
for(p = rn1.buf, end = p + rn1.size; p < end; p++)
printf("%02x", *p);
printf("]\n");

ret = asn_REAL2double(&rn, &val);
ret = asn_REAL2double(&rn1, &val1);
assert(ret == 0);
printf("] => %f\n", val1);

printf("%.12f vs %.12f\n", d, val);
printf("%.12f vs %.12f vs %.12f\n", d, val0, val1);

assert(val == d);
assert(val0 == d);
assert(val1 == d);

check_str_representation(val, sample, canonical_sample, lineno);
ASN_STRUCT_RESET(asn_DEF_REAL, &rn0);

check_str_representation(val1, sample, canonical_sample, lineno);
}

/*
Expand Down Expand Up @@ -696,5 +704,6 @@ main() {
check_xer(1, 0.0000000000000000000001);
check_xer(1, -0.0000000000000000000001);

ASN_STRUCT_RESET(asn_DEF_REAL, &rn);
return 0;
}

0 comments on commit 229ad00

Please sign in to comment.