library: correct 'vectorized' string logic, <PIDS> api

The commit referenced below addressed (some) anomalies
surrounding 'strv' pointers. However, there remained a
couple quirks involving a potential NULL return value.

Any NULL values returned from the old library readproc
guys would cause no real harm for newlib. But they did
produce the misleading "[ duplicate ENUM_ID ]" result.

The following all represent potential NULL results and
suggest shortcomings in testing of that earlier patch.

. kernel threads do not have cgroup, cmdline & environ
. even if present environ could require root to access

So, this patch reverts a portion of the earlier commit
and ensures when some vectored string is not available
a traditional dash ('-') is the 'strv' returned value.

[ and we'll also correct one typo in the header file ]

Reference(s):
. eliminated a final potential NULL, <PIDS> api
commit 09503dc597

Signed-off-by: Jim Warner <james.warner@comcast.net>
This commit is contained in:
Jim Warner
2019-04-25 00:00:00 -05:00
committed by Craig Small
parent 30e27d48e9
commit a9bfb186eb
4 changed files with 43 additions and 27 deletions

View File

@ -94,25 +94,6 @@ struct pids_info {
// ___ Results 'Set' Support ||||||||||||||||||||||||||||||||||||||||||||||||||
static char** pids_vectorize_this (const char* src) {
#define pSZ (sizeof(char*))
char *cpy, **vec;
size_t adj, tot;
tot = strlen(src) + 1; // prep for our vectors
if (tot < 1 || tot >= INT_MAX) tot = INT_MAX-1; // integer overflow?
adj = (pSZ-1) - ((tot + pSZ-1) & (pSZ-1)); // calc alignment bytes
cpy = calloc(1, tot + adj + (2 * pSZ)); // get new larger buffer
if (!cpy) return NULL; // oops, looks like ENOMEM
snprintf(cpy, tot, "%s", src); // duplicate their string
vec = (char**)(cpy + tot + adj); // prep pointer to pointers
*vec = cpy; // point 1st vector to string
*(vec+1) = NULL; // null ptr 'list' delimit
return vec; // ==> free(*vec) to dealloc
#undef pSZ
} // end: pids_vectorize_this
#define setNAME(e) set_pids_ ## e
#define setDECL(e) static void setNAME(e) \
(struct pids_info *I, struct pids_result *R, proc_t *P)
@ -136,7 +117,7 @@ static char** pids_vectorize_this (const char* src) {
some sort of hint that they duplicated this char ** item ... */
#define VEC_set(e,x) setDECL(e) { \
if (NULL != P-> x) { R->result.strv = P-> x; P-> x = NULL; } \
else { R->result.strv = pids_vectorize_this("[ duplicate " STRINGIFY(e) " ]"); \
else { R->result.strv = vectorize_this_str("[ duplicate " STRINGIFY(e) " ]"); \
if (!R->result.str) I->seterr = 1; } }