diff mbox

[perftest,16/23] perftest: check for NULL fgets() returns

Message ID 1471541538-20270-17-git-send-email-jarod@redhat.com (mailing list archive)
State Not Applicable
Headers show

Commit Message

Jarod Wilson Aug. 18, 2016, 5:32 p.m. UTC
Check for fgets() failure in addition to fopen() failure and take
appropriate action.

CC: Gil Rockah <gilr@mellanox.com>
Signed-off-by: Jarod Wilson <jarod@redhat.com>
---
 src/perftest_parameters.c    | 5 ++++-
 src/perftest_resources.c     | 5 ++++-
 src/raw_ethernet_resources.c | 5 ++++-
 3 files changed, 12 insertions(+), 3 deletions(-)
diff mbox

Patch

diff --git a/src/perftest_parameters.c b/src/perftest_parameters.c
index e316143..32ebea9 100755
--- a/src/perftest_parameters.c
+++ b/src/perftest_parameters.c
@@ -147,7 +147,10 @@  static int get_cache_line_size()
 		if (fp == NULL) {
 			return DEF_CACHE_LINE_SIZE;
 		}
-		fgets(line,10,fp);
+		if (fgets(line,10,fp) == NULL) {
+			fclose(fp);
+			return DEF_CACHE_LINE_SIZE;
+		}
 		size = atoi(line);
 		fclose(fp);
 	}
diff --git a/src/perftest_resources.c b/src/perftest_resources.c
index 4dc0705..91c6d1e 100755
--- a/src/perftest_resources.c
+++ b/src/perftest_resources.c
@@ -230,7 +230,10 @@  static void get_cpu_stats(struct perftest_parameters *duration_param,int stat_in
 	int index=0;
 	fp = fopen(file_name, "r");
 	if (fp != NULL) {
-		fgets(line,100,fp);
+		if (fgets(line,100,fp) == NULL) {
+			fclose(fp);
+			return;
+		}
 		compress_spaces(line,line);
 		index=get_n_word_string(line,tmp,index,2); /* skip first word */
 		duration_param->cpu_util_data.ustat[stat_index-1] = atoll(tmp);
diff --git a/src/raw_ethernet_resources.c b/src/raw_ethernet_resources.c
index d087cab..22e1bab 100755
--- a/src/raw_ethernet_resources.c
+++ b/src/raw_ethernet_resources.c
@@ -69,7 +69,10 @@  int check_flow_steering_support(char *dev_name)
 	fp = fopen(file_name, "r");
 	if (fp == NULL)
 		return 0;
-	fgets(line,4,fp);
+	if (fgets(line,4,fp) == NULL) {
+		fclose(fp);
+		return 0;
+	}
 
 	int val = atoi(line);
 	if (val >= 0) {