asn1: Fix leak in asn1_print

This commit is contained in:
Nicolas Williams
2021-03-02 20:56:16 -06:00
parent f780bdc244
commit 32974ac421

View File

@@ -65,7 +65,7 @@ static unsigned long indefinite_form_loop_max = 10000;
typedef int (*decoder)(const unsigned char *, size_t, void *, size_t *); typedef int (*decoder)(const unsigned char *, size_t, void *, size_t *);
typedef char *(*printer)(const void *, int); typedef char *(*printer)(const void *, int);
typedef void (*releaser)(void *); typedef void (*releaser)(void *);
struct types { const struct types {
const char *name; const char *name;
decoder decode; decoder decode;
printer print; printer print;
@@ -97,6 +97,8 @@ struct types {
#include "x690sample_asn1_syms.x" #include "x690sample_asn1_syms.x"
}; };
struct types sorted_types[sizeof(types)/sizeof(types[0])];
static size_t static size_t
loop (unsigned char *buf, size_t len, int indent) loop (unsigned char *buf, size_t len, int indent)
{ {
@@ -350,7 +352,6 @@ type_cmp(const void *va, const void *vb)
static int static int
dotype(unsigned char *buf, size_t len, char **argv) dotype(unsigned char *buf, size_t len, char **argv)
{ {
struct types *sorted_types = emalloc(sizeof(types));
const char *typename; const char *typename;
size_t matches = 0; size_t matches = 0;
size_t sz; size_t sz;
@@ -397,12 +398,12 @@ dotype(unsigned char *buf, size_t len, char **argv)
fprintf(stderr, "Match: %s\n", typename); fprintf(stderr, "Match: %s\n", typename);
else else
fprintf(stderr, "Prefix match: %s\n", typename); fprintf(stderr, "Prefix match: %s\n", typename);
s = sorted_types[i].print(v, s = sorted_types[i].print(v, indent_flag ? ASN1_PRINT_INDENT : 0);
indent_flag ? ASN1_PRINT_INDENT : 0);
sorted_types[i].release(v); sorted_types[i].release(v);
if (!s) if (!s)
ret = errno; ret = errno;
} }
free(v);
if (ret == 0) { if (ret == 0) {
fprintf(stdout, "%s\n", s); fprintf(stdout, "%s\n", s);
@@ -488,7 +489,6 @@ dotype(unsigned char *buf, size_t len, char **argv)
err(1, "Could not decode and print data as type %s", typename); err(1, "Could not decode and print data as type %s", typename);
} }
} }
free(buf);
free(s); free(s);
return 0; return 0;
} }