From a6783afcf61f18e9f1aef3e2655b30af7501c902 Mon Sep 17 00:00:00 2001
From: Robin Luckey <robin@ohloh.net>
Date: Thu, 1 Oct 2009 14:32:16 -0700
Subject: [PATCH] [FIX] Avoid null dereference in disambiguate_inc()

---
 src/detector.c              |   18 ++++++++++--------
 test/unit/detector_test.h   |    1 +
 2 files changed, 11 insertions(+), 8 deletions(-)
 create mode 100644 test/detect_files/empty.inc

diff --git a/src/detector.c b/src/detector.c
index 4d0e1f4..9b4d8d2 100644
--- a/src/detector.c
+++ b/src/detector.c
@@ -452,14 +452,16 @@ const char *disambiguate_in(SourceFile *sourcefile) {
 
 const char *disambiguate_inc(SourceFile *sourcefile) {
   char *p = ohcount_sourcefile_get_contents(sourcefile);
-  char *eof = p + strlen(p);
-  while (p < eof) {
-    if (*p == '\0')
-      return BINARY;
-    else if (*p == '?' && strncmp(p + 1, "php", 3) == 0)
-      return LANG_PHP;
-    p++;
-  }
+	if (p) {
+		char *eof = p + strlen(p);
+		while (p < eof) {
+			if (*p == '\0')
+				return BINARY;
+			else if (*p == '?' && strncmp(p + 1, "php", 3) == 0)
+				return LANG_PHP;
+			p++;
+		}
+	}
   return NULL;
 }
 
diff --git a/test/detect_files/empty.inc b/test/detect_files/empty.inc
new file mode 100644
index 0000000..e69de29
diff --git a/test/unit/detector_test.h b/test/unit/detector_test.h
index cd36b6d..628b6cc 100644
--- a/test/unit/detector_test.h
+++ b/test/unit/detector_test.h
@@ -77,6 +77,7 @@ void test_detector_detect_polyglot() {
 	ASSERT_DETECT(LANG_IDL_PVWAVE, "foo.pro");
   ASSERT_DETECT(LANG_ASSEMBLER, "foo.z80");
   ASSERT_DETECT(LANG_PHP, "php.inc");
+  ASSERT_NODETECT("empty.inc");
   ASSERT_DETECT(LANG_FSHARP, "fs1.fs");
 }
 
-- 
1.7.0.1

