From: =?utf-8?b?0L3QsNCx?= <nabijaczleweli@nabijaczleweli.xyz>
Date: Sat, 24 May 2025 19:21:09 +0200
Subject: Don't segfault when given nonexistent file directly or via -f in
 {c,ml,php}_count (Closes: #716179)

Error path adapted from driver.c
---
 c_count.c   | 4 ++++
 ml_count.c  | 4 ++++
 php_count.c | 4 ++++
 3 files changed, 12 insertions(+)

diff --git a/c_count.c b/c_count.c
index 8581e55..761d47b 100644
--- a/c_count.c
+++ b/c_count.c
@@ -162,6 +162,10 @@ void count_file(char *filename) {
   FILE *stream;
 
   stream = fopen(filename, "r");
+  if (!stream) {
+    fprintf(stderr, "Error: Cannot open %s\n", filename);
+    return;
+  }
   line_number = 1;
   sloc = sloc_count(filename, stream);
   total_sloc += sloc;
diff --git a/ml_count.c b/ml_count.c
index dc18f35..26da127 100644
--- a/ml_count.c
+++ b/ml_count.c
@@ -146,6 +146,10 @@ void count_file(char *filename) {
   FILE *stream;
 
   stream = fopen(filename, "r");
+  if (!stream) {
+    fprintf(stderr, "Error: Cannot open %s\n", filename);
+    return;
+  }
   line_number = 1;
   sloc = sloc_count(filename, stream);
   total_sloc += sloc;
diff --git a/php_count.c b/php_count.c
index ee7ce10..e62c975 100644
--- a/php_count.c
+++ b/php_count.c
@@ -270,6 +270,10 @@ void count_file(char *filename) {
   FILE *stream;
 
   stream = fopen(filename, "r");
+  if (!stream) {
+    fprintf(stderr, "Error: Cannot open %s\n", filename);
+    return;
+  }
   line_number = 0;
   init_input(stream);
   sloc = sloc_count(filename, stream);
