Skip to content

Commit

Permalink
fix: Correct fullspeed table size.
Browse files Browse the repository at this point in the history
Correct fullspeed table size computation from 'tblend + numecs + 1' to
'tblend + 2 + 1' to avoid allocating extra space that is never initialized.

Refs: westes#628
  • Loading branch information
Mightyjo committed Mar 4, 2024
1 parent cf9c659 commit e09102e
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/gen.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,14 @@ static struct yytbl_data *mkctbl (void)
flex_int32_t *tdata = 0, curr = 0;
int end_of_buffer_action = num_rules + 1;

struct packtype_t *ptype = optimize_pack(tblend + numecs + 1);
struct packtype_t *ptype = optimize_pack(tblend + 2 + 1);
out_str ("m4_define([[M4_HOOK_MKCTBL_TYPE]], [[%s]])", ptype->name);

tbl = calloc(1, sizeof (struct yytbl_data));
yytbl_data_init (tbl, YYTD_ID_TRANSITION);
tbl->td_flags = YYTD_DATA32 | YYTD_STRUCT;
tbl->td_hilen = 0;
tbl->td_lolen = (flex_uint32_t) (tblend + numecs + 1); /* number of structs */
tbl->td_lolen = (flex_uint32_t) (tblend + 2 + 1); /* number of structs */

tbl->td_data = tdata =
calloc(tbl->td_lolen * 2, sizeof (flex_int32_t));
Expand Down Expand Up @@ -243,7 +243,7 @@ void genctbl (void)
int end_of_buffer_action = num_rules + 1;

/* Table of verify for transition and offset to next state. */
out_dec ("m4_define([[M4_HOOK_TRANSTABLE_SIZE]], [[%d]])", tblend + numecs + 1);
out_dec ("m4_define([[M4_HOOK_TRANSTABLE_SIZE]], [[%d]])", tblend + 2 + 1);
outn ("m4_define([[M4_HOOK_TRANSTABLE_BODY]], [[m4_dnl");

/* We want the transition to be represented as the offset to the
Expand Down Expand Up @@ -312,7 +312,7 @@ void genctbl (void)
transition_struct_out (chk[tblend + 2], nxt[tblend + 2]);

outn ("]])");
footprint += sizeof(struct yy_trans_info) * (tblend + numecs + 1);
footprint += sizeof(struct yy_trans_info) * (tblend + 2 + 1);

out_dec ("m4_define([[M4_HOOK_STARTTABLE_SIZE]], [[%d]])", lastsc * 2 + 1);

Expand Down Expand Up @@ -982,6 +982,14 @@ void make_tables (void)

snprintf(buf, sizeof(buf), "footprint: %ld bytes\n", footprint);
comment(buf);
snprintf(buf, sizeof(buf), "tblend: %d\n", tblend);
comment(buf);
snprintf(buf, sizeof(buf), "numecs: %d\n", numecs);
comment(buf);
snprintf(buf, sizeof(buf), "num_rules: %d\n", num_rules);
comment(buf);
snprintf(buf, sizeof(buf), "lastdfa: %d\n", lastdfa);
comment(buf);
outc ('\n');

// Only at this point do we know if the automaton has backups.
Expand Down

0 comments on commit e09102e

Please sign in to comment.