From 96c4890f3addebe9ef1bc735871d786d897c2b78 Mon Sep 17 00:00:00 2001 From: Tuukka Tikkanen Date: Wed, 28 Jan 2015 16:28:46 +0200 Subject: 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 Signed-off-by: Amit Kucheria --- topology.c | 65 ++++++++++++++++++++++++++++++++++++++++---------------------- 1 file 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) -- cgit v1.2.3