library: the uref functions were insufficiently robust
The earlier attempt at protecting these functions from
already freed memory worked just fine until the memory
was, in fact, reused by the OS. At that point, the ref
count would most likely fail an existing a test for 0.
So this commit will take control of the 'info' pointer
and force it to NULL when a reference count reaches 0.
Plus, since it makes little sense returning an address
that a caller already has, henceforth we will return a
reference count out of the 'ref' and 'unref functions.
Reference(s):
commit 74beff80ff
Signed-off-by: Jim Warner <james.warner@comcast.net>
This commit is contained in:
@ -125,25 +125,27 @@ PROCPS_EXPORT int procps_vmstat_read (
|
||||
return 0;
|
||||
}
|
||||
|
||||
PROCPS_EXPORT struct procps_vmstat *procps_vmstat_ref (
|
||||
PROCPS_EXPORT int procps_vmstat_ref (
|
||||
struct procps_vmstat *info)
|
||||
{
|
||||
if (info == NULL)
|
||||
return NULL;
|
||||
return -EINVAL;
|
||||
info->refcount++;
|
||||
return info;
|
||||
return info->refcount;
|
||||
}
|
||||
|
||||
PROCPS_EXPORT struct procps_vmstat *procps_vmstat_unref (
|
||||
struct procps_vmstat *info)
|
||||
PROCPS_EXPORT int procps_vmstat_unref (
|
||||
struct procps_vmstat **info)
|
||||
{
|
||||
if (info == NULL || info->refcount == 0)
|
||||
return NULL;
|
||||
info->refcount--;
|
||||
if (info->refcount > 0)
|
||||
return info;
|
||||
free(info);
|
||||
return NULL;
|
||||
if (info == NULL || *info == NULL)
|
||||
return -EINVAL;
|
||||
(*info)->refcount--;
|
||||
if ((*info)->refcount == 0) {
|
||||
free(*info);
|
||||
*info = NULL;
|
||||
return 0;
|
||||
}
|
||||
return (*info)->refcount;
|
||||
}
|
||||
|
||||
/* Accessor functions */
|
||||
|
Reference in New Issue
Block a user