Compare commits

...

1 Commits

Author SHA1 Message Date
Xavier Roche
ffb4e6b55b tests: make the strjoker work-budget test deterministic, not timeout-reliant
The filterbounds self-test's step-budget case (900 stars, subject cut to 900)
stayed under the 2,000,000-step budget: the #501 failure memo alone bounded it,
so the budget never fired and the `== NULL` assertion held with or without it.
Only a harness timeout would have caught the budget's removal.

Size the star-heavy dead-end to the length cap (1023 stars, 2048-char subject),
where the memoized matcher runs ~1.26e9 steps (~6s) unbounded, and assert
through a new test-only strjoker_steps() that the work stays near the cap.
Deleting the budget now trips the assertion in milliseconds instead of timing
the suite out.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Xavier Roche <roche@httrack.com>
2026-07-12 15:33:55 +02:00
3 changed files with 29 additions and 7 deletions

View File

@@ -213,6 +213,20 @@ const char *strjoker_nomemo(const char *chaine, const char *joker, LLint *size,
return strjoker_rec(&memo, chaine, joker, size, size_flag);
}
/* Test-only: strjoker() reporting the work-budget steps spent and the cap, so a
self-test can prove the budget bounds a hostile pattern's work. */
const char *strjoker_steps(const char *chaine, const char *joker,
size_t *nsteps_out, size_t *maxsteps_out) {
size_t nsteps = 0;
const char *r = strjoker_bounded(chaine, joker, NULL, NULL, &nsteps);
if (nsteps_out != NULL)
*nsteps_out = nsteps;
if (maxsteps_out != NULL)
*maxsteps_out = STRJOKER_MAXSTEPS;
return r;
}
static const char *strjoker_impl(const strjoker_memo *memo, const char *chaine,
const char *joker, LLint *size,
int *size_flag) {

View File

@@ -52,6 +52,10 @@ HTS_INLINE const char *strjoker(const char *chaine, const char *joker, LLint * s
oracle for the memoized matcher. */
const char *strjoker_nomemo(const char *chaine, const char *joker, LLint *size,
int *size_flag);
/* strjoker() reporting the work-budget steps it spent and the cap; test-only,
lets a self-test assert the budget bounds a hostile pattern's work. */
const char *strjoker_steps(const char *chaine, const char *joker,
size_t *nsteps_out, size_t *maxsteps_out);
const char *strjokerfind(const char *chaine, const char *joker);
#endif

View File

@@ -744,29 +744,33 @@ static int st_filterdual(httrackp *opt, int argc, char **argv) {
process (OSS-Fuzz 5060751291908096 / 5745936014573568). */
static int st_filterbounds(httrackp *opt, int argc, char **argv) {
const size_t big = 100000; /* well past the length cap */
const size_t stars = 900; /* star-heavy pattern, under the cap */
const size_t stars = 1023; /* pattern len 2047, under the length cap */
const size_t subjlen = 2048;
char *subj = malloct(big + 1);
char *pat = malloct(2 * stars + 2);
size_t i;
size_t steps = 0, maxsteps = 0, i;
(void) opt;
(void) argc;
(void) argv;
memset(subj, 'a', big);
subj[big] = '\0';
/* '*' matches anything, but an over-length subject is refused by the cap */
/* '*' matches anything, but an over-length subject trips the length cap */
assertf(strjoker(subj, "*", NULL, NULL) == NULL);
assertf(strjokerfind(subj, "*") == NULL);
/* within-cap star-heavy dead-end: the step budget keeps it bounded (a hang
would time the test out) */
/* Star-heavy dead-end at the length cap: unbounded it runs ~1.26e9 memo-steps
(~6s). */
for (i = 0; i < stars; i++) {
pat[2 * i] = '*';
pat[2 * i + 1] = 'a';
}
pat[2 * stars] = 'b'; /* never matches an all-'a' subject */
pat[2 * stars + 1] = '\0';
subj[stars] = '\0';
assertf(strjoker(subj, pat, NULL, NULL) == NULL);
subj[subjlen] = '\0';
/* Budget must bound the work; NULL alone wouldn't prove it (a cheap mismatch
is NULL too). */
assertf(strjoker_steps(subj, pat, &steps, &maxsteps) == NULL);
assertf(steps < 10 * maxsteps);
assertf(strjokerfind(subj, pat) == NULL);
freet(pat);
freet(subj);