[experimental-ims] 344a709 std.fileread() should not blindly return whatever file it returned last without checking if the filename changed.

Poul-Henning Kamp phk at FreeBSD.org
Thu Dec 18 10:27:47 CET 2014


commit 344a709ccf9559f3d8e5d7a0a9a35c6e94705f0f
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Mon Jun 4 09:18:31 2012 +0000

    std.fileread() should not blindly return whatever file it returned
    last without checking if the filename changed.
    
    Fixes		#1145
    Testcase by:	tobixen

diff --git a/bin/varnishtest/tests/r01145.vtc b/bin/varnishtest/tests/r01145.vtc
new file mode 100644
index 0000000..2a2502e
--- /dev/null
+++ b/bin/varnishtest/tests/r01145.vtc
@@ -0,0 +1,43 @@
+varnishtest "Test fileread for std VMOD"
+
+shell {
+	printf "File One" > "${tmpdir}/one"
+	printf "File Two" > "${tmpdir}/two"
+	printf "File Three" > "${tmpdir}/three"
+}
+
+server s1 {
+	loop 3 {
+		rxreq
+		txresp
+	}
+} -start
+
+
+varnish v1 -vcl+backend {
+	import std from "${topbuild}/lib/libvmod_std/.libs/libvmod_std.so" ;
+
+	sub vcl_deliver {
+            set resp.http.foo = std.fileread("${tmpdir}" + req.url);
+	}
+} -start
+
+
+client c1 {
+	txreq -url "/one"
+	rxresp
+	expect resp.http.foo == "File One"
+
+	txreq -url "/two"
+	rxresp
+	expect resp.http.foo == "File Two"
+
+	txreq -url "/three"
+	rxresp
+	expect resp.http.foo == "File Three"
+} -run
+
+client c1 -run
+client c1 -run
+
+
diff --git a/lib/libvmod_std/vmod_std_fileread.c b/lib/libvmod_std/vmod_std_fileread.c
index 154d8ca..7012b66 100644
--- a/lib/libvmod_std/vmod_std_fileread.c
+++ b/lib/libvmod_std/vmod_std_fileread.c
@@ -85,17 +85,21 @@ free_frfile(void *ptr)
 const char *
 vmod_fileread(struct sess *sp, struct vmod_priv *priv, const char *file_name)
 {
-	struct frfile *frf;
+	struct frfile *frf = NULL;
 	char *s;
 
 	(void)sp;
 	AN(priv);
+
 	if (priv->priv != NULL) {
 		CAST_OBJ_NOTNULL(frf, priv->priv, CACHED_FILE_MAGIC);
-		return (frf->contents);
+		if (!strcmp(file_name, frf->file_name))
+			return (frf->contents);
 	}
 
 	AZ(pthread_mutex_lock(&frmtx));
+	if (frf != NULL)
+		frf->refcount--;
 	VTAILQ_FOREACH(frf, &frlist, list) {
 		if (!strcmp(file_name, frf->file_name)) {
 			frf->refcount++;



More information about the varnish-commit mailing list