Skip to content

Commit 1af8586

Browse files
rarindamwenlingz
authored andcommitted
HV: Fix missing brackets for MISRA C Violations
Patch 7 of 7. Added changes to make sure Misra C violations are fixed for rules 11S and 12S. Signed-off-by: Arindam Roy <arindam.roy@intel.com>
1 parent af806a9 commit 1af8586

File tree

3 files changed

+106
-58
lines changed

3 files changed

+106
-58
lines changed

hypervisor/lib/memory.c

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,9 @@ static void *allocate_mem(struct mem_pool *pool, unsigned int num_bytes)
8080
for (bit_idx = ffz64(pool->bitmap[idx]);
8181
bit_idx < BITMAP_WORD_SIZE; bit_idx++) {
8282
/* Check if selected buffer is free */
83-
if ((pool->bitmap[idx] & (1U << bit_idx)) != 0U)
83+
if ((pool->bitmap[idx] & (1U << bit_idx)) != 0U) {
8484
continue;
85+
}
8586

8687
/* Declare temporary variables to be used locally in
8788
* this block
@@ -109,8 +110,9 @@ static void *allocate_mem(struct mem_pool *pool, unsigned int num_bytes)
109110

110111
/* Break if selected buffer is not free */
111112
if ((pool->bitmap[tmp_idx]
112-
& (1U << tmp_bit_idx)) != 0U)
113+
& (1U << tmp_bit_idx)) != 0U) {
113114
break;
115+
}
114116
}
115117

116118
/* Check if requested_buffs number of free contiguous
@@ -207,16 +209,18 @@ static void deallocate_mem(struct mem_pool *pool, void *ptr)
207209
contiguity_bitmask = &pool->contiguity_bitmap[bmp_idx];
208210

209211
/* Mark the buffer as free */
210-
if ((*bitmask & (1U << bit_idx)) != 0U)
212+
if ((*bitmask & (1U << bit_idx)) != 0U) {
211213
*bitmask ^= (1U << bit_idx);
212-
else
214+
} else {
213215
break;
216+
}
214217

215218
/* Reset the Contiguity bit of buffer */
216-
if ((*contiguity_bitmask & (1U << bit_idx)) != 0U)
219+
if ((*contiguity_bitmask & (1U << bit_idx)) != 0U) {
217220
*contiguity_bitmask ^= (1U << bit_idx);
218-
else
221+
} else {
219222
break;
223+
}
220224

221225
/* Increment buff_idx */
222226
buff_idx++;
@@ -245,8 +249,9 @@ void *malloc(unsigned int num_bytes)
245249
}
246250

247251
/* Check if memory allocation is successful */
248-
if (memory == NULL)
252+
if (memory == NULL) {
249253
pr_err("%s: failed to alloc 0x%x Bytes", __func__, num_bytes);
254+
}
250255

251256
/* Return memory pointer to caller */
252257
return memory;
@@ -260,8 +265,9 @@ void *alloc_pages(unsigned int page_num)
260265
memory = allocate_mem(&Paging_Memory_Pool, page_num * CPU_PAGE_SIZE);
261266

262267
/* Check if memory allocation is successful */
263-
if (memory == NULL)
268+
if (memory == NULL) {
264269
pr_err("%s: failed to alloc %d pages", __func__, page_num);
270+
}
265271

266272
return memory;
267273
}
@@ -311,8 +317,9 @@ void *memchr(const void *void_s, int c, size_t n)
311317
unsigned char *end = ptr + n;
312318

313319
while (ptr < end) {
314-
if (*ptr == val)
320+
if (*ptr == val) {
315321
return ((void *)ptr);
322+
}
316323
ptr++;
317324
}
318325
return NULL;
@@ -348,16 +355,19 @@ void *memcpy_s(void *d, size_t dmax, const void *s, size_t slen)
348355
uint8_t *dest8;
349356
uint8_t *src8;
350357

351-
if (slen == 0U || dmax == 0U || dmax < slen)
358+
if (slen == 0U || dmax == 0U || dmax < slen) {
352359
ASSERT(false);
360+
}
353361

354362
if ((d > s && d <= s + slen - 1)
355-
|| (d < s && s <= d + dmax - 1))
363+
|| (d < s && s <= d + dmax - 1)) {
356364
ASSERT(false);
365+
}
357366

358367
/*same memory block, no need to copy*/
359-
if (d == s)
368+
if (d == s) {
360369
return d;
370+
}
361371

362372
dest8 = (uint8_t *)d;
363373
src8 = (uint8_t *)s;

