Skip to content

Commit 4446864

Browse files
shiqingglijinxia
authored andcommitted
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>
1 parent 5cb9972 commit 4446864

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

hypervisor/lib/sprintf.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,20 +117,29 @@ static const char *get_flags(const char *s, int *flags)
117117
PRINT_FLAG_SIGN, /* + */
118118
PRINT_FLAG_SPACE /* ' ' */
119119
};
120-
const char *pos;
120+
uint32_t i;
121+
bool found;
121122

122123
/* parse multiple flags */
123124
while ((*s) != 0) {
124-
/* get index of flag. Terminate loop if no flag character was
125-
* found
125+
/*
126+
* Get index of flag.
127+
* Terminate loop if no flag character was found.
126128
*/
127-
pos = strchr(flagchars, *s);
128-
if (pos == NULL)
129+
found = false;
130+
for (i = 0U; i < sizeof(flagchars); i++) {
131+
if (*s == flagchars[i]) {
132+
found = true;
133+
break;
134+
}
135+
}
136+
if (!found) {
129137
break;
138+
}
130139

131140
/* apply matching flags and continue with the next character */
132141
++s;
133-
*flags |= fl[pos - flagchars];
142+
*flags |= fl[i];
134143
}
135144

136145
/* Spec says that '-' has a higher priority than '0' */

0 commit comments

Comments
 (0)