MikePopoloski / slang

SystemVerilog compiler and language services
MIT License
546 stars 114 forks source link

slang-netlist appends garbage at end of (short) .dot files #955

Closed udif closed 1 month ago

udif commented 1 month ago

@jameshanlon

Describe the bug When creating short .dot files, garbage binary data is appended to the output.

To Reproduce

udif@localhost:~/git/slang$ cat t19.v
module x;
endmodule
udif@localhost:~/git/slang$ slang-netlist --netlist-dot t19.dot t19.v
Top level design units:
    x

Build succeeded: 0 errors, 0 warnings
udif@localhost:~/git/slang$ od -xc t19.dot
0000000    6964    7267    7061    2068    0a7b    2020    6f6e    6564
          d   i   g   r   a   p   h       {  \n           n   o   d   e
0000020    5b20    6873    7061    3d65    6572    6f63    6472    3b5d
              [   s   h   a   p   e   =   r   e   c   o   r   d   ]   ;
0000040    7d0a    950a    7ffd
         \n   }  \n 225 375 177
0000046

Additional context As you can see, the t19.dot valid data is followed by 3 random binary bytes. Following the code, the source of the bug is because printDOT() is using slang::FormatBuffer buffer to collect output data. This buffer is not cleared when allocated, but has a size field instead. In the end of printDOT(), the buffer is printed by OS::writeFile(fileName, buffer.data());, but as far as I can tell, buffer.data() no longer carries the size, only a pointer to the data.