From 9bcca3ac21176e1ff54001206b05c2488ccca558 Mon Sep 17 00:00:00 2001 From: Tim Edwards Date: Wed, 3 Jun 2020 17:00:42 -0400 Subject: [PATCH] Corrected the verilog parsing (yet again!) so that it does not mistakenly flag bus delimiter characters inside backslash-escaped names when looking for bus delimiters. --- VERSION | 2 +- base/netfile.c | 10 ++++----- base/verilog.c | 57 ++++++++++++++++++++++++++++++++++++-------------- 3 files changed, 47 insertions(+), 22 deletions(-) diff --git a/VERSION b/VERSION index c995b25..ebc3416 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.5.146 +1.5.147 diff --git a/base/netfile.c b/base/netfile.c index 9413148..e6f13d8 100644 --- a/base/netfile.c +++ b/base/netfile.c @@ -301,20 +301,20 @@ int GetNextLineNoNewline(char *delimiter) if (linesize == 0) { /* Allocate memory for line */ - linesize = 500; - line = (char *)MALLOC(linesize); - linetok = (char *)MALLOC(linesize); + linesize = 2000; + line = (char *)MALLOC(linesize + 1); + linetok = (char *)MALLOC(linesize + 1); } fgets(line, linesize, infile); while (strlen(line) == linesize - 1) { - newbuf = (char *)MALLOC(linesize + 500); + newbuf = (char *)MALLOC(linesize + 501); strcpy(newbuf, line); FREE(line); line = newbuf; fgets(line + linesize - 1, 501, infile); linesize += 500; FREE(linetok); - linetok = (char *)MALLOC(linesize); + linetok = (char *)MALLOC(linesize + 1); } /* Check for substitutions (verilog only). Make sure linetok is */ diff --git a/base/verilog.c b/base/verilog.c index ca3de18..aba10e4 100644 --- a/base/verilog.c +++ b/base/verilog.c @@ -98,6 +98,31 @@ struct bus *NewBus() return (wb); } +//------------------------------------------------------------------------- +// Find a character c in a string, assuming that string may contain +// verilog names, where anything, including char c, may appear in the +// string if it is a backslash-escaped name. Only the position of +// character c outside of a verilog name is reported. +//------------------------------------------------------------------------- + +char *strvchr(char *string, char c) +{ + char *s; + + for (s = string; *s != '\0'; s++) { + if (*s == '\\') { + while (*s != '\0' && *s != ' ') s++; + if (*s == '\0') { + Fprintf(stderr, "Error: Verilog backslash-escaped name" + " does not end with a space.\n"); + break; + } + } + if (*s == c) return s; + } + return NULL; +} + //------------------------------------------------------------------------- // Get bus indexes from the notation name[a:b]. If there is only "name" // then look up the name in the bus hash list and return the index bounds. @@ -302,8 +327,8 @@ int GetBus(char *astr, struct bus *wb) astr++; wb->end = 0; while((*astr != '\0') && (*astr != '}')) { - sigend = strchr(astr, ','); - if (sigend == NULL) sigend = strchr(astr, '}'); + sigend = strvchr(astr, ','); + if (sigend == NULL) sigend = strvchr(astr, '}'); if (sigend == NULL) { Printf("Badly formed wire bundle \"%s\"\n", astr - 1); return 1; @@ -332,15 +357,15 @@ int GetBus(char *astr, struct bus *wb) while (*aastr != ' ' && *aastr != '\\' && *aastr != '\0') aastr++; } - brackstart = strchr(aastr, '['); + brackstart = strvchr(aastr, '['); if (brackstart != NULL) { - brackend = strchr(aastr, ']'); + brackend = strvchr(aastr, ']'); if (brackend == NULL) { Printf("Badly formed array notation \"%s\"\n", astr); return 1; } *brackend = '\0'; - colonptr = strchr(aastr, ':'); + colonptr = strvchr(aastr, ':'); if (colonptr) *colonptr = '\0'; result = sscanf(brackstart + 1, "%d", &start); if (colonptr) *colonptr = ':'; @@ -1184,7 +1209,7 @@ skip_endmodule: else { /* "assign" */ SkipTokComments(VLOG_PIN_CHECK_DELIMITERS); if (GetBus(nexttok, &wb) == 0) { - char *aptr = strchr(nexttok, '['); + char *aptr = strvchr(nexttok, '['); if (aptr != NULL) { *aptr = '\0'; /* Find object of first net in bus */ @@ -1238,7 +1263,7 @@ skip_endmodule: } else { if (GetBus(nexttok, &wb2) == 0) { - char *aptr = strchr(nexttok, '['); + char *aptr = strvchr(nexttok, '['); j = wb2.start; if (aptr != NULL) { *aptr = '\0'; @@ -1699,7 +1724,7 @@ nextinst: for (bobj = CurrentCell->cell; bobj; bobj = bobj->next) { if (bobj->type == PORT) { - if ((bptr = strchr(bobj->name, '[')) != NULL) { + if ((bptr = strvchr(bobj->name, '[')) != NULL) { *bptr = '\0'; if (!strcmp(bobj->name, scan->net)) { *bptr = '['; @@ -1711,7 +1736,7 @@ nextinst: } } else if (bobj->type == NODE) { - if ((bptr = strchr(bobj->name, '[')) != NULL) { + if ((bptr = strvchr(bobj->name, '[')) != NULL) { *bptr = '\0'; if (!strcmp(bobj->name, scan->net)) { if (sscanf(bptr + 1, "%d", &testidx) == 1) { @@ -1772,8 +1797,8 @@ nextinst: if (*netname == '{') { is_bundle = 1; netname++; - cptr = strchr(netname, ','); - if (cptr == NULL) cptr = strchr(netname, '}'); + cptr = strvchr(netname, ','); + if (cptr == NULL) cptr = strvchr(netname, '}'); if (cptr == NULL) cptr = netname + strlen(netname) - 1; cchar = *cptr; *cptr = '\0'; @@ -1782,7 +1807,7 @@ nextinst: // Remove indexed part of scan->net if (GetBus(netname, &wbb) == 0) { i = wbb.start; - if ((bptr = strchr(netname, '[')) != NULL) + if ((bptr = strvchr(netname, '[')) != NULL) *bptr = '\0'; } else @@ -1825,15 +1850,15 @@ nextinst: netname = cptr + 1; if (cptr) *cptr = cchar; /* Restore previous bundle delimiter */ - cptr = strchr(netname, ','); - if (cptr == NULL) cptr = strchr(netname, '}'); + cptr = strvchr(netname, ','); + if (cptr == NULL) cptr = strvchr(netname, '}'); if (cptr == NULL) cptr = netname + strlen(netname) - 1; cchar = *cptr; *cptr = '\0'; if (GetBus(netname, &wbb) == 0) { i = wbb.start; - if ((bptr = strchr(netname, '[')) != NULL) + if ((bptr = strvchr(netname, '[')) != NULL) *bptr = '\0'; } else i = -1; @@ -1901,7 +1926,7 @@ nextinst: char *bptr2; char *scanroot; scanroot = strsave(scan->net); - brackptr = strchr(scanroot, '['); + brackptr = strvchr(scanroot, '['); if (brackptr) *brackptr = '\0'; if (arraystart == -1) {