hv: fix 'Pointer arithmetic is not on array'

Violation 'Pointer arithmetic is not on array' occurs in following
statement:
        *flags |= fl[pos - flagchars];

char flagchars[] is a well defined array.
It could be used directly to avoid the pointer arithmetic.

v1 -> v2:
 * use uint32_t rather than uint8_t for the index in order to let the
    type aligned with sizeof(flagchars)
 * add brackets to the then-block

Signed-off-by: Shiqing Gao <shiqing.gao@intel.com>
Reviewed-by: Junjie Mao <junjie.mao@intel.com>
This commit is contained in:
Shiqing Gao 2018-07-10 13:31:14 +08:00 committed by lijinxia
parent 5cb9972919
commit 4446864eff
1 changed files with 15 additions and 6 deletions

View File

@ -117,20 +117,29 @@ static const char *get_flags(const char *s, int *flags)
PRINT_FLAG_SIGN, /* + */
PRINT_FLAG_SPACE /* ' ' */
};
const char *pos;
uint32_t i;
bool found;
/* parse multiple flags */
while ((*s) != 0) {
/* get index of flag. Terminate loop if no flag character was
* found
/*
* Get index of flag.
* Terminate loop if no flag character was found.
*/
pos = strchr(flagchars, *s);
if (pos == NULL)
found = false;
for (i = 0U; i < sizeof(flagchars); i++) {
if (*s == flagchars[i]) {
found = true;
break;
}
}
if (!found) {
break;
}
/* apply matching flags and continue with the next character */
++s;
*flags |= fl[pos - flagchars];
*flags |= fl[i];
}
/* Spec says that '-' has a higher priority than '0' */