diff options
author | Tristan Gingold <tgingold@free.fr> | 2017-06-06 08:01:20 +0200 |
---|---|---|
committer | Tristan Gingold <tgingold@free.fr> | 2017-06-06 08:01:20 +0200 |
commit | be0bbfc67b73988ed28319fd6173107cfee0449f (patch) | |
tree | 8a66f2a6da5e4a5b6235f90b3d94df5c8aac69ee | |
parent | 990f637cac18d53854d0c67015b1c921f18d5e3d (diff) | |
download | ghdl-be0bbfc67b73988ed28319fd6173107cfee0449f.tar.gz ghdl-be0bbfc67b73988ed28319fd6173107cfee0449f.tar.bz2 ghdl-be0bbfc67b73988ed28319fd6173107cfee0449f.zip |
ghwlib: fix sign issues, leaks, base types.
-rw-r--r-- | src/grt/ghwlib.c | 148 | ||||
-rw-r--r-- | src/grt/ghwlib.h | 14 |
2 files changed, 105 insertions, 57 deletions
diff --git a/src/grt/ghwlib.c b/src/grt/ghwlib.c index ddfab6b57..5bb867716 100644 --- a/src/grt/ghwlib.c +++ b/src/grt/ghwlib.c @@ -125,7 +125,7 @@ ghw_read_byte (struct ghw_handler *h, unsigned char *res) int ghw_read_uleb128 (struct ghw_handler *h, uint32_t *res) { - unsigned int r = 0; + uint32_t r = 0; unsigned int off = 0; while (1) @@ -203,7 +203,8 @@ ghw_read_f64 (struct ghw_handler *h, double *res) const char * ghw_read_strid (struct ghw_handler *h) { - unsigned int id; + uint32_t id; + if (ghw_read_uleb128 (h, &id) != 0) return NULL; return h->str_table[id]; @@ -212,7 +213,8 @@ ghw_read_strid (struct ghw_handler *h) union ghw_type * ghw_read_typeid (struct ghw_handler *h) { - unsigned int id; + uint32_t id; + if (ghw_read_uleb128 (h, &id) != 0) return NULL; return h->types[id - 1]; @@ -233,10 +235,13 @@ ghw_read_range (struct ghw_handler *h) r->kind = t & 0x7f; r->dir = (t & 0x80) != 0; if (ghw_read_byte (h, &r->left) != 0) - return NULL; + goto err_b2; if (ghw_read_byte (h, &r->right) != 0) - return NULL; + goto err_b2; return (union ghw_range *)r; + err_b2: + free (r); + return NULL; } case ghdl_rtik_type_e8: { @@ -245,10 +250,13 @@ ghw_read_range (struct ghw_handler *h) r->kind = t & 0x7f; r->dir = (t & 0x80) != 0; if (ghw_read_byte (h, &r->left) != 0) - return NULL; + goto err_e8; if (ghw_read_byte (h, &r->right) != 0) - return NULL; + goto err_e8; return (union ghw_range *)r; + err_e8: + free (r); + return NULL; } case ghdl_rtik_type_i32: case ghdl_rtik_type_p32: @@ -258,10 +266,13 @@ ghw_read_range (struct ghw_handler *h) r->kind = t & 0x7f; r->dir = (t & 0x80) != 0; if (ghw_read_sleb128 (h, &r->left) != 0) - return NULL; + goto err_i32; if (ghw_read_sleb128 (h, &r->right) != 0) - return NULL; + goto err_i32; return (union ghw_range *)r; + err_i32: + free (r); + return NULL; } case ghdl_rtik_type_i64: case ghdl_rtik_type_p64: @@ -271,10 +282,13 @@ ghw_read_range (struct ghw_handler *h) r->kind = t & 0x7f; r->dir = (t & 0x80) != 0; if (ghw_read_lsleb128 (h, &r->left) != 0) - return NULL; + goto err_i64; if (ghw_read_lsleb128 (h, &r->right) != 0) - return NULL; + goto err_i64; return (union ghw_range *)r; + err_i64: + free (r); + return NULL; } case ghdl_rtik_type_f64: { @@ -283,10 +297,13 @@ ghw_read_range (struct ghw_handler *h) r->kind = t & 0x7f; r->dir = (t & 0x80) != 0; if (ghw_read_f64 (h, &r->left) != 0) - return NULL; + goto err_f64; if (ghw_read_f64 (h, &r->right) != 0) - return NULL; + goto err_f64; return (union ghw_range *)r; + err_f64: + free (r); + return NULL; } default: fprintf (stderr, "ghw_read_range: type %d unhandled\n", t & 0x7f); @@ -298,7 +315,7 @@ int ghw_read_str (struct ghw_handler *h) { unsigned char hdr[12]; - int i; + unsigned i; char *p; int prev_len; @@ -421,8 +438,8 @@ get_nbr_elements (union ghw_type *t) } } -static int -get_range_length (union ghw_range *rng) +int +ghw_get_range_length (union ghw_range *rng) { switch (rng->kind) { @@ -453,7 +470,7 @@ struct ghw_subtype_array * ghw_read_array_subtype (struct ghw_handler *h, struct ghw_type_array *base) { struct ghw_subtype_array *sa; - int j; + unsigned j; int nbr_scalars; sa = malloc (sizeof (struct ghw_subtype_array)); @@ -489,7 +506,7 @@ ghw_read_record_subtype (struct ghw_handler *h, struct ghw_type_record *base) else { /* Read subtypes. */ - int j; + unsigned j; int nbr_scalars; sr->els = malloc (base->nbr_fields * sizeof (struct ghw_record_element)); @@ -536,7 +553,7 @@ int ghw_read_type (struct ghw_handler *h) { unsigned char hdr[8]; - int i; + unsigned i; if (fread (hdr, sizeof (hdr), 1, h->stream) != 1) return -1; @@ -561,14 +578,14 @@ ghw_read_type (struct ghw_handler *h) case ghdl_rtik_type_e8: { struct ghw_type_enum *e; - int j; + unsigned j; e = malloc (sizeof (struct ghw_type_enum)); e->kind = t; e->wkt = ghw_wkt_unknown; e->name = ghw_read_strid (h); if (ghw_read_uleb128 (h, &e->nbr) != 0) - return -1; + goto err_b2; e->lits = (const char **) malloc (e->nbr * sizeof (char *)); if (h->flag_verbose > 1) printf ("enum %s:", e->name); @@ -581,6 +598,10 @@ ghw_read_type (struct ghw_handler *h) if (h->flag_verbose > 1) printf ("\n"); h->types[i] = (union ghw_type *)e; + break; + err_b2: + free (e); + return -1; } break; case ghdl_rtik_type_i32: @@ -605,25 +626,31 @@ ghw_read_type (struct ghw_handler *h) ph = malloc (sizeof (struct ghw_type_physical)); ph->kind = t; ph->name = ghw_read_strid (h); + ph->units = NULL; if (h->version == 0) ph->nbr_units = 0; else { - int i; + unsigned i; if (ghw_read_uleb128 (h, &ph->nbr_units) != 0) - return -1; + goto err_p32; ph->units = malloc (ph->nbr_units * sizeof (struct ghw_unit)); for (i = 0; i < ph->nbr_units; i++) { ph->units[i].name = ghw_read_strid (h); if (ghw_read_lsleb128 (h, &ph->units[i].val) < 0) - return -1; + goto err_p32; } } if (h->flag_verbose > 1) printf ("physical: %s\n", ph->name); h->types[i] = (union ghw_type *)ph; + break; + err_p32: + free (ph->units); + free (ph); + return -1; } break; case ghdl_rtik_subtype_scalar: @@ -643,14 +670,14 @@ ghw_read_type (struct ghw_handler *h) case ghdl_rtik_type_array: { struct ghw_type_array *arr; - int j; + unsigned j; arr = malloc (sizeof (struct ghw_type_array)); arr->kind = t; arr->name = ghw_read_strid (h); arr->el = ghw_read_typeid (h); if (ghw_read_uleb128 (h, &arr->nbr_dim) != 0) - return -1; + goto err_array; arr->dims = (union ghw_type **) malloc (arr->nbr_dim * sizeof (union ghw_type *)); for (j = 0; j < arr->nbr_dim; j++) @@ -658,6 +685,10 @@ ghw_read_type (struct ghw_handler *h) if (h->flag_verbose > 1) printf ("array: %s\n", arr->name); h->types[i] = (union ghw_type *)arr; + break; + err_array: + free (arr); + return -1; } break; case ghdl_rtik_subtype_array: @@ -680,14 +711,15 @@ ghw_read_type (struct ghw_handler *h) case ghdl_rtik_type_record: { struct ghw_type_record *rec; - int j; + unsigned j; int nbr_scalars; rec = malloc (sizeof (struct ghw_type_record)); rec->kind = t; rec->name = ghw_read_strid (h); + rec->els = NULL; if (ghw_read_uleb128 (h, &rec->nbr_fields) != 0) - return -1; + goto err_record; rec->els = malloc (rec->nbr_fields * sizeof (struct ghw_record_element)); nbr_scalars = 0; @@ -709,6 +741,11 @@ ghw_read_type (struct ghw_handler *h) printf ("record type: %s (nbr_scalars=%d)\n", rec->name, rec->nbr_scalars); h->types[i] = (union ghw_type *)rec; + break; + err_record: + free (rec->els); + free (rec); + return NULL; } break; case ghdl_rtik_subtype_record: @@ -775,6 +812,7 @@ ghw_read_wk_types (struct ghw_handler *h) void ghw_disp_typename (struct ghw_handler *h, union ghw_type *t) { + (void)h; printf ("%s", t->common.name); } @@ -796,7 +834,7 @@ ghw_read_signal (struct ghw_handler *h, unsigned int *sigs, union ghw_type *t) return -1; *sigs = sig_el; if (sig_el == 0 || sig_el >= h->nbr_sigs) - abort (); + return -1; if (h->sigs[sig_el].type == NULL) h->sigs[sig_el].type = ghw_get_base_type (t); } @@ -916,7 +954,7 @@ ghw_read_hie (struct ghw_handler *h) unsigned char hdr[16]; int nbr_scopes; int nbr_sigs; - int i; + unsigned i; struct ghw_hie *blk; struct ghw_hie **last; @@ -1035,6 +1073,8 @@ ghw_read_hie (struct ghw_handler *h) el->u.sig.type = ghw_read_typeid (h); nbr_el = get_nbr_elements (el->u.sig.type); + if (nbr_el < 0) + return -1; sigs = (unsigned int *) malloc ((nbr_el + 1) * sizeof (unsigned int)); el->u.sig.sigs = sigs; @@ -1047,9 +1087,9 @@ ghw_read_hie (struct ghw_handler *h) return -1; if (h->flag_verbose > 1) { - int i; - for (i = 0; i < nbr_el; i++) - printf (" #%u", sigs[i]); + int j; + for (j = 0; j < nbr_el; j++) + printf (" #%u", sigs[j]); printf ("]\n"); } } @@ -1257,6 +1297,7 @@ ghw_disp_hie (struct ghw_handler *h, struct ghw_hie *top) int ghw_read_eoh (struct ghw_handler *h) { + (void)h; return 0; } @@ -1304,7 +1345,7 @@ int ghw_read_snapshot (struct ghw_handler *h) { unsigned char hdr[12]; - int i; + unsigned i; struct ghw_sig *s; if (fread (hdr, sizeof (hdr), 1, h->stream) != 1) @@ -1418,16 +1459,16 @@ ghw_read_cycle_end (struct ghw_handler *h) } static const char * -ghw_get_lit (union ghw_type *type, int e) +ghw_get_lit (union ghw_type *type, unsigned e) { - if (e >= type->en.nbr || e < 0) + if (e >= type->en.nbr) return "??"; else return type->en.lits[e]; } static void -ghw_disp_lit (union ghw_type *type, int e) +ghw_disp_lit (union ghw_type *type, unsigned e) { printf ("%s (%d)", ghw_get_lit (type, e), e); } @@ -1465,12 +1506,14 @@ ghw_disp_value (union ghw_val *val, union ghw_type *type) void ghw_get_value (char *buf, int len, union ghw_val *val, union ghw_type *type) { - switch (ghw_get_base_type (type)->kind) + union ghw_type *base = ghw_get_base_type (type); + + switch (base->kind) { case ghdl_rtik_type_b2: if (val->b2 <= 1) { - strncpy (buf, type->en.lits[val->b2], len - 1); + strncpy (buf, base->en.lits[val->b2], len - 1); buf[len - 1] = 0; } else @@ -1479,9 +1522,9 @@ ghw_get_value (char *buf, int len, union ghw_val *val, union ghw_type *type) } break; case ghdl_rtik_type_e8: - if (val->b2 <= type->en.nbr) + if (val->b2 <= base->en.nbr) { - strncpy (buf, type->en.lits[val->e8], len - 1); + strncpy (buf, base->en.lits[val->e8], len - 1); buf[len - 1] = 0; } else @@ -1521,7 +1564,8 @@ void ghw_filter_signals (struct ghw_handler *h, int *signals_to_keep, int nb_signals_to_keep) { - int i; + unsigned i; + if (0 < nb_signals_to_keep && 0 != signals_to_keep) { if (0 == h->skip_sigs) @@ -1547,7 +1591,7 @@ ghw_filter_signals (struct ghw_handler *h, void ghw_disp_values (struct ghw_handler *h) { - int i; + unsigned i; for (i = 0; i < h->nbr_sigs; i++) { struct ghw_sig *s = &h->sigs[i]; @@ -1804,7 +1848,7 @@ int ghw_read_section (struct ghw_handler *h) { unsigned char hdr[4]; - int i; + unsigned i; if (fread (hdr, sizeof (hdr), 1, h->stream) != 1) { @@ -1874,8 +1918,8 @@ ghw_disp_range (union ghw_type *type, union ghw_range *rng) static void ghw_disp_array_subtype_bounds (struct ghw_subtype_array *a) { - int i; - + unsigned i; + printf (" ("); for (i = 0; i < a->base->nbr_dim; i++) { @@ -1891,7 +1935,7 @@ ghw_disp_record_subtype_bounds (struct ghw_subtype_record *sr) { struct ghw_type_record *base = sr->base; int is_first = 1; - int i; + unsigned i; for (i = 0; i < base->nbr_fields; i++) { @@ -1984,7 +2028,7 @@ ghw_disp_type (struct ghw_handler *h, union ghw_type *t) case ghdl_rtik_type_e8: { struct ghw_type_enum *e = &t->en; - int i; + unsigned i; printf ("type %s is (", e->name); for (i = 0; i < e->nbr; i++) @@ -2009,7 +2053,7 @@ ghw_disp_type (struct ghw_handler *h, union ghw_type *t) case ghdl_rtik_type_p32: case ghdl_rtik_type_p64: { - int i; + unsigned i; struct ghw_type_physical *p = &t->ph; printf ("type %s is range <> units\n", p->name); @@ -2024,7 +2068,7 @@ ghw_disp_type (struct ghw_handler *h, union ghw_type *t) case ghdl_rtik_type_array: { struct ghw_type_array *a = &t->ar; - int i; + unsigned i; printf ("type %s is array (", a->name); for (i = 0; i < a->nbr_dim; i++) @@ -2042,7 +2086,7 @@ ghw_disp_type (struct ghw_handler *h, union ghw_type *t) case ghdl_rtik_type_record: { struct ghw_type_record *r = &t->rec; - int i; + unsigned i; printf ("type %s is record\n", r->name); for (i = 0; i < r->nbr_fields; i++) @@ -2072,7 +2116,7 @@ ghw_disp_type (struct ghw_handler *h, union ghw_type *t) void ghw_disp_types (struct ghw_handler *h) { - int i; + unsigned i; for (i = 0; i < h->nbr_types; i++) if (h->flag_verbose || !ghw_is_anonymous_type (h, h->types[i])) diff --git a/src/grt/ghwlib.h b/src/grt/ghwlib.h index 5ee05953a..ad9f93c79 100644 --- a/src/grt/ghwlib.h +++ b/src/grt/ghwlib.h @@ -320,20 +320,20 @@ struct ghw_handler /* String table. */ /* Number of strings. */ - int nbr_str; + unsigned nbr_str; /* Size of the strings (without nul). */ - int str_size; + unsigned str_size; /* String table. */ char **str_table; /* Array containing strings. */ char *str_content; /* Type table. */ - int nbr_types; + unsigned nbr_types; union ghw_type **types; /* Non-composite (or basic) signals. */ - int nbr_sigs; + unsigned nbr_sigs; char *skip_sigs; int flag_full_names; struct ghw_sig *sigs; @@ -349,11 +349,15 @@ struct ghw_handler Return < 0 in case of error. */ int ghw_open (struct ghw_handler *h, const char *filename); +/* Return base type of T. */ union ghw_type *ghw_get_base_type (union ghw_type *t); +/* Return length of RNG. */ +int ghw_get_range_length (union ghw_range *rng); + /* Put the ASCII representation of VAL into BUF, whose size if LEN. A NUL is always written to BUF. */ -void ghw_get_value (char *buf, int len, +void ghw_get_value (char *buf, int len, union ghw_val *val, union ghw_type *type); const char *ghw_get_hie_name (struct ghw_hie *h); |