Turbostat: init_counter cannot fail anymore
[collectd.git] / src / turbostat.c
index cc0be20..d8e31c1 100644 (file)
@@ -262,6 +262,7 @@ enum return_values {
        ERR_CPU_ALLOC,
        ERR_NOT_ROOT,
        UNSUPPORTED_CPU,
+       ERR_PATH_TOO_LONG,
 };
 
 
@@ -600,7 +601,7 @@ turbostat_submit (const char *plugin_instance,
 static int
 submit_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
 {
-       char name[12];
+       char name[DATA_MAX_NAME_LEN];
        double interval_float;
 
        interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
@@ -1069,11 +1070,16 @@ parse_int_file(const char *fmt, ...)
        va_list args;
        char path[PATH_MAX];
        FILE *filep;
-       int value;
+       int len, value;
 
        va_start(args, fmt);
-       vsnprintf(path, sizeof(path), fmt, args);
+       len = vsnprintf(path, sizeof(path), fmt, args);
        va_end(args);
+       if (len < 0 || len >= PATH_MAX) {
+               ERROR("Turbostat plugin: path truncated: '%s'", path);
+               return -ERR_PATH_TOO_LONG;
+       }
+
        filep = fopen(path, "r");
        if (!filep) {
                ERROR("Turbostat plugin: Failed to open '%s'", path);
@@ -1331,7 +1337,7 @@ err:
        return -ERR_CALLOC;
 }
 
-static int
+static void
 init_counter(struct thread_data *thread_base, struct core_data *core_base,
        struct pkg_data *pkg_base, int cpu_id)
 {
@@ -1352,32 +1358,20 @@ init_counter(struct thread_data *thread_base, struct core_data *core_base,
 
        c->core_id = cpu->core_id;
        p->package_id = cpu->package_id;
-
-       return 0;
 }
 
-static int
+static void
 initialize_counters(void)
 {
-       int ret;
        int cpu_id;
 
        for (cpu_id = 0; cpu_id <= topology.max_cpu_id; ++cpu_id) {
-               if (cpu_is_not_present(cpu_id)) {
+               if (cpu_is_not_present(cpu_id))
                        continue;
-               }
-
-               ret = init_counter(EVEN_COUNTERS, cpu_id);
-               if (ret < 0)
-                       return ret;
-               ret = init_counter(ODD_COUNTERS, cpu_id);
-               if (ret < 0)
-                       return ret;
-               ret = init_counter(DELTA_COUNTERS, cpu_id);
-               if (ret < 0)
-                       return ret;
+               init_counter(EVEN_COUNTERS, cpu_id);
+               init_counter(ODD_COUNTERS, cpu_id);
+               init_counter(DELTA_COUNTERS, cpu_id);
        }
-       return 0;
 }
 
 
@@ -1445,7 +1439,7 @@ static int setup_all_buffers(void)
        DO_OR_GOTO_ERR(allocate_counters(&thread_even, &core_even, &package_even));
        DO_OR_GOTO_ERR(allocate_counters(&thread_odd, &core_odd, &package_odd));
        DO_OR_GOTO_ERR(allocate_counters(&thread_delta, &core_delta, &package_delta));
-       DO_OR_GOTO_ERR(initialize_counters());
+       initialize_counters();
        DO_OR_GOTO_ERR(for_all_cpus(set_temperature_target, EVEN_COUNTERS));
        DO_OR_GOTO_ERR(for_all_cpus(set_temperature_target, ODD_COUNTERS));