varnishtest "Test recursive regexp's fail before consuming all the stack"

# Use 64bit defaults also on 32bit
varnish v1 -cliok "param.set workspace_client 64k"
varnish v1 -cliok "param.set http_req_size 32k"

# If you want to play around, uncomment the next lines and adjust
# the length of the ABAB strings below to suit your needs.
# Better yet:  Rewrite your regexps to avoid this madness.

# Tweaks you may add to this test:
# varnish v1 -cliok "param.set thread_pool_stack 48k"
# varnish v1 -cliok "param.set pcre2_match_limit 100"
# varnish v1 -cliok "param.set pcre2_depth_limit 10000"
# varnish v1 -cliok "param.set pcre2_jit_compilation off"

# Approximate formula for FreeBSD/amd64:
#	pcre2_depth_limit = thread_pool_stack * 2 - 9

varnish v1 -vcl+backend {
	backend proforma none;

	sub vcl_recv {
		return (synth(200));
	}
	sub vcl_synth {
		# shamelessly copied from "bugzilla77 at gmail dot com"
		# https://bugs.php.net/bug.php?id=70110
		if (req.url ~ "^/(A{1,2}B)+$") {
			set resp.http.found = "1";
		}
	}
} -start

# This should succeed with default params and JIT/no-JIT
client c1 {
	txreq -url "/ABAABABAABABABAB"
	rxresp
	expect resp.status == 200
	expect resp.http.found == 1
} -run

logexpect l1 -v v1 {
	expect * * VCL_Error "(match|depth|recursion|JIT stack) limit"
} -start

# This should fail with default params and JIT/no-JIT
client c1 {
	txreq -url "/${string,repeat,8192,AB}"
	rxresp
	expect resp.status == 500
	expect_close
} -run

logexpect l1 -wait
