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
RevLine 
[97]1/*
2    init.c - Part of libsensors, a Linux library for reading sensor data.
[207]3    Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl>
[97]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>
[98]21#include <stdio.h>
[97]22#include "sensors.h"
23#include "data.h"
24#include "error.h"
[107]25#include "access.h"
[119]26#include "conf.h"
[3093]27#include "sysfs.h"
[3300]28#include "scanner.h"
[97]29
[98]30int sensors_init(FILE *input)
[97]31{
[4736]32        int res;
[4693]33
[4736]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;
[97]46}
47
[4737]48static void free_chip_name(sensors_chip_name *name)
[97]49{
[4736]50        free(name->prefix);
51        free(name->path);
[97]52}
53
[4760]54static void free_chip_features(sensors_chip_features *features)
[4510]55{
[4736]56        int i;
[4510]57
[4760]58        for (i = 0; i < features->feature_count; i++)
59                free(features->feature[i].data.name);
60        free(features->feature);
[4510]61}
62
[4737]63static void free_bus(sensors_bus *bus)
[97]64{
[4736]65        free(bus->adapter);
[97]66}
67
[4737]68static void free_label(sensors_label *label)
[97]69{
[4737]70        free(label->name);
71        free(label->value);
72}
73
[4768]74void free_expr(sensors_expr *expr)
[4737]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{
[4736]107        int i;
[97]108
[4736]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;
[3293]113
[4736]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;
[97]118
[4736]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;
[97]123
[4736]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;
[676]128
[4736]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;
[97]133}
134
[4737]135void sensors_cleanup(void)
[97]136{
[4737]137        int i;
[97]138
[4737]139        sensors_scanner_exit();
[97]140
[4737]141        for (i = 0; i < sensors_proc_chips_count; i++) {
142                free_chip_name(&sensors_proc_chips[i].chip);
[4760]143                free_chip_features(&sensors_proc_chips[i]);
[4737]144        }
145        free(sensors_proc_chips);
146        sensors_proc_chips = NULL;
147        sensors_proc_chips_count = sensors_proc_chips_max = 0;
[97]148
[4737]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;
[676]154
[4737]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;
[97]166}
Note: See TracBrowser for help on using the browser.