diff --git a/docs/CONTRIBUTORS b/docs/CONTRIBUTORS index a80d950b9..77e232945 100644 --- a/docs/CONTRIBUTORS +++ b/docs/CONTRIBUTORS @@ -33,6 +33,7 @@ Gianfranco Costamagna Glen Gibb Graham Rushton Guokai Chen +Gustav Svensk Harald Heckmann Howard Su Huang Rui diff --git a/include/verilated.cpp b/include/verilated.cpp index 7b9200363..49ef170dc 100644 --- a/include/verilated.cpp +++ b/include/verilated.cpp @@ -1459,11 +1459,12 @@ void VL_SFORMAT_X(int obits, void* destp, const char* formatp, ...) VL_MT_SAFE { void VL_SFORMAT_X(int obits_ignored, std::string& output, const char* formatp, ...) VL_MT_SAFE { if (obits_ignored) {} - output = ""; + std::string temp_output; va_list ap; va_start(ap, formatp); - _vl_vsformat(output, formatp, ap); + _vl_vsformat(temp_output, formatp, ap); va_end(ap); + output = temp_output; } std::string VL_SFORMATF_NX(const char* formatp, ...) VL_MT_SAFE { diff --git a/test_regress/t/t_sys_sformat.v b/test_regress/t/t_sys_sformat.v index 2e3443e5b..bf8bc8a5b 100644 --- a/test_regress/t/t_sys_sformat.v +++ b/test_regress/t/t_sys_sformat.v @@ -83,6 +83,12 @@ module t; $swriteo(str2, 4'd12); if (str2 != "14") $stop; + str3 = "foo"; + $sformat(str3, "%s", str3); // $sformat twice so verilator does not + $sformat(str3, "%s", str3); // optimize the call to $sformat(str3, "%s", "foo") +`ifdef TEST_VERBOSE $display("str3=%0s", str3); `endif + if (str3 != "foo") $stop; + $write("*-* All Finished *-*\n"); $finish; end