root/lm-sensors/branches/lm-sensors-3.0.0/lib/init.c @ 4768

Revision 4768, 4.1 KB (checked in by khali, 6 years ago)

Fix memory leaks in the configuration file parser when a label, set,
compute or ignore statement is found before the first chip statement.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2    init.c - Part of libsensors, a Linux library for reading sensor data.
3    Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl>
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18*/
19
20#include <stdlib.h>
21#include <stdio.h>
22#include "sensors.h"
23#include "data.h"
24#include "error.h"
25#include "access.h"
26#include "conf.h"
27#include "sysfs.h"
28#include "scanner.h"
29
30int sensors_init(FILE *input)
31{
32        int res;
33
34        if (!sensors_init_sysfs())
35                return -SENSORS_ERR_PROC;
36        if ((res = sensors_read_sysfs_bus()) ||
37            (res = sensors_read_sysfs_chips()))
38                return res;
39        if ((res = sensors_scanner_init(input)))
40                return -SENSORS_ERR_PARSE;
41        if ((res = sensors_yyparse()))
42                return -SENSORS_ERR_PARSE;
43        if ((res = sensors_substitute_busses()))
44                return res;
45        return 0;
46}
47
48static void free_chip_name(sensors_chip_name *name)
49{
50        free(name->prefix);
51        free(name->path);
52}
53
54static void free_chip_features(sensors_chip_features *features)
55{
56        int i;
57
58        for (i = 0; i < features->feature_count; i++)
59                free(features->feature[i].data.name);
60        free(features->feature);
61}
62
63static void free_bus(sensors_bus *bus)
64{
65        free(bus->adapter);
66}
67
68static void free_label(sensors_label *label)
69{
70        free(label->name);
71        free(label->value);
72}
73
74void free_expr(sensors_expr *expr)
75{
76        if (expr->kind == sensors_kind_var)
77                free(expr->data.var);
78        else if (expr->kind == sensors_kind_sub) {
79                if (expr->data.subexpr.sub1)
80                        free_expr(expr->data.subexpr.sub1);
81                if (expr->data.subexpr.sub2)
82                        free_expr(expr->data.subexpr.sub2);
83        }
84        free(expr);
85}
86
87static void free_set(sensors_set *set)
88{
89        free(set->name);
90        free_expr(set->value);
91}
92
93static void free_compute(sensors_compute *compute)
94{
95        free(compute->name);
96        free_expr(compute->from_proc);
97        free_expr(compute->to_proc);
98}
99
100static void free_ignore(sensors_ignore *ignore)
101{
102        free(ignore->name);
103}
104
105static void free_chip(sensors_chip *chip)
106{
107        int i;
108
109        for (i = 0; i < chip->chips.fits_count; i++)
110                free_chip_name(&chip->chips.fits[i]);
111        free(chip->chips.fits);
112        chip->chips.fits_count = chip->chips.fits_max = 0;
113
114        for (i = 0; i < chip->labels_count; i++)
115                free_label(&chip->labels[i]);
116        free(chip->labels);
117        chip->labels_count = chip->labels_max = 0;
118
119        for (i = 0; i < chip->sets_count; i++)
120                free_set(&chip->sets[i]);
121        free(chip->sets);
122        chip->sets_count = chip->sets_max = 0;
123
124        for (i = 0; i < chip->computes_count; i++)
125                free_compute(&chip->computes[i]);
126        free(chip->computes);
127        chip->computes_count = chip->computes_max = 0;
128
129        for (i = 0; i < chip->ignores_count; i++)
130                free_ignore(&chip->ignores[i]);
131        free(chip->ignores);
132        chip->ignores_count = chip->ignores_max = 0;
133}
134
135void sensors_cleanup(void)
136{
137        int i;
138
139        sensors_scanner_exit();
140
141        for (i = 0; i < sensors_proc_chips_count; i++) {
142                free_chip_name(&sensors_proc_chips[i].chip);
143                free_chip_features(&sensors_proc_chips[i]);
144        }
145        free(sensors_proc_chips);
146        sensors_proc_chips = NULL;
147        sensors_proc_chips_count = sensors_proc_chips_max = 0;
148
149        for (i = 0; i < sensors_config_busses_count; i++)
150                free_bus(&sensors_config_busses[i]);
151        free(sensors_config_busses);
152        sensors_config_busses = NULL;
153        sensors_config_busses_count = sensors_config_busses_max = 0;
154
155        for (i = 0; i < sensors_config_chips_count; i++)
156                free_chip(&sensors_config_chips[i]);
157        free(sensors_config_chips);
158        sensors_config_chips = NULL;
159        sensors_config_chips_count = sensors_config_chips_max = 0;
160
161        for (i = 0; i < sensors_proc_bus_count; i++)
162                free_bus(&sensors_proc_bus[i]);
163        free(sensors_proc_bus);
164        sensors_proc_bus = NULL;
165        sensors_proc_bus_count = sensors_proc_bus_max = 0;
166}
Note: See TracBrowser for help on using the browser.