diff --git a/include/verilated.cpp b/include/verilated.cpp index ed7dac08e..0758d1736 100644 --- a/include/verilated.cpp +++ b/include/verilated.cpp @@ -1832,8 +1832,8 @@ IData _vl_vsscanf(FILE* fp, // If a fscanf return got; done: - // Scan stopped early, return parsed or EOF - if (_vl_vsss_eof(fp, floc)) return -1; + // Scan stopped early; EOF only if input ended before the first conversion + if (got == 0 && _vl_vsss_eof(fp, floc)) return -1; return got; } diff --git a/test_regress/t/t_sys_sscanf.v b/test_regress/t/t_sys_sscanf.v index 7edfa0228..3d32dd0ca 100644 --- a/test_regress/t/t_sys_sscanf.v +++ b/test_regress/t/t_sys_sscanf.v @@ -35,6 +35,20 @@ module t; `checks(token, "\0\0\0Hello"); `checkh(idx, 1); + // Input ends after some conversions: return the number assigned, not EOF + code = $sscanf("Hi 5", "%s %d %d", token, idx, val); + `checkh(code, 2); + `checks(token, "\0\0\0\0\0\0Hi"); + `checkh(idx, 5); + + // Matching failure before the first conversion: 0 + code = $sscanf("Hi", "%d", idx); + `checkh(code, 0); + + // Input ends before the first conversion: EOF + code = $sscanf("", "%d", idx); + `checkh(code, -1); + $finish; end