hypervisor/lib/sprintf.c

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,9 @@ static const char *get_int(const char *s, int *x)
9999
}
100100

101101
/* apply sign to result */
102-
if (negative != 0)
102+
if (negative != 0) {
103103
*x = -*x;
104+
}
104105

105106
return s;
106107
}
@@ -143,12 +144,14 @@ static const char *get_flags(const char *s, int *flags)
143144
}
144145

145146
/* Spec says that '-' has a higher priority than '0' */
146-
if ((*flags & PRINT_FLAG_LEFT_JUSTIFY) != 0)
147+
if ((*flags & PRINT_FLAG_LEFT_JUSTIFY) != 0) {
147148
*flags &= ~PRINT_FLAG_PAD_ZERO;
149+
}
148150

149151
/* Spec says that '+' has a higher priority than ' ' */
150-
if ((*flags & PRINT_FLAG_SIGN) != 0)
152+
if ((*flags & PRINT_FLAG_SIGN) != 0) {
151153
*flags &= ~PRINT_FLAG_SPACE;
154+
}
152155

153156
return s;
154157
}
@@ -167,15 +170,15 @@ static const char *get_length_modifier(const char *s,
167170
*flags |= PRINT_FLAG_SHORT;
168171
*mask = 0x0000FFFF;
169172
}
170-
}
173+
} else if (*s == 'l') {
171174
/* check for l[l] (long/long long) */
172-
else if (*s == 'l') {
173175
s++;
174176
if (*s == 'l') {
175177
*flags |= PRINT_FLAG_LONG_LONG;
176178
++s;
177-
} else
179+
} else {
178180
*flags |= PRINT_FLAG_LONG;
181+
}
179182
}
180183

181184
return s;
@@ -199,12 +202,14 @@ static int format_number(struct print_param *param)
199202
width = param->vars.valuelen + param->vars.prefixlen;
200203

201204
/* calculate additional characters for precision */
202-
if ((uint32_t)(param->vars.precision) > width)
205+
if ((uint32_t)(param->vars.precision) > width) {
203206
p = param->vars.precision - width;
207+
}
204208

205209
/* calculate additional characters for width */
206-
if ((uint32_t)(param->vars.width) > (width + p))
210+
if ((uint32_t)(param->vars.width) > (width + p)) {
207211
w = param->vars.width - (width + p);
212+
}
208213

