library: introduced 'Item_table' validation provisions

The recent work on updating the <meminfo> and <vmstat>
modules with some newly added linux fields reminded me
(again) of a need for some mechanism guaranteeing that
a header file agrees with the source file assumptions.

Sadly, in the past, if a table entry was omitted or if
the table and header are ordered differently, then the
library would silently return the wrong results values
or even potentially experience a SIGSEGV abnormal end.

This patch offers a much needed development assist for
ensuring that Item_table entries are synchronized with
header file enumerators in terms of number plus order.

It's intended solely for our use as libprocps evolves.

Now, by activating ITEMTABLE_DEBUG, either directly or
via ./configure CFLAGS='-DITEMTABLE_DEBUG', the number
and order will be verified. It is envisioned that this
feature will be used at least once prior to a release.

Signed-off-by: Jim Warner <james.warner@comcast.net>
This commit is contained in:
Jim Warner
2020-08-10 00:00:00 -05:00
committed by Craig Small
parent c865b06c30
commit 92d0297e1e
6 changed files with 199 additions and 23 deletions

View File

@ -35,6 +35,13 @@
#define MEMINFO_FILE "/proc/meminfo"
#define MEMINFO_BUFF 8192
/* ------------------------------------------------------------------------- +
this provision can be used to ensure that our Item_table was synchronized |
with those enumerators found in the associated header file. It's intended |
to only be used locally (& temporarily) at some point prior to a release! | */
// #define ITEMTABLE_DEBUG //----------------------------------------------- |
// ------------------------------------------------------------------------- +
struct meminfo_data {
unsigned long Active;
@ -284,7 +291,11 @@ HST_set(SWAP_DELTA_USED, s_int, derived_swap_used)
// ___ Controlling Table ||||||||||||||||||||||||||||||||||||||||||||||||||||||
typedef void (*SET_t)(struct meminfo_result *, struct mem_hist *);
#ifdef ITEMTABLE_DEBUG
#define RS(e) (SET_t)setNAME(e), MEMINFO_ ## e, STRINGIFY(MEMINFO_ ## e)
#else
#define RS(e) (SET_t)setNAME(e)
#endif
#define TS(t) STRINGIFY(t)
#define TS_noop ""
@ -295,6 +306,10 @@ typedef void (*SET_t)(struct meminfo_result *, struct mem_hist *);
* those 'enum meminfo_item' guys ! */
static struct {
SET_t setsfunc; // the actual result setting routine
#ifdef ITEMTABLE_DEBUG
int enumnumb; // enumerator (must match position!)
char *enum2str; // enumerator name as a char* string
#endif
char *type2str; // the result type as a string value
} Item_table[] = {
/* setsfunc type2str
@ -435,9 +450,6 @@ static struct {
{ RS(SWAP_DELTA_FREE), TS(s_int) },
{ RS(SWAP_DELTA_TOTAL), TS(s_int) },
{ RS(SWAP_DELTA_USED), TS(s_int) },
// dummy entry corresponding to MEMINFO_logical_end ...
{ NULL, NULL }
};
/* please note,
@ -787,6 +799,23 @@ PROCPS_EXPORT int procps_meminfo_new (
{
struct meminfo_info *p;
#ifdef ITEMTABLE_DEBUG
int i, failed = 0;
for (i = 0; i < MAXTABLE(Item_table); i++) {
if (i != Item_table[i].enumnumb) {
fprintf(stderr, "%s: enum/table error: Item_table[%d] was %s, but its value is %d\n"
, __FILE__, i, Item_table[i].enum2str, Item_table[i].enumnumb);
failed = 1;
}
}
if (i != MEMINFO_logical_end) {
fprintf(stderr, "%s: MEMINFO_logical_end is %d, expected %d\n"
, __FILE__, MEMINFO_logical_end, i);
failed = 1;
}
if (failed) _Exit(EXIT_FAILURE);
#endif
if (info == NULL || *info != NULL)
return -EINVAL;
if (!(p = calloc(1, sizeof(struct meminfo_info))))