Fix $sscanf/$fscanf returning -1 after a partial match (#8457) (#8459)

This commit is contained in:
David Harris
2026-09-23 05:35:17 -04:00
committed by GitHub
parent 39e2d8e853
commit fe4b3bdec4
2 changed files with 16 additions and 2 deletions
+2 -2
View File
@@ -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;
}
+14
View File
@@ -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