library: strengthen the VAL macro validation functions

One ought not to assume that random memory access will
always succeed or, when it does, that an obviously bad
item enumerator will always be found at that location.

Thus, this patch corrects some really poor assumptions
associated with the 'xtra_procps_debug.h' header file.

[ and it does so in somewhat contorted ways so as to ]
[ avoid several darn gcc -Wnonnull warning messages! ]

Signed-off-by: Jim Warner <james.warner@comcast.net>
This commit is contained in:
Jim Warner 2016-08-08 01:23:45 -05:00 committed by Craig Small
parent 6b6ad95f40
commit 9f27e9d8d9
7 changed files with 102 additions and 72 deletions

View File

@ -1014,7 +1014,7 @@ PROCPS_EXPORT struct diskstats_result *xtra_diskstats_get (
} // end: xtra_diskstats_get_
PROCPS_EXPORT void xtra_diskstats_val (
PROCPS_EXPORT struct diskstats_result *xtra_diskstats_val (
int relative_enum,
const char *typestr,
const struct diskstats_stack *stack,
@ -1022,17 +1022,21 @@ PROCPS_EXPORT void xtra_diskstats_val (
const char *file,
int lineno)
{
struct diskstats_result *r;
char *str;
int i;
r = &stack->head[relative_enum];
if (r->item < 0 || r->item >= DISKSTATS_logical_end) {
fprintf(stderr, "%s line %d: invalid item = %d, relative_enum = %d, type = %s\n"
, file, lineno, r->item, relative_enum, typestr);
return;
for (i = 0; stack->head[i].item < DISKSTATS_logical_end; i++)
;
if (relative_enum < 0 || relative_enum >= i) {
fprintf(stderr, "%s line %d: invalid relative_enum = %d, type = %s\n"
, file, lineno, relative_enum, typestr);
return NULL;
}
str = Item_table[r->item].type2str;
str = Item_table[stack->head[relative_enum].item].type2str;
if (str[0]
&& (strcmp(typestr, str)))
&& (strcmp(typestr, str))) {
fprintf(stderr, "%s line %d: was %s, expected %s\n", file, lineno, typestr, str);
return NULL;
}
return &stack->head[relative_enum];
} // end: xtra_diskstats_val

View File

@ -886,7 +886,7 @@ PROCPS_EXPORT struct meminfo_result *xtra_meminfo_get (
} // end: xtra_meminfo_get_
PROCPS_EXPORT void xtra_meminfo_val (
PROCPS_EXPORT struct meminfo_result *xtra_meminfo_val (
int relative_enum,
const char *typestr,
const struct meminfo_stack *stack,
@ -894,17 +894,21 @@ PROCPS_EXPORT void xtra_meminfo_val (
const char *file,
int lineno)
{
struct meminfo_result *r;
char *str;
int i;
r = &stack->head[relative_enum];
if (r->item < 0 || r->item >= MEMINFO_logical_end) {
fprintf(stderr, "%s line %d: invalid item = %d, relative_enum = %d, type = %s\n"
, file, lineno, r->item, relative_enum, typestr);
return;
for (i = 0; stack->head[i].item < MEMINFO_logical_end; i++)
;
if (relative_enum < 0 || relative_enum >= i) {
fprintf(stderr, "%s line %d: invalid relative_enum = %d, type = %s\n"
, file, lineno, relative_enum, typestr);
return NULL;
}
str = Item_table[r->item].type2str;
str = Item_table[stack->head[relative_enum].item].type2str;
if (str[0]
&& (strcmp(typestr, str)))
&& (strcmp(typestr, str))) {
fprintf(stderr, "%s line %d: was %s, expected %s\n", file, lineno, typestr, str);
return NULL;
}
return &stack->head[relative_enum];
} // end: xtra_meminfo_val

View File

@ -1503,7 +1503,7 @@ PROCPS_EXPORT struct pids_stack **procps_pids_sort (
* 2) the '#include <proc/xtra-procps-debug.h>' used
*/
PROCPS_EXPORT void xtra_pids_val (
PROCPS_EXPORT struct pids_result *xtra_pids_val (
int relative_enum,
const char *typestr,
const struct pids_stack *stack,
@ -1511,17 +1511,21 @@ PROCPS_EXPORT void xtra_pids_val (
const char *file,
int lineno)
{
struct pids_result *r;
char *str;
int i;
r = &stack->head[relative_enum];
if (r->item < 0 || r->item >= PIDS_logical_end) {
fprintf(stderr, "%s line %d: invalid item = %d, relative_enum = %d, type = %s\n"
, file, lineno, r->item, relative_enum, typestr);
return;
for (i = 0; stack->head[i].item < PIDS_logical_end; i++)
;
if (relative_enum < 0 || relative_enum >= i) {
fprintf(stderr, "%s line %d: invalid relative_enum = %d, type = %s\n"
, file, lineno, relative_enum, typestr);
return NULL;
}
str = Item_table[r->item].type2str;
str = Item_table[stack->head[relative_enum].item].type2str;
if (str[0]
&& (strcmp(typestr, str)))
&& (strcmp(typestr, str))) {
fprintf(stderr, "%s line %d: was %s, expected %s\n", file, lineno, typestr, str);
return NULL;
}
return &stack->head[relative_enum];
} // end: xtra_pids_val

View File

@ -1043,7 +1043,7 @@ PROCPS_EXPORT struct slabinfo_result *xtra_slabinfo_get (
} // end: xtra_slabinfo_get_
PROCPS_EXPORT void xtra_slabinfo_val (
PROCPS_EXPORT struct slabinfo_result *xtra_slabinfo_val (
int relative_enum,
const char *typestr,
const struct slabinfo_stack *stack,
@ -1051,17 +1051,21 @@ PROCPS_EXPORT void xtra_slabinfo_val (
const char *file,
int lineno)
{
struct slabinfo_result *r;
char *str;
int i;
r = &stack->head[relative_enum];
if (r->item < 0 || r->item >= SLABINFO_logical_end) {
fprintf(stderr, "%s line %d: invalid item = %d, relative_enum = %d, type = %s\n"
, file, lineno, r->item, relative_enum, typestr);
return;
for (i = 0; stack->head[i].item < SLABINFO_logical_end; i++)
;
if (relative_enum < 0 || relative_enum >= i) {
fprintf(stderr, "%s line %d: invalid relative_enum = %d, type = %s\n"
, file, lineno, relative_enum, typestr);
return NULL;
}
str = Item_table[r->item].type2str;
str = Item_table[stack->head[relative_enum].item].type2str;
if (str[0]
&& (strcmp(typestr, str)))
&& (strcmp(typestr, str))) {
fprintf(stderr, "%s line %d: was %s, expected %s\n", file, lineno, typestr, str);
return NULL;
}
return &stack->head[relative_enum];
} // end: xtra_slabinfo_val

View File

@ -1161,7 +1161,7 @@ PROCPS_EXPORT struct stat_result *xtra_stat_get (
} // end: xtra_stat_get_
PROCPS_EXPORT void xtra_stat_val (
PROCPS_EXPORT struct stat_result *xtra_stat_val (
int relative_enum,
const char *typestr,
const struct stat_stack *stack,
@ -1169,17 +1169,21 @@ PROCPS_EXPORT void xtra_stat_val (
const char *file,
int lineno)
{
struct stat_result *r;
char *str;
int i;
r = &stack->head[relative_enum];
if (r->item < 0 || r->item >= STAT_logical_end) {
fprintf(stderr, "%s line %d: invalid item = %d, relative_enum = %d, type = %s\n"
, file, lineno, r->item, relative_enum, typestr);
return;
for (i = 0; stack->head[i].item < STAT_logical_end; i++)
;
if (relative_enum < 0 || relative_enum >= i) {
fprintf(stderr, "%s line %d: invalid relative_enum = %d, type = %s\n"
, file, lineno, relative_enum, typestr);
return NULL;
}
str = Item_table[r->item].type2str;
str = Item_table[stack->head[relative_enum].item].type2str;
if (str[0]
&& (strcmp(typestr, str)))
&& (strcmp(typestr, str))) {
fprintf(stderr, "%s line %d: was %s, expected %s\n", file, lineno, typestr, str);
return NULL;
}
return &stack->head[relative_enum];
} // end: xtra_stat_val

View File

@ -1282,7 +1282,7 @@ PROCPS_EXPORT struct vmstat_result *xtra_vmstat_get (
} // end: xtra_vmstat_get_
PROCPS_EXPORT void xtra_vmstat_val (
PROCPS_EXPORT struct vmstat_result *xtra_vmstat_val (
int relative_enum,
const char *typestr,
const struct vmstat_stack *stack,
@ -1290,17 +1290,21 @@ PROCPS_EXPORT void xtra_vmstat_val (
const char *file,
int lineno)
{
struct vmstat_result *r;
char *str;
int i;
r = &stack->head[relative_enum];
if (r->item < 0 || r->item >= VMSTAT_logical_end) {
fprintf(stderr, "%s line %d: invalid item = %d, relative_enum = %d, type = %s\n"
, file, lineno, r->item, relative_enum, typestr);
return;
for (i = 0; stack->head[i].item < VMSTAT_logical_end; i++)
;
if (relative_enum < 0 || relative_enum >= i) {
fprintf(stderr, "%s line %d: invalid relative_enum = %d, type = %s\n"
, file, lineno, relative_enum, typestr);
return NULL;
}
str = Item_table[r->item].type2str;
str = Item_table[stack->head[relative_enum].item].type2str;
if (str[0]
&& (strcmp(typestr, str)))
&& (strcmp(typestr, str))) {
fprintf(stderr, "%s line %d: was %s, expected %s\n", file, lineno, typestr, str);
return NULL;
}
return &stack->head[relative_enum];
} // end: xtra_vmstat_val

View File

@ -43,7 +43,7 @@ struct diskstats_result *xtra_diskstats_get (
#ifdef DISKSTATS_VAL
#undef DISKSTATS_VAL
void xtra_diskstats_val (
struct diskstats_result *xtra_diskstats_val (
int relative_enum,
const char *typestr,
const struct diskstats_stack *stack,
@ -52,8 +52,9 @@ void xtra_diskstats_val (
int lineno);
#define DISKSTATS_VAL( relative_enum, type, stack, info ) ( { \
xtra_diskstats_val(relative_enum, STRINGIFY(type), stack, info, __FILE__, __LINE__); \
stack -> head [ relative_enum ] . result . type; } )
struct diskstats_result *r; \
r = xtra_diskstats_val(relative_enum, STRINGIFY(type), stack, info, __FILE__, __LINE__); \
r ? r->result . type : 0; } )
#endif // . . . . . . . . . .
@ -75,7 +76,7 @@ struct meminfo_result *xtra_meminfo_get (
#ifdef MEMINFO_VAL
#undef MEMINFO_VAL
void xtra_meminfo_val (
struct meminfo_result *xtra_meminfo_val (
int relative_enum,
const char *typestr,
const struct meminfo_stack *stack,
@ -84,15 +85,16 @@ void xtra_meminfo_val (
int lineno);
#define MEMINFO_VAL( relative_enum, type, stack, info ) ( { \
xtra_meminfo_val(relative_enum, STRINGIFY(type), stack, info, __FILE__, __LINE__); \
stack -> head [ relative_enum ] . result . type; } )
struct meminfo_result *r; \
r = xtra_meminfo_val(relative_enum, STRINGIFY(type), stack, info, __FILE__, __LINE__); \
r ? r->result . type : 0; } )
#endif // . . . . . . . . . .
// --- PIDS -----------------------------------------------
#ifdef PIDS_VAL
#undef PIDS_VAL
void xtra_pids_val (
struct pids_result *xtra_pids_val (
int relative_enum,
const char *typestr,
const struct pids_stack *stack,
@ -101,8 +103,9 @@ void xtra_pids_val (
int lineno);
#define PIDS_VAL( relative_enum, type, stack, info ) ( { \
xtra_pids_val(relative_enum, STRINGIFY(type), stack, info, __FILE__, __LINE__); \
stack -> head [ relative_enum ] . result . type; } )
struct pids_result *r; \
r = xtra_pids_val(relative_enum, STRINGIFY(type), stack, info, __FILE__, __LINE__); \
r ? r->result . type : 0; } )
#endif // . . . . . . . . . .
@ -124,7 +127,7 @@ struct slabinfo_result *xtra_slabinfo_get (
#ifdef SLABINFO_VAL
#undef SLABINFO_VAL
void xtra_slabinfo_val (
struct slabinfo_result *xtra_slabinfo_val (
int relative_enum,
const char *typestr,
const struct slabinfo_stack *stack,
@ -133,8 +136,9 @@ void xtra_slabinfo_val (
int lineno);
#define SLABINFO_VAL( relative_enum, type, stack, info ) ( { \
xtra_slabinfo_val(relative_enum, STRINGIFY(type), stack, info, __FILE__, __LINE__); \
stack -> head [ relative_enum ] . result . type; } )
struct slabinfo_result *r; \
r = xtra_slabinfo_val(relative_enum, STRINGIFY(type), stack, info, __FILE__, __LINE__); \
r ? r->result . type : 0; } )
#endif // . . . . . . . . . .
@ -156,7 +160,7 @@ struct stat_result *xtra_stat_get (
#ifdef STAT_VAL
#undef STAT_VAL
void xtra_stat_val (
struct stat_result *xtra_stat_val (
int relative_enum,
const char *typestr,
const struct stat_stack *stack,
@ -165,8 +169,9 @@ void xtra_stat_val (
int lineno);
#define STAT_VAL( relative_enum, type, stack, info ) ( { \
xtra_stat_val(relative_enum, STRINGIFY(type), stack, info, __FILE__, __LINE__); \
stack -> head [ relative_enum ] . result . type; } )
struct stat_result *r; \
r = xtra_stat_val(relative_enum, STRINGIFY(type), stack, info, __FILE__, __LINE__); \
r ? r->result . type : 0; } )
#endif // . . . . . . . . . .
@ -188,7 +193,7 @@ struct vmstat_result *xtra_vmstat_get (
#ifdef VMSTAT_VAL
#undef VMSTAT_VAL
void xtra_vmstat_val (
struct vmstat_result *xtra_vmstat_val (
int relative_enum,
const char *typestr,
const struct vmstat_stack *stack,
@ -197,8 +202,9 @@ void xtra_vmstat_val (
int lineno);
#define VMSTAT_VAL( relative_enum, type, stack, info ) ( { \
xtra_vmstat_val(relative_enum, STRINGIFY(type), stack, info, __FILE__, __LINE__); \
stack -> head [ relative_enum ] . result . type; } )
struct vmstat_result *r; \
r = xtra_vmstat_val(relative_enum, STRINGIFY(type), stack, info, __FILE__, __LINE__); \
r ? r->result . type : 0; } )
#endif // . . . . . . . . . .
#endif // end: XTRA_PROCPS_DEBUG_H