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:
parent
ff852103f8
commit
faf6d4dc93
@ -214,25 +214,27 @@ PROCPS_EXPORT int procps_meminfo_read (
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
PROCPS_EXPORT struct procps_meminfo *procps_meminfo_ref (
|
PROCPS_EXPORT int procps_meminfo_ref (
|
||||||
struct procps_meminfo *info)
|
struct procps_meminfo *info)
|
||||||
{
|
{
|
||||||
if (info == NULL)
|
if (info == NULL)
|
||||||
return NULL;
|
return -EINVAL;
|
||||||
info->refcount++;
|
info->refcount++;
|
||||||
return info;
|
return info->refcount;
|
||||||
}
|
}
|
||||||
|
|
||||||
PROCPS_EXPORT struct procps_meminfo *procps_meminfo_unref (
|
PROCPS_EXPORT int procps_meminfo_unref (
|
||||||
struct procps_meminfo *info)
|
struct procps_meminfo **info)
|
||||||
{
|
{
|
||||||
if (info == NULL || info->refcount == 0)
|
if (info == NULL || *info == NULL)
|
||||||
return NULL;
|
return -EINVAL;
|
||||||
info->refcount--;
|
(*info)->refcount--;
|
||||||
if (info->refcount > 0)
|
if ((*info)->refcount == 0) {
|
||||||
return info;
|
free(*info);
|
||||||
free(info);
|
*info = NULL;
|
||||||
return NULL;
|
return 0;
|
||||||
|
}
|
||||||
|
return (*info)->refcount;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Accessor functions */
|
/* Accessor functions */
|
||||||
|
@ -58,8 +58,8 @@ struct procps_meminfo;
|
|||||||
int procps_meminfo_new (struct procps_meminfo **info);
|
int procps_meminfo_new (struct procps_meminfo **info);
|
||||||
int procps_meminfo_read (struct procps_meminfo *info);
|
int procps_meminfo_read (struct procps_meminfo *info);
|
||||||
|
|
||||||
struct procps_meminfo *procps_meminfo_ref (struct procps_meminfo *info);
|
int procps_meminfo_ref (struct procps_meminfo *info);
|
||||||
struct procps_meminfo *procps_meminfo_unref (struct procps_meminfo *info);
|
int procps_meminfo_unref (struct procps_meminfo **info);
|
||||||
|
|
||||||
unsigned long procps_meminfo_get (struct procps_meminfo *info, enum meminfo_item item);
|
unsigned long procps_meminfo_get (struct procps_meminfo *info, enum meminfo_item item);
|
||||||
int procps_meminfo_get_chain (struct procps_meminfo *info, struct meminfo_result *item);
|
int procps_meminfo_get_chain (struct procps_meminfo *info, struct meminfo_result *item);
|
||||||
|
@ -152,27 +152,29 @@ PROCPS_EXPORT int procps_stat_read (
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
PROCPS_EXPORT struct procps_statinfo *procps_stat_ref (
|
PROCPS_EXPORT int procps_stat_ref (
|
||||||
struct procps_statinfo *info)
|
struct procps_statinfo *info)
|
||||||
{
|
{
|
||||||
if (info == NULL)
|
if (info == NULL)
|
||||||
return NULL;
|
return -EINVAL;
|
||||||
info->refcount++;
|
info->refcount++;
|
||||||
return info;
|
return info->refcount;
|
||||||
}
|
}
|
||||||
|
|
||||||
PROCPS_EXPORT struct procps_statinfo *procps_stat_unref (
|
PROCPS_EXPORT int procps_stat_unref (
|
||||||
struct procps_statinfo *info)
|
struct procps_statinfo **info)
|
||||||
{
|
{
|
||||||
if (info == NULL || info->refcount == 0)
|
if (info == NULL || *info == NULL)
|
||||||
return NULL;
|
return -EINVAL;
|
||||||
info->refcount--;
|
(*info)->refcount--;
|
||||||
if (info->refcount > 0)
|
if ((*info)->refcount == 0) {
|
||||||
return info;
|
if ((*info)->jiff_hists != NULL)
|
||||||
if (info->jiff_hists)
|
free((*info)->jiff_hists);
|
||||||
free(info->jiff_hists);
|
free(*info);
|
||||||
free(info);
|
*info = NULL;
|
||||||
return NULL;
|
return 0;
|
||||||
|
}
|
||||||
|
return (*info)->refcount;
|
||||||
}
|
}
|
||||||
|
|
||||||
PROCPS_EXPORT jiff procps_stat_get_cpu (
|
PROCPS_EXPORT jiff procps_stat_get_cpu (
|
||||||
|
@ -74,8 +74,8 @@ int procps_stat_new (struct procps_statinfo **info);
|
|||||||
int procps_stat_read (struct procps_statinfo *info, const int cpu_only);
|
int procps_stat_read (struct procps_statinfo *info, const int cpu_only);
|
||||||
int procps_stat_read_jiffs (struct procps_statinfo *info);
|
int procps_stat_read_jiffs (struct procps_statinfo *info);
|
||||||
|
|
||||||
struct procps_statinfo *procps_stat_ref (struct procps_statinfo *info);
|
int procps_stat_ref (struct procps_statinfo *info);
|
||||||
struct procps_statinfo *procps_stat_unref (struct procps_statinfo *info);
|
int procps_stat_unref (struct procps_statinfo **info);
|
||||||
|
|
||||||
jiff procps_stat_get_cpu (struct procps_statinfo *info, enum procps_cpu_item item);
|
jiff procps_stat_get_cpu (struct procps_statinfo *info, enum procps_cpu_item item);
|
||||||
int procps_stat_get_cpu_chain (struct procps_statinfo *info, struct procps_cpu_result *item);
|
int procps_stat_get_cpu_chain (struct procps_statinfo *info, struct procps_cpu_result *item);
|
||||||
|
@ -125,25 +125,27 @@ PROCPS_EXPORT int procps_vmstat_read (
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
PROCPS_EXPORT struct procps_vmstat *procps_vmstat_ref (
|
PROCPS_EXPORT int procps_vmstat_ref (
|
||||||
struct procps_vmstat *info)
|
struct procps_vmstat *info)
|
||||||
{
|
{
|
||||||
if (info == NULL)
|
if (info == NULL)
|
||||||
return NULL;
|
return -EINVAL;
|
||||||
info->refcount++;
|
info->refcount++;
|
||||||
return info;
|
return info->refcount;
|
||||||
}
|
}
|
||||||
|
|
||||||
PROCPS_EXPORT struct procps_vmstat *procps_vmstat_unref (
|
PROCPS_EXPORT int procps_vmstat_unref (
|
||||||
struct procps_vmstat *info)
|
struct procps_vmstat **info)
|
||||||
{
|
{
|
||||||
if (info == NULL || info->refcount == 0)
|
if (info == NULL || *info == NULL)
|
||||||
return NULL;
|
return -EINVAL;
|
||||||
info->refcount--;
|
(*info)->refcount--;
|
||||||
if (info->refcount > 0)
|
if ((*info)->refcount == 0) {
|
||||||
return info;
|
free(*info);
|
||||||
free(info);
|
*info = NULL;
|
||||||
return NULL;
|
return 0;
|
||||||
|
}
|
||||||
|
return (*info)->refcount;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Accessor functions */
|
/* Accessor functions */
|
||||||
|
@ -45,8 +45,8 @@ struct procps_vmstat;
|
|||||||
int procps_vmstat_new (struct procps_vmstat **info);
|
int procps_vmstat_new (struct procps_vmstat **info);
|
||||||
int procps_vmstat_read (struct procps_vmstat *info);
|
int procps_vmstat_read (struct procps_vmstat *info);
|
||||||
|
|
||||||
struct procps_vmstat *procps_vmstat_ref (struct procps_vmstat *info);
|
int procps_vmstat_ref (struct procps_vmstat *info);
|
||||||
struct procps_vmstat *procps_vmstat_unref (struct procps_vmstat *info);
|
int procps_vmstat_unref (struct procps_vmstat **info);
|
||||||
|
|
||||||
unsigned long procps_vmstat_get (struct procps_vmstat *info, enum vmstat_item item);
|
unsigned long procps_vmstat_get (struct procps_vmstat *info, enum vmstat_item item);
|
||||||
int procps_vmstat_get_chain (struct procps_vmstat *info, struct vmstat_result *item);
|
int procps_vmstat_get_chain (struct procps_vmstat *info, struct vmstat_result *item);
|
||||||
|
@ -106,7 +106,7 @@ static void get_boot_time(void)
|
|||||||
xerrx(EXIT_FAILURE,
|
xerrx(EXIT_FAILURE,
|
||||||
_("Unable to read system stat information"));
|
_("Unable to read system stat information"));
|
||||||
boot_time = procps_stat_get_sys(sys_info, PROCPS_STAT_BTIME);
|
boot_time = procps_stat_get_sys(sys_info, PROCPS_STAT_BTIME);
|
||||||
procps_stat_unref(sys_info);
|
procps_stat_unref(&sys_info);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void get_memory_total()
|
static void get_memory_total()
|
||||||
@ -119,7 +119,7 @@ static void get_memory_total()
|
|||||||
xerrx(EXIT_FAILURE,
|
xerrx(EXIT_FAILURE,
|
||||||
_("Unable to read meminfo information"));
|
_("Unable to read meminfo information"));
|
||||||
memory_total = procps_meminfo_get(mem_info, PROCPS_MEM_TOTAL);
|
memory_total = procps_meminfo_get(mem_info, PROCPS_MEM_TOTAL);
|
||||||
procps_meminfo_unref(mem_info);
|
procps_meminfo_unref(&mem_info);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*************************************************************************/
|
/*************************************************************************/
|
||||||
|
@ -578,8 +578,8 @@ static void bye_bye (const char *str) {
|
|||||||
#endif // end: ATEOJ_RPTHSH
|
#endif // end: ATEOJ_RPTHSH
|
||||||
#endif // end: OFF_HST_HASH
|
#endif // end: OFF_HST_HASH
|
||||||
|
|
||||||
procps_stat_unref(sys_info);
|
procps_stat_unref(&sys_info);
|
||||||
procps_meminfo_unref(mem_info);
|
procps_meminfo_unref(&mem_info);
|
||||||
#ifndef NUMA_DISABLE
|
#ifndef NUMA_DISABLE
|
||||||
if (Libnuma_handle) dlclose(Libnuma_handle);
|
if (Libnuma_handle) dlclose(Libnuma_handle);
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user