Mesa (master): glcpp: Fix segfault in standalone preprocessor for " file not found", etc.

Carl Worth cworth at kemper.freedesktop.org
Mon Aug 23 17:48:14 UTC 2010


Module: Mesa
Branch: master
Commit: cf8bb19a114d753bca94f920b87dcf51aa26af99
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=cf8bb19a114d753bca94f920b87dcf51aa26af99

Author: Carl Worth <cworth at cworth.org>
Date:   Mon Aug 23 09:42:14 2010 -0700

glcpp: Fix segfault in standalone preprocessor for "file not found", etc.

This error message was missing so that the program would simply
segfault if the provided filename could not be opened for some reason.

While we're at it, we add explicit support for a filename of "-" to
indicate input from stdin.

---

 src/glsl/glcpp/glcpp.c |   21 +++++++++++++++++----
 1 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/src/glsl/glcpp/glcpp.c b/src/glsl/glcpp/glcpp.c
index e49a1df..011058a 100644
--- a/src/glsl/glcpp/glcpp.c
+++ b/src/glsl/glcpp/glcpp.c
@@ -25,6 +25,8 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <unistd.h>
+#include <string.h>
+#include <errno.h>
 #include "glcpp.h"
 
 extern int yydebug;
@@ -35,10 +37,18 @@ load_text_file(void *ctx, const char *file_name)
 	char *text = NULL;
 	struct stat st;
 	ssize_t total_read = 0;
-	int fd = file_name == NULL ? STDIN_FILENO : open(file_name, O_RDONLY);
-
-	if (fd < 0) {
-		return NULL;
+	int fd;
+
+	if (file_name == NULL || strcmp(file_name, "-") == 0) {
+	   fd = STDIN_FILENO;
+	} else {
+	   fd = open (file_name, O_RDONLY);
+
+	   if (fd < 0) {
+	      fprintf (stderr, "Failed to open file %s: %s\n",
+		       file_name, strerror (errno));
+	      return NULL;
+	   }
 	}
 
 	if (fstat(fd, & st) == 0) {
@@ -82,6 +92,9 @@ main (int argc, char *argv[])
 	}
 
 	shader = load_text_file(ctx, filename);
+	if (shader == NULL)
+	   return 1;
+
 	ret = preprocess(ctx, &shader, &info_log, NULL);
 
 	printf("%s", shader);




More information about the mesa-commit mailing list