1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
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);
|