library: protect against possible 'refcount' underflow

In each module employing a priming read at 'new' time,
should that read fail, a call to 'unref' will be made.

However, there is a hidden dependency that these calls
must never occur before the context 'refcount' was set
due to the way an 'unref' conditional was constructed.

So this commit just ensures that 'unref' will function
as expected, even if called with a 'refcount' of zero.

Signed-off-by: Jim Warner <james.warner@comcast.net>
This commit is contained in:
Jim Warner 2016-11-22 10:10:10 -05:00 committed by Craig Small
parent b3e8581a30
commit e70531a945
6 changed files with 14 additions and 8 deletions

View File

@ -800,7 +800,8 @@ PROCPS_EXPORT int procps_diskstats_unref (
return -EINVAL;
(*info)->refcount--;
if ((*info)->refcount == 0) {
if ((*info)->refcount < 1) {
if ((*info)->diskstats_fp) {
fclose((*info)->diskstats_fp);
(*info)->diskstats_fp = NULL;
@ -828,7 +829,6 @@ PROCPS_EXPORT int procps_diskstats_unref (
free(*info);
*info = NULL;
return 0;
}
return (*info)->refcount;

View File

@ -778,14 +778,16 @@ PROCPS_EXPORT int procps_meminfo_unref (
{
if (info == NULL || *info == NULL)
return -EINVAL;
(*info)->refcount--;
if ((*info)->refcount == 0) {
if ((*info)->refcount < 1) {
if ((*info)->extents)
meminfo_extents_free_all((*info));
if ((*info)->items)
free((*info)->items);
hdestroy_r(&(*info)->hashtab);
free(*info);
*info = NULL;
return 0;

View File

@ -1180,7 +1180,8 @@ PROCPS_EXPORT int procps_pids_unref (
return -EINVAL;
(*info)->refcount--;
if ((*info)->refcount == 0) {
if ((*info)->refcount < 1) {
#ifdef UNREF_RPTHASH
pids_unref_rpthash(*info);
#endif

View File

@ -843,7 +843,8 @@ PROCPS_EXPORT int procps_slabinfo_unref (
return -EINVAL;
(*info)->refcount--;
if ((*info)->refcount == 0) {
if ((*info)->refcount < 1) {
if ((*info)->slabinfo_fp) {
fclose((*info)->slabinfo_fp);
(*info)->slabinfo_fp = NULL;
@ -867,7 +868,6 @@ PROCPS_EXPORT int procps_slabinfo_unref (
free(*info);
*info = NULL;
return 0;
}
return (*info)->refcount;

View File

@ -901,9 +901,10 @@ PROCPS_EXPORT int procps_stat_unref (
{
if (info == NULL || *info == NULL)
return -EINVAL;
(*info)->refcount--;
if ((*info)->refcount == 0) {
if ((*info)->refcount < 1) {
if ((*info)->cpus.anchor)
free((*info)->cpus.anchor);
if ((*info)->cpus.result.stacks)

View File

@ -1162,14 +1162,16 @@ PROCPS_EXPORT int procps_vmstat_unref (
{
if (info == NULL || *info == NULL)
return -EINVAL;
(*info)->refcount--;
if ((*info)->refcount == 0) {
if ((*info)->refcount < 1) {
if ((*info)->extents)
vmstat_extents_free_all((*info));
if ((*info)->items)
free((*info)->items);
hdestroy_r(&(*info)->hashtab);
free(*info);
*info = NULL;
return 0;