aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTuukka Tikkanen <idlestat@tic0.net>2015-01-28 16:28:46 +0200
committerAmit Kucheria <amit.kucheria@linaro.org>2015-03-10 02:27:30 +0530
commit96c4890f3addebe9ef1bc735871d786d897c2b78 (patch)
tree34cf6c0ab8e7b7e01554c9995d6a947253a9adde
parent31d9654b479c960187ab6cdf87b5db3a92a990af (diff)
Topology: Make read_cpu_topo_info more robust
The function read_cpu_topo_info ignores several return values, doesn't skip comment lines and contains redundant innermost loop. This patch fixes these issues. Signed-off-by: Tuukka Tikkanen <idlestat@tic0.net> Signed-off-by: Amit Kucheria <amit.kucheria@linaro.org>
-rw-r--r--topology.c65
1 files changed, 42 insertions, 23 deletions
diff --git a/topology.c b/topology.c
index c7528af..c1b399c 100644
--- a/topology.c
+++ b/topology.c
@@ -414,49 +414,68 @@ struct cpu_topology *read_cpu_topo_info(FILE *f, char *buf)
return result;
do {
+ /* Skip comment lines */
+ if (*buf == '#' || *buf == '\0') {
+ if (!fgets(buf, BUFSIZE, f))
+ goto read_error_or_eof;
+ continue;
+ }
+
+ /* Cluster line? */
ret = sscanf(buf, "cluster%c", &pid);
if (!ret)
break;
-
cpu_info.physical_id = pid - 'A';
- fgets(buf, BUFSIZE, f);
+ if (!fgets(buf, BUFSIZE, f))
+ goto read_error_or_eof;
+
+ is_ht = false;
do {
+ /* Skip comment lines */
+ if (*buf == '#' || *buf == '\0') {
+ if (!fgets(buf, BUFSIZE, f))
+ goto read_error_or_eof;
+ continue;
+ }
+
+ /* Core line? */
ret = sscanf(buf, "\tcore%d", &cpu_info.core_id);
if (ret) {
+ if (!fgets(buf, BUFSIZE, f))
+ goto read_error_or_eof;
is_ht = true;
- fgets(buf, BUFSIZE, f);
- } else {
- ret = sscanf(buf, "\tcpu%d", &cpu_info.cpu_id);
- if (ret)
- is_ht = false;
- else
- break;
+ continue;
}
- do {
- if (!is_ht) {
- ret = sscanf(buf, "\tcpu%d",
- &cpu_info.cpu_id);
- cpu_info.core_id = cpu_info.cpu_id;
- } else {
- ret = sscanf(buf, "\t\tcpu%d",
- &cpu_info.cpu_id);
- }
+ /* Cpu line? */
+ if (!is_ht) {
+ ret = sscanf(buf, "\tcpu%d",
+ &cpu_info.cpu_id);
+ cpu_info.core_id = cpu_info.cpu_id;
+ } else {
+ ret = sscanf(buf, "\t\tcpu%d",
+ &cpu_info.cpu_id);
+ }
- if (!ret)
- break;
+ if (!ret)
+ break;
- add_topo_info(result, &cpu_info);
+ add_topo_info(result, &cpu_info);
- fgets(buf, BUFSIZE, f);
- } while (1);
+ if (!fgets(buf, BUFSIZE, f))
+ goto read_error_or_eof;
} while (1);
} while (1);
/* output_topo_info(result); */
return result;
+
+read_error_or_eof:
+ fprintf(stderr, "Error: EOF in trace file while reading topology\n");
+ release_cpu_topo_info(result);
+ return ptrerror(NULL);
}
int release_cpu_topo_info(struct cpu_topology *topo)