File: 0007-tracefile_idlestat-EOF-checks-while-parsing-file-wit.patch

package info (click to toggle)
idlestat 0.8-9
  • links: PTS
  • area: main
  • in suites: sid
  • size: 480 kB
  • sloc: ansic: 6,539; makefile: 56
file content (56 lines) | stat: -rw-r--r-- 1,710 bytes parent folder | download | duplicates (3)
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
From 4bf657b0c625a29eeea47e4405c343bde3ddd831 Mon Sep 17 00:00:00 2001
From: Colin Ian King <colin.i.king@gmail.com>
Date: Tue, 13 Dec 2022 16:36:24 +0000
Subject: [PATCH] tracefile_idlestat: EOF checks while parsing file with fgets
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit

Signed-off-by: Colin Ian King <colin.i.king@gmail.com>
---
 tracefile_idlestat.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/tracefile_idlestat.c b/tracefile_idlestat.c
index 01b1b26..c7aeda6 100644
--- a/tracefile_idlestat.c
+++ b/tracefile_idlestat.c
@@ -81,20 +81,20 @@ static struct cpuidle_cstates *load_and_build_cstate_info(FILE* f, char *buffer,
 		}
 
 		for (i = 0; i < MAXCSTATE; i++) {
-			int residency;
-			char *name = malloc(128);
+			int residency = 0;
+			char *name = calloc(sizeof(*name), 128);
 			if (!name) {
 				release_cstate_info(cstates, cpu);
 				return ptrerror(__func__);
 			}
 
-			fgets(buffer, BUFSIZE, f);
-			sscanf(buffer, "\t%s\n", name);
-			fgets(buffer, BUFSIZE, f);
-			sscanf(buffer, "\t%d\n", &residency);
+			if (fgets(buffer, BUFSIZE, f) != NULL)
+				sscanf(buffer, "\t%s\n", name);
+			if (fgets(buffer, BUFSIZE, f) != NULL)
+				sscanf(buffer, "\t%d\n", &residency);
 
 			c = &(cstates[cpu].cstate[i]);
-			if (!strcmp(name, "(null)")) {
+			if ((*name = '\0') || !strcmp(name, "(null)")) {
 				free(name);
 				c->name = NULL;
 			} else {
@@ -110,7 +110,7 @@ static struct cpuidle_cstates *load_and_build_cstate_info(FILE* f, char *buffer,
 			c->duration = 0.;
 			c->target_residency = residency;
 		}
-		fgets(buffer, BUFSIZE, f);
+		(void)fgets(buffer, BUFSIZE, f);
 	}
 
 	return cstates;
-- 
2.38.1