209214
/* handle case of right justification */
210215
if ((param->vars.flags & PRINT_FLAG_LEFT_JUSTIFY) == 0) {
@@ -243,21 +248,24 @@ static int format_number(struct print_param *param)
243248
/* emit prefix (if any), return early in case of an error */
244249
res = param->emit(PRINT_CMD_COPY, param->vars.prefix,
245250
param->vars.prefixlen, param->data);
246-
if ((param->vars.prefix != NULL) && (res < 0))
251+
if ((param->vars.prefix != NULL) && (res < 0)) {
247252
return res;
253+
}
248254

249255
/* insert additional 0's for precision, return early if an error
250256
* occurred
251257
*/
252258
res = param->emit(PRINT_CMD_FILL, "0", p, param->data);
253-
if (res < 0)
259+
if (res < 0) {
254260
return res;
261+
}
255262

256263
/* emit the pre-calculated result, return early in case of an error */
257264
res = param->emit(PRINT_CMD_COPY, param->vars.value,
258265
param->vars.valuelen, param->data);
259-
if (res < 0)
266+
if (res < 0) {
260267
return res;
268+
}
261269

262270
/* handle left justification */
263271
if ((param->vars.flags & PRINT_FLAG_LEFT_JUSTIFY) != 0) {
@@ -415,40 +423,46 @@ static int print_string(struct print_param *param, const char *s)
415423
/* we need the length of the string if either width or precision is
416424
* given
417425
*/
418-
if ((param->vars.precision != 0)|| (param->vars.width != 0))
426+
if ((param->vars.precision != 0)|| (param->vars.width != 0)) {
419427
len = strnlen_s(s, PRINT_STRING_MAX_LEN);
428+
}
420429

421430
/* precision gives the max. number of characters to emit. */
422-
if ((param->vars.precision != 0) && (len > param->vars.precision))
431+
if ((param->vars.precision != 0) && (len > param->vars.precision)) {
423432
len = param->vars.precision;
433+
}
424434

425435
/* calculate the number of additional characters to get the required
426436
* width
427437
*/
428-
if (param->vars.width > 0 && param->vars.width > len)
438+
if (param->vars.width > 0 && param->vars.width > len) {
429439
w = param->vars.width - len;
440+
}
430441

431442
/* emit additional characters for width, return early if an error
432443
* occurred
433444
*/
434445
if ((param->vars.flags & PRINT_FLAG_LEFT_JUSTIFY) == 0) {
435446
res = param->emit(PRINT_CMD_FILL, " ", w, param->data);
436-
if (res < 0)
447+
if (res < 0) {
437448
return res;
449+
}
438450
}
439451

440452
/* emit the string, return early if an error occurred */
441453
res = param->emit(PRINT_CMD_COPY, s, len, param->data);
442-
if (res < 0)
454+
if (res < 0) {
443455
return res;
456+
}
444457

445458
/* emit additional characters on the right, return early if an error
446459
* occurred
447460
*/
448461
if ((param->vars.flags & PRINT_FLAG_LEFT_JUSTIFY) != 0) {
449462
res = param->emit(PRINT_CMD_FILL, " ", w, param->data);
450-
if (res < 0)
463+
if (res < 0) {
451464
return res;
465+
}
452466
}
453467

454468
return res;
@@ -479,8 +493,9 @@ int do_print(const char *fmt, struct print_param *param,
479493
*/
480494
res = param->emit(PRINT_CMD_COPY, start, fmt - start,
481495
param->data);
482-
if (res < 0)
496+
if (res < 0) {
483497
return res;
498+
}
484499

485500
/* continue only if the '%' character was found */
486501
if (*fmt == '%') {
@@ -518,9 +533,8 @@ int do_print(const char *fmt, struct print_param *param,
518533
if (ch == '%') {
519534
res = param->emit(PRINT_CMD_COPY, &ch, 1,
520535
param->data);
521-
}
536+
} else if ((ch == 'd') || (ch == 'i')) {
522537
/* decimal number */
523-
else if ((ch == 'd') || (ch == 'i')) {
524538
res = print_decimal(param,
525539
((param->vars.flags &
526540
PRINT_FLAG_LONG_LONG) != 0) ?
@@ -556,8 +570,9 @@ int do_print(const char *fmt, struct print_param *param,
556570
}
557571
/* hexadecimal number */
558572
else if ((ch == 'X') || (ch == 'x')) {
559-
if (ch == 'X')
573+
if (ch == 'X') {
560574
param->vars.flags |= PRINT_FLAG_UPPER;
575+
}
561576
res = print_pow2(param,
562577
((param->vars.flags &
563578
PRINT_FLAG_LONG_LONG) != 0) ?
@@ -572,8 +587,9 @@ int do_print(const char *fmt, struct print_param *param,
572587
else if (ch == 's') {
573588
const char *s = __builtin_va_arg(args, char *);
574589

575-
if (s == NULL)
590+
if (s == NULL) {
576591
s = "(null)";
592+
}
577593
res = print_string(param, s);
578594
}
579595
/* pointer argument */
@@ -601,8 +617,9 @@ int do_print(const char *fmt, struct print_param *param,
601617
}
602618
}
603619
/* return if an error occurred */
604-
if (res < 0)
620+
if (res < 0) {
605621
return res;
622+
}
606623
}
607624

608625
/* done. Return the result of the last emit function call */
@@ -622,17 +639,19 @@ static int charmem(int cmd, const char *s, int sz, void *hnd)
622639
if (cmd == PRINT_CMD_COPY) {
623640
if (sz < 0) {
624641
while ((*s) != 0) {
625-
if (n < param->sz - param->wrtn)
642+
if (n < param->sz - param->wrtn) {
626643
*p = *s;
644+
}
627645
p++;
628646
s++;
629647
n++;
630648
}
631649

632650
} else if (sz > 0) {
633651
while (((*s) != 0) && n < sz) {
634-
if (n < param->sz - param->wrtn)
652+
if (n < param->sz - param->wrtn) {
635653
*p = *s;
654+
}
636655
p++;
637656
s++;
638657
n++;
@@ -678,14 +697,17 @@ int vsnprintf(char *dst, int sz, const char *fmt, va_list args)
678697
param.data = &snparam;
679698

680699
/* execute the printf() */
681-
if (do_print(fmt, &param, args) < 0)
700+
if (do_print(fmt, &param, args) < 0) {
682701
return -1;
702+
}
683703

684704
/* ensure the written string is NULL terminated */
685-
if (snparam.wrtn < sz)
705+
if (snparam.wrtn < sz) {
686706
snparam.dst[snparam.wrtn] = '\0';
687-
else
707+
}
708+
else {
688709
snparam.dst[sz - 1] = '\0';
710+
}
689711

690712
/* return the number of chars which would be written */
691713
res = snparam.wrtn;

0 commit comments

Comments
 (0)