2
0
mirror of https://github.com/lm-sensors/lm-sensors synced 2025-09-02 15:25:38 +00:00

libsensors: New method to free the memory allocated for the internal

representation of chip names.
sensord, sensors: Fix a memory leak when one or more chip names are
provided on the command line.


git-svn-id: http://lm-sensors.org/svn/lm-sensors/trunk@5740 7894878c-1315-0410-8ee3-d5d059ff63e0
This commit is contained in:
Jean Delvare
2009-06-20 10:04:52 +00:00
parent ef2f2c04ce
commit 78f496ca62
9 changed files with 44 additions and 4 deletions

View File

@@ -5,6 +5,7 @@ SVN-HEAD
isadump: Use geteuid instead of getuid so that setuid bit works
isaset: Use geteuid instead of getuid so that setuid bit works
libsensors: Don't rely on dirent->dt_type being set
New method to free the memory allocated for chip names
Makefile: Include generated source files in dependency checking
Make it possible to skip building of the static library
fancontrol: Add support for absolute path to hwmon devices
@@ -16,6 +17,8 @@ SVN-HEAD
Don't use the system log when generating a CGI script
Disable unit scaling for fan speeds
Use daemon logging facility instead of local4 by default
Fix a memory leak when a chip name is provided
sensors: Fix a memory leak when a chip name is provided
sensors-detect: Add nNidia nForce MCP78S SMBus detection
Display Super-I/O address even if LD is disabled
Differentiate between PC8374L and WPCD377I

View File

@@ -6,6 +6,10 @@ over time. This document summarizes these evolutions so that application
authors can quickly figure out how to test for the availability of a
given new feature.
0x420 lm-sensors 3.1.1
* Added a method to free the memory allocated by sensors_parse_chip_name()
void sensors_free_chip_name(sensors_chip_name *chip);
0x410 lm-sensors 3.1.0
* Added bus type "acpi":
#define SENSORS_BUS_TYPE_ACPI

View File

@@ -54,6 +54,11 @@ sensors_bus *sensors_proc_bus = NULL;
int sensors_proc_bus_count = 0;
int sensors_proc_bus_max = 0;
void sensors_free_chip_name(sensors_chip_name *chip)
{
free(chip->prefix);
}
/*
Parse a chip name to the internal representation. These are valid names:

View File

@@ -42,6 +42,7 @@ libsensors \- publicly accessible functions provided by the sensors library
/* Chip name handling */
.BI "int sensors_parse_chip_name(const char *" orig_name ","
.BI " sensors_chip_name *" res ");"
.BI "void sensors_free_chip_name(sensors_chip_name *" chip ");"
.BI "int sensors_snprintf_chip_name(char *" str ", size_t " size ","
.BI " const sensors_chip_name *" chip ");"
.BI "const char *sensors_get_adapter_name(const sensors_bus_id *" bus ");"
@@ -101,7 +102,14 @@ is a string representing the version of libsensors.
.B sensors_parse_chip_name()
parses a chip name to the internal representation. Return 0 on success,
<0 on error.
<0 on error. Make sure to call sensors_free_chip_name() when you're done
with the data.
.B sensors_free_chip_name()
frees the memory that may have been allocated for the internal
representation of a chip name. You only have to call this for chip
names which do not originate from libsensors itself (that is, chip
names which were generated by sensors_parse_chip_name()).
.B sensors_snprintf_chip_name()
prints a chip name from its internal representation. Note that chip should

View File

@@ -31,7 +31,7 @@
when the API + ABI breaks), the third digit is incremented to track small
API additions like new flags / enum values. The second digit is for tracking
larger additions like new methods. */
#define SENSORS_API_VERSION 0x410
#define SENSORS_API_VERSION 0x420
#define SENSORS_CHIP_NAME_PREFIX_ANY NULL
#define SENSORS_CHIP_NAME_ADDR_ANY (-1)
@@ -80,6 +80,9 @@ void sensors_cleanup(void);
on error. */
int sensors_parse_chip_name(const char *orig_name, sensors_chip_name *res);
/* Free memory allocated for the internal representation of a chip name. */
void sensors_free_chip_name(sensors_chip_name *chip);
/* Print a chip name from its internal representation. Note that chip should
not contain wildcard values! Return the number of characters printed on
success (same as snprintf), <0 on error. */

View File

@@ -255,6 +255,8 @@ int parseChips(int argc, char **argv)
if (err) {
fprintf(stderr, "Invalid chip name `%s': %s\n", arg,
sensors_strerror(err));
for (i--; i >= 0; i--)
sensors_free_chip_name(sensord_args.chipNames + i);
return -1;
}
}
@@ -262,3 +264,11 @@ int parseChips(int argc, char **argv)
return 0;
}
void freeChips()
{
int i;
for (i = 0; i < sensord_args.numChipNames; i++)
sensors_free_chip_name(sensord_args.chipNames + i);
}

View File

@@ -233,17 +233,21 @@ int main(int argc, char **argv)
parseChips(argc, argv))
exit(EXIT_FAILURE);
if (loadLib(sensord_args.cfgFile))
if (loadLib(sensord_args.cfgFile)) {
freeChips();
exit(EXIT_FAILURE);
}
if (!sensord_args.doCGI)
openLog();
if (sensord_args.rrdFile) {
ret = rrdInit();
if (ret)
if (ret) {
freeChips();
exit(EXIT_FAILURE);
}
}
if (sensord_args.doCGI) {
ret = rrdCGI();
@@ -253,6 +257,7 @@ int main(int argc, char **argv)
undaemonize();
}
freeChips();
if (unloadLib())
exit(EXIT_FAILURE);

View File

@@ -31,6 +31,7 @@ extern void sensorLog(int priority, const char *fmt, ...);
extern int parseArgs(int argc, char **argv);
extern int parseChips(int argc, char **argv);
extern void freeChips(void);
/* from lib.c */

View File

@@ -334,6 +334,7 @@ int main(int argc, char *argv[])
goto exit;
}
cnt += do_the_real_work(&chip, &err);
sensors_free_chip_name(&chip);
}
if (!cnt) {