[master] 18f1f7a Use VFIL_readfile() rather than home-rolled function.

Poul-Henning Kamp phk at FreeBSD.org
Tue Jan 6 15:12:36 CET 2015


commit 18f1f7ac1aeaefc68484537c9e7482f90d03f991
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Tue Jan 6 14:11:53 2015 +0000

    Use VFIL_readfile() rather than home-rolled function.
    
    Inspired by fix from JonathanHuot at github

diff --git a/bin/varnishtest/vtc_main.c b/bin/varnishtest/vtc_main.c
index 6fdfb6f..8b649d8 100644
--- a/bin/varnishtest/vtc_main.c
+++ b/bin/varnishtest/vtc_main.c
@@ -43,6 +43,7 @@
 #include "vtc.h"
 
 #include "vev.h"
+#include "vfil.h"
 #include "vqueue.h"
 #include "vrnd.h"
 #include "vtim.h"
@@ -108,37 +109,6 @@ parse_D_opt(char *arg)
 }
 
 /**********************************************************************
- * Read a file into memory
- */
-
-static char *
-read_file(const char *fn)
-{
-	char *buf;
-	ssize_t sz = MAX_FILESIZE;
-	ssize_t s;
-	int fd;
-
-	fd = open(fn, O_RDONLY);
-	if (fd < 0)
-		return (NULL);
-	buf = malloc(sz);
-	assert(buf != NULL);
-	s = read(fd, buf, sz - 1);
-	if (s <= 0) {
-		free(buf);
-		(void)close(fd);
-		return (NULL);
-	}
-	AZ(close (fd));
-	assert(s < sz);		/* XXX: increase MAX_FILESIZE */
-	buf[s] = '\0';
-	buf = realloc(buf, s + 1);
-	assert(buf != NULL);
-	return (buf);
-}
-
-/**********************************************************************
  * Print usage
  */
 
@@ -345,7 +315,7 @@ i_mode(void)
 
 	vsb = VSB_new_auto();
 
-	q = p = read_file("Makefile");
+	q = p = VFIL_readfile(NULL, "Makefile", NULL);
 	if (p == NULL) {
 		fprintf(stderr, "No Makefile to search for -i flag.\n");
 		VSB_printf(vsb, "%s/../..", cwd);
@@ -492,7 +462,7 @@ main(int argc, char * const *argv)
 	argv += optind;
 
 	for (;argc > 0; argc--, argv++) {
-		p = read_file(*argv);
+		p = VFIL_readfile(NULL, *argv, NULL);
 		if (p == NULL) {
 			fprintf(stderr, "Cannot stat file \"%s\": %s\n",
 			    *argv, strerror(errno));
diff --git a/lib/libvarnish/vfil.c b/lib/libvarnish/vfil.c
index d985e3e..0b035da 100644
--- a/lib/libvarnish/vfil.c
+++ b/lib/libvarnish/vfil.c
@@ -105,8 +105,11 @@ VFIL_readfd(int fd, ssize_t *sz)
 		return (NULL);
 	f = malloc(st.st_size + 1);
 	assert(f != NULL);
-	i = read(fd, f, st.st_size);
-	assert(i == st.st_size);
+	i = read(fd, f, st.st_size + 1);
+	if (i != st.st_size) {
+		free(f);
+		return (NULL);
+	}
 	f[i] = '\0';
 	if (sz != NULL)
 		*sz = st.st_size;



More information about the varnish-commit mailing list