Memory leak in sensors_get_label()
Hello,
there seems to be a minor memory leak in the sensors_get_label() call
in the lm-sensors library, version 2.9.1. The code in question is as follows, from lib/access.c:
[...]
171 } else if (alt_featureptr &&
172 !strcasecmp(alt_featureptr->name, chip->labels[i].name)) {
173 if (*result)
174 free(*result);
175 if (! (*result = strdup(chip->labels[i].value)))
176 sensors_fatal_error("sensors_get_label","Allocating label text");
177 }
178 if (! (*result = strdup(featureptr->name)))
179 sensors_fatal_error("sensors_get_label","Allocating label text");
180 return 0;
You can see that if line 175 gets executed, then the memory it allocates
is going to be leaked at line 178 where the *result pointer is overwritten.
It seems to me that there should either be a "return 0;" statement
between lines 176 and 177, to return with *result pointing at the copy of chip->labels[i].value, or else *result should not be being assigned in this instance. To be honest, I don't really understand what the code here is trying to achieve, being a bit of an lm-sensors noob -- I was only looking at it to track down the memory leak. Hopefully you can advise what the correct fix is.
Clearly this isn't a problem for one-shot programs, but polling twice
a minute we are leaking about 100 bytes a time, which is over 200k per day -- a bit of a nuisance on an embedded platform.
Cheers,
John
Signature · 2 lines
Dr. John Naylon Cambridge Broadband Ltd. // www.cambridgebroadband.com
Hi John,
Quoted 17 lines
there seems to be a minor memory leak in the
sensors_get_label()call in the lm-sensors library, version 2.9.1. The code in question is as follows, fromlib/access.c:[...]
171 } else if (
alt_featureptr&&
172 !strcasecmp(alt_featureptr->name, chip->labels[i].name)) {
173 if (*result)
174free(*result);
175 if (! (*result =strdup(chip->labels[i].value)))
176sensors_fatal_error("sensors_get_label","Allocating label text");
177 }
178 if (! (*result =strdup(featureptr->name)))
179sensors_fatal_error("sensors_get_label","Allocating label text");
180 return 0;
Quoted 3 lines
You can see that if line 175 gets executed, then the memory it allocates is going to be leaked at line 178 where the *result pointer is overwritten.
Correct.
Quoted 3 lines
It seems to me that there should either be a "return 0;" statement
between lines 176 and 177, to return with *result pointing at the copy of chip->labels[i].value,
No, this would return as soon as a non-exact match happens. The idea behind the algorithm is to gather non-exact labels until an exact match happens, or the end of the loop is reached.
Quoted 1 line
or else *result should not be being assigned in this instance.
I think it is correct to assign it, but it should then not be overwritten by the trailing default case. IOW, there should be:
if (*result)
return 0;
between lines 177 and 178. Or so I think.
Quoted 4 lines
To be honest, I don't really understand what the code here is trying to achieve, being a bit of an lm-sensors noob -- I was only looking at it to track down the memory leak. Hopefully you can advise what the correct fix is.
Thanks a lot for looking into this. We know there are a few leaks in libsensors, most of which are in the configuration file parser, and none of us know how to fix them. The one you have been revealing was gone unnoticed so far, it seems.
I think that the code attempts to do the following:
1* If a label is found for the given feature, return it. 2* If the feature has no label but has a logical parent which has a label, return that label.
3* If no label is found, return the name of the feature itself.
Currently, 3* will override 2* instead of being used as a last resort solution, as it should. That's where the leak happens. Good catch :)
We can't blame you for not understanding how that code works. It's not exactly elegant, let alone the terrible coding style and lack of comments.
The immediate solution would be to add the conditional return I mentioned above. However, I don't think that the way the function works is great by itself. Returning the logical parent's label doesn't make any sense to me. I'd rather see the feature name rather than the parent's label, it's more likely to tell me what the feature is meant for.
What do the other folks here think?
Quoted 3 lines
Clearly this isn't a problem for one-shot programs, but polling twice a minute we are leaking about 100 bytes a time, which is over 200k per day -- a bit of a nuisance on an embedded platform.
Yes of course. Leaks are bad anyway, so let's kill that one. What tool are you using BTW? sensord?
I've attached my proposed patch. Please give it a try. If it works for you and nobody objects to the way it changes the function, I'll apply it to lm_sensors CVS.
And if you happen to spot other leaks ruining your embedded platforms, please let us know, and we'll try to hunt them down.
Thanks,
Signature · 56 lines
Jean Delvare
-------------- next part --------------
Index: lib/access.c
=================================RCS file: /home/cvs/lm_sensors2/lib/access.c,v
retrieving revision 1.25
diff -u -r1.25 access.c
--- lib/access.c 19 Sep 2004 17:37:18 -0000 1.25
+++ lib/access.c 22 May 2005 13:50:33 -0000
@@ -139,14 +139,12 @@
/* Look up the label which belongs to this chip. Note that chip should not
contain wildcard values! *result is newly allocated (free it yourself).
- This function will return 0 on success, and <0 on failure.
- Changed behaviour as of libsensors.so.1: now we actually take notice
- of the logical class, as documented, but not done before! */
+ This function will return 0 on success, and <0 on failure.
+ If no label exists for this feature, its name is returned itself. */
int sensors_get_label(sensors_chip_name name, int feature, char **result)
{
const sensors_chip *chip;
const sensors_chip_feature *featureptr;
- const sensors_chip_feature *alt_featureptr;
int i;
*result=NULL;
@@ -154,12 +152,7 @@
return -SENSORS_ERR_WILDCARDS;
if (! (featureptr = sensors_lookup_feature_nr(name.prefix,feature)))
return -SENSORS_ERR_NO_ENTRY;
- if (featureptr->logical_mapping = SENSORS_NO_MAPPING)
- alt_featureptr = NULL;
- else if (! (alt_featureptr - sensors_lookup_feature_nr(name.prefix,
- featureptr->logical_mapping)))
- return -SENSORS_ERR_NO_ENTRY;
+
for (chip = NULL; (chip = sensors_for_all_config_chips(name,chip));)
for (i = 0; i < chip->labels_count; i++)
if (!strcasecmp(featureptr->name, chip->labels[i].name)) {
@@ -167,14 +160,10 @@
free(*result);
if (! (*result = strdup(chip->labels[i].value)))
sensors_fatal_error("sensors_get_label","Allocating label text");
- return 0; /* Exact match always overrules! */
- } else if (alt_featureptr &&
- !strcasecmp(alt_featureptr->name, chip->labels[i].name)) {
- if (*result)
- free(*result);
- if (! (*result = strdup(chip->labels[i].value)))
- sensors_fatal_error("sensors_get_label","Allocating label text");
+ return 0;
}
+
+ /* No label, return the feature name instead */
if (! (*result = strdup(featureptr->name)))
sensors_fatal_error("sensors_get_label","Allocating label text");
return 0;Jean,
thanks for looking at this.
Quoted 3 lines · [email protected] said:
Thanks a lot for looking into this. We know there are a few leaks in libsensors, most of which are in the configuration file parser, and none of us know how to fix them.
Yes, sensors_parse_chip_name() definitely leaks memory too; however as
the file parsing stuff isn't usually called repeatedly, I'm less concerned about this.
Quoted 2 lines · [email protected] said:
We can't blame you for not understanding how that code works. It's not exactly elegant, let alone the terrible coding style and lack of comments.
Well, I didn't want to say anything...! In my experience, not using
braces for conditionals and (especially) loops usually leads to bugs, and hard-to-spot ones at that.
Quoted 2 lines · [email protected] said:
Yes of course. Leaks are bad anyway, so let's kill that one. What tool are you using BTW? sensord?
No, we have a home-brew module that exports sensor information via
SNMP.
Quoted 3 lines · [email protected] said:
I've attached my proposed patch. Please give it a try. If it works for you and nobody objects to the way it changes the function, I'll apply it to
lm_sensorsCVS.
I've given this a try, and it works without leaks. It does change the
output -- we have a section like this in sensors.conf:
label temp1 "CPU"
set temp1_over 55
set temp1_hyst 53Previously we would get "temp1_hyst" returned as a label; now we just get "temp1_low". This isn't a problem for us; is it what you would have expected though?
Quoted 2 lines · [email protected] said:
And if you happen to spot other leaks ruining your embedded platforms, please let us know, and we'll try to hunt them down.
Will do! Thanks again for your response.
John
Signature · 2 lines
Dr. John Naylon Cambridge Broadband Ltd. // www.cambridgebroadband.com
Hi John,
Quoted 5 lines
Quoted 2 lines
Yes of course. Leaks are bad anyway, so let's kill that one. What tool are you using BTW? sensord?
No, we have a home-brew module that exports sensor information via SNMP.
Is there somewhere other users could download this module? We had several users complaining about the lack of SNMP integration in lm_sensors, maybe what you wrote could be of some help to them.
Quoted 10 lines
I've given this a try, and it works without leaks. It does change the output -- we have a section like this in sensors.conf:
label temp1 "CPU" set temp1_over 55 set temp1_hyst 53Previously we would get "
temp1_hyst" returned as a label; now we just get "temp1_low". This isn't a problem for us; is it what you would have expected though?
Not really, I have to admit I am surprised, especially since I did not modify that very part of the code. Could you please verify that the patch itself, and not a version change (2.9.1 vs. CVS maybe?) causes the change in the output? I know we did fix a number of low limit vs. hysteresis name issues some times ago, this would more likely be related.
Also, we would need to know what kernel version you are using, and which chip is used for hardware monitoring on this system.
--
Jean Delvare
Quoted 5 lines · [email protected] said:
Quoted 2 lines
No, we have a home-brew module that exports sensor information via SNMP.
Is there somewhere other users could download this module? We had several users complaining about the lack of SNMP integration in
lm_sensors, maybe what you wrote could be of some help to them.
This specific module is very application-specific, so it's unlikely to be
of much use to anyone else. However, I am a member of the Net-SNMP project, and sensor-related things are happening there, so watch this space.
Quoted 5 lines · [email protected] said:
Not really, I have to admit I am surprised, especially since I did not modify that very part of the code. Could you please verify that the patch itself, and not a version change (2.9.1 vs. CVS maybe?) causes the change in the output? I know we did fix a number of low limit vs. hysteresis name issues some times ago, this would more likely be related.
You are quite correct -- my mistake. The patch *does not* cause any
changed in output; I was comparing against a much older version.
John
Signature · 2 lines
Dr. John Naylon Cambridge Broadband Ltd. // www.cambridgebroadband.com