From fe4b3bdec449f12e0b395cab6e61824abc909de1 Mon Sep 17 00:00:00 2001 From: David Harris <74973295+davidharrishmc@users.noreply.github.com> Date: Wed, 23 Sep 2026 02:35:17 -0700 Subject: [PATCH] Fix $sscanf/$fscanf returning -1 after a partial match (#8457) (#8459) --- include/verilated.cpp | 4 ++-- test_regress/t/t_sys_sscanf.v | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) 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