One-pass key-val pair parsing for bms and plugs

This commit is contained in:
Arun Prakash Jana 2020-04-15 20:43:26 +05:30
parent ac8b56ecc2
commit 425c0559a5
No known key found for this signature in database
GPG key ID: A75979F35C080412

View file

@ -2928,45 +2928,35 @@ static int xlink(char *prefix, char *path, char *curfname, char *buf, int *prese
static bool parsekvpair(kv **arr, char **envcpy, const uchar id, uchar *items)
{
uint maxitems = 0, i = 0;
const uchar INCR = 8;
uint i = 0;
char *nextkey;
kv *kvarr = NULL;
char *ptr = getenv(env_cfg[id]);
kv *kvarr;
if (!ptr || !*ptr)
return TRUE;
nextkey = ptr;
while (*nextkey)
if (*nextkey++ == ':')
++maxitems;
if (!maxitems || maxitems > 100)
return FALSE;
*arr = calloc(maxitems, sizeof(kv));
if (!arr) {
xerror();
return FALSE;
}
kvarr = *arr;
*envcpy = xstrdup(ptr);
if (!*envcpy) {
xerror();
return FALSE;
}
/* Clear trailing ;s */
if (*--nextkey == ';')
*(*envcpy + (nextkey - ptr)) = '\0';
ptr = *envcpy;
nextkey = ptr;
while (*ptr && i < maxitems) {
while (*ptr && i < 100) {
if (ptr == nextkey) {
if (!(i & (INCR - 1))) {
kvarr = xrealloc(kvarr, sizeof(kv) * (i + INCR));
*arr = kvarr;
if (!kvarr) {
xerror();
return FALSE;
}
memset(kvarr + i, 0, sizeof(kv) * INCR);
}
kvarr[i].key = (uchar)*ptr;
if (*++ptr != ':')
return FALSE;
@ -2986,18 +2976,8 @@ static bool parsekvpair(kv **arr, char **envcpy, const uchar id, uchar *items)
++ptr;
}
maxitems = i;
if (kvarr[i - 1].val && *kvarr[i - 1].val == '\0')
return FALSE;
/* Redundant check so far, all paths will get evaluated and fail */
//for (i = 0; i < maxitems && kvarr[i].key; ++i)
// if (xstrlen(kvarr[i].val) >= PATH_MAX)
// return FALSE;
*items = maxitems;
return TRUE;
*items = i;
return (i != 0);
}
/*