Skip to content

Commit 10ed599

Browse files
mgcaolijinxia
authored andcommitted
HV: cleanup sprintf&string.c MISRA-C issues
main focus on integral issues, and change some functions interface to unify the params data type; also modify to simplify the code logic. Signed-off-by: Minggui Cao <minggui.cao@intel.com> Reviewed-by: Yonghua Huang <yonghua.huang@intel.com> Acked-by: Eddie Dong <eddie.dong@intel.com>
1 parent 88f74b5 commit 10ed599

File tree

4 files changed

+103
-151
lines changed

4 files changed

+103
-151
lines changed

hypervisor/debug/printf.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,24 @@
66

77
#include <hypervisor.h>
88

9-
static int charout(int cmd, const char *s, int32_t sz_arg, void *hnd)
9+
static int charout(int cmd, const char *s, uint32_t sz, void *hnd)
1010
{
1111
/* pointer to an integer to store the number of characters */
1212
int *nchars = (int *)hnd;
1313
/* working pointer */
1414
const char *p = s;
15-
int32_t sz = sz_arg;
1615

1716
/* copy mode ? */
1817
if (cmd == PRINT_CMD_COPY) {
19-
/* copy all characters until NUL is found */
20-
if (sz < 0) {
21-
s += console_puts(s);
22-
} else { /* copy 'sz' characters */
18+
if (sz > 0U) { /* copy 'sz' characters */
2319
s += console_write(s, sz);
2420
}
2521

2622
*nchars += (s - p);
27-
return *nchars;
2823
} else {
2924
/* fill mode */
3025
*nchars += sz;
31-
while (sz != 0) {
26+
while (sz != 0U) {
3227
(void)console_putc(*s);
3328
sz--;
3429
}

hypervisor/include/lib/sprintf.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
/* Structure used to parse parameters and variables to subroutines. */
1717
struct print_param {
1818
/* A pointer to the function that is used to emit characters. */
19-
int (*emit)(int, const char *, int, void *);
19+
int (*emit)(int, const char *, uint32_t, void *);
2020
/* An opaque pointer that is passed as third argument to the emit
2121
* function.
2222
*/
@@ -26,9 +26,9 @@ struct print_param {
2626
/* A bitfield with the parsed format flags. */
2727
uint32_t flags;
2828
/* The parsed format width. */
29-
int width;
29+
uint32_t width;
3030
/* The parsed format precision. */
31-
int precision;
31+
uint32_t precision;
3232
/* The bitmask for unsigned values. */
3333
uint64_t mask;
3434
/* A pointer to the preformated value. */
@@ -56,8 +56,7 @@ int do_print(const char *fmt, struct print_param *param,
5656
* @return The number of bytes which would be written, even if the destination
5757
* is smaller. On error a negative number is returned.
5858
*/
59-
60-
int vsnprintf(char *dst, int32_t sz_arg, const char *fmt, va_list args);
59+
int vsnprintf(char *dst, size_t sz, const char *fmt, va_list args);
6160

6261
/** The well known snprintf() function.
6362
*

hypervisor/lib/sprintf.c

Lines changed: 66 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ struct snprint_param {
5252
/** The destination buffer. */
5353
char *dst;
5454
/** The size of the destination buffer. */
55-
int sz;
55+
uint32_t sz;
5656
/** Counter for written chars. */
57-
int wrtn;
57+
uint32_t wrtn;
5858
};
5959

6060
/** The characters to use for upper case hexadecimal conversion.
@@ -83,28 +83,22 @@ static const char lower_hex_digits[] = {
8383
'a', 'b', 'c', 'd', 'e', 'f', 'x'
8484
};
8585

86-
static const char *get_int(const char *s, int *x)
86+
static const char *get_param(const char *s, uint32_t *x)
8787
{
88-
int negative = 0;
89-
*x = 0;
88+
*x = 0U;
9089

91-
/* evaluate leading '-' for negative numbers */
90+
/* ignore '-' for negative numbers, it will be handled in flags*/
9291
if (*s == '-') {
93-
negative = 1;
9492
++s;
9593
}
9694

9795
/* parse uint32_teger */
9896
while ((*s >= '0') && (*s <= '9')) {
99-
*x = (*x * 10) + (*s - '0');
97+
char delta = *s - '0';
98+
*x = *x * 10U + (uint32_t)delta;
10099
s++;
101100
}
102101

103-
/* apply sign to result */
104-
if (negative != 0) {
105-
*x = -*x;
106-
}
107-
108102
return s;
109103
}
110104

@@ -195,26 +189,21 @@ static int format_number(struct print_param *param)
195189
/* effective width of the result */
196190
uint32_t width;
197191
/* number of characters to insert for width (w) and precision (p) */
198-
uint32_t p, w;
192+
uint32_t p = 0U, w = 0U;
199193
/* the result */
200-
int res;
194+
int res = 0;
201195

202196
/* initialize variables */
203-
p = 0U;
204-
w = 0U;
205-
res = 0;
206197
width = param->vars.valuelen + param->vars.prefixlen;
207198

208199
/* calculate additional characters for precision */
209-
p = (uint32_t)(param->vars.precision);
210-
if (p > width) {
211-
p = p - width;
200+
if (param->vars.precision > width) {
201+
p = param->vars.precision - width;
212202
}
213203

214204
/* calculate additional characters for width */
215-
w = (uint32_t)(param->vars.width);
216-
if (w > (width + p)) {
217-
w = w - (width + p);
205+
if (param->vars.width > (width + p)) {
206+
w = param->vars.width - (width + p);
218207
}
219208

220209
/* handle case of right justification */
@@ -421,33 +410,25 @@ static int print_decimal(struct print_param *param, int64_t value)
421410
static int print_string(struct print_param *param, const char *s)
422411
{
423412
/* the length of the string (-1) if unknown */
424-
int len;
413+
uint32_t len;
425414
/* the number of additional characters to insert to reach the required
426415
* width
427416
*/
428-
uint32_t w;
417+
uint32_t w = 0U;
429418
/* the last result of the emit function */
430419
int res;
431420

432-
w = 0U;
433-
len = -1;
434-
435-
/* we need the length of the string if either width or precision is
436-
* given
437-
*/
438-
if ((param->vars.precision != 0)|| (param->vars.width != 0)) {
439-
len = strnlen_s(s, PRINT_STRING_MAX_LEN);
440-
}
421+
len = strnlen_s(s, PRINT_STRING_MAX_LEN);
441422

442423
/* precision gives the max. number of characters to emit. */
443-
if ((param->vars.precision != 0) && (len > param->vars.precision)) {
424+
if ((param->vars.precision != 0U) && (len > param->vars.precision)) {
444425
len = param->vars.precision;
445426
}
446427

447428
/* calculate the number of additional characters to get the required
448429
* width
449430
*/
450-
if (param->vars.width > 0 && param->vars.width > len) {
431+
if (param->vars.width > 0U && param->vars.width > len) {
451432
w = param->vars.width - len;
452433
}
453434

@@ -461,7 +442,6 @@ static int print_string(struct print_param *param, const char *s)
461442
}
462443
}
463444

464-
/* emit the string, return early if an error occurred */
465445
res = param->emit(PRINT_CMD_COPY, s, len, param->data);
466446
if (res < 0) {
467447
return res;
@@ -527,14 +507,11 @@ int do_print(const char *fmt, struct print_param *param,
527507
* - get the length modifier
528508
*/
529509
fmt = get_flags(fmt, &(param->vars.flags));
530-
fmt = get_int(fmt, &(param->vars.width));
510+
fmt = get_param(fmt, &(param->vars.width));
531511

532512
if (*fmt == '.') {
533513
fmt++;
534-
fmt = get_int(fmt, &(param->vars.precision));
535-
if (param->vars.precision < 0) {
536-
param->vars.precision = 0;
537-
}
514+
fmt = get_param(fmt, &(param->vars.precision));
538515
}
539516

540517
fmt = get_length_modifier(fmt, &(param->vars.flags),
@@ -544,57 +521,61 @@ int do_print(const char *fmt, struct print_param *param,
544521

545522
/* a single '%'? => print out a single '%' */
546523
if (ch == '%') {
547-
res = param->emit(PRINT_CMD_COPY, &ch, 1,
524+
res = param->emit(PRINT_CMD_COPY, &ch, 1U,
548525
param->data);
549526
} else if ((ch == 'd') || (ch == 'i')) {
550527
/* decimal number */
551-
res = print_decimal(param,
552-
((param->vars.flags &
553-
PRINT_FLAG_LONG_LONG) != 0U) ?
554-
__builtin_va_arg(args,
555-
long long)
556-
: (long long)
557-
__builtin_va_arg(args,
558-
int));
528+
if ((param->vars.flags &
529+
PRINT_FLAG_LONG_LONG) != 0U) {
530+
res = print_decimal(param,
531+
__builtin_va_arg(args, long));
532+
} else {
533+
res = print_decimal(param,
534+
__builtin_va_arg(args, int));
535+
}
559536
}
560537
/* unsigned decimal number */
561538
else if (ch == 'u') {
562539
param->vars.flags |= PRINT_FLAG_UINT32;
563-
res = print_decimal(param,
564-
((param->vars.flags &
565-
PRINT_FLAG_LONG_LONG) != 0U) ?
566-
__builtin_va_arg(args,
567-
unsigned long long)
568-
: (unsigned long long)
569-
__builtin_va_arg(args,
570-
unsigned int));
540+
if ((param->vars.flags &
541+
PRINT_FLAG_LONG_LONG) != 0U) {
542+
res = print_decimal(param,
543+
(int64_t)__builtin_va_arg(args,
544+
uint64_t));
545+
} else {
546+
res = print_decimal(param,
547+
(int64_t)__builtin_va_arg(args,
548+
uint32_t));
549+
}
571550
}
572551
/* octal number */
573552
else if (ch == 'o') {
574-
res = print_pow2(param,
575-
((param->vars.flags &
576-
PRINT_FLAG_LONG_LONG) != 0U) ?
553+
if ((param->vars.flags &
554+
PRINT_FLAG_LONG_LONG) != 0U) {
555+
res = print_pow2(param,
577556
__builtin_va_arg(args,
578-
unsigned long long)
579-
: (unsigned long long)
557+
uint64_t), 3U);
558+
} else {
559+
res = print_pow2(param,
580560
__builtin_va_arg(args,
581-
uint32_t),
582-
3U);
561+
uint32_t), 3U);
562+
}
583563
}
584564
/* hexadecimal number */
585565
else if ((ch == 'X') || (ch == 'x')) {
586566
if (ch == 'X') {
587567
param->vars.flags |= PRINT_FLAG_UPPER;
588568
}
589-
res = print_pow2(param,
590-
((param->vars.flags &
591-
PRINT_FLAG_LONG_LONG) != 0U) ?
569+
if ((param->vars.flags &
570+
PRINT_FLAG_LONG_LONG) != 0U) {
571+
res = print_pow2(param,
592572
__builtin_va_arg(args,
593-
unsigned long long)
594-
: (unsigned long long)
573+
uint64_t), 4U);
574+
} else {
575+
res = print_pow2(param,
595576
__builtin_va_arg(args,
596-
uint32_t),
597-
4U);
577+
uint32_t), 4U);
578+
}
598579
}
599580
/* string argument */
600581
else if (ch == 's') {
@@ -635,28 +616,18 @@ int do_print(const char *fmt, struct print_param *param,
635616
return res;
636617
}
637618

638-
static int charmem(int cmd, const char *s, int sz, void *hnd)
619+
static int charmem(int cmd, const char *s, uint32_t sz, void *hnd)
639620
{
640621
/* pointer to the snprint parameter list */
641622
struct snprint_param *param = (struct snprint_param *) hnd;
642623
/* pointer to the destination */
643624
char *p = param->dst + param->wrtn;
644625
/* characters actually written */
645-
int n = 0;
626+
uint32_t n = 0U;
646627

647628
/* copy mode ? */
648629
if (cmd == PRINT_CMD_COPY) {
649-
if (sz < 0) {
650-
while ((*s) != '\0') {
651-
if (n < (param->sz - param->wrtn)) {
652-
*p = *s;
653-
}
654-
p++;
655-
s++;
656-
n++;
657-
}
658-
659-
} else if (sz > 0) {
630+
if (sz > 0U) {
660631
while (((*s) != '\0') && n < sz) {
661632
if (n < (param->sz - param->wrtn)) {
662633
*p = *s;
@@ -665,33 +636,26 @@ static int charmem(int cmd, const char *s, int sz, void *hnd)
665636
s++;
666637
n++;
667638
}
668-
} else {
669-
/* sz == 0, no copy needed. */
670639
}
671640

672641
param->wrtn += n;
673-
return n;
674642
}
675643
/* fill mode */
676644
else {
677-
n = (sz < (param->sz - param->wrtn)) ? sz : 0;
645+
n = (sz < param->sz - param->wrtn) ? sz : 0U;
678646
param->wrtn += sz;
679-
(void)memset(p, *s, n);
647+
(void)memset(p, (uint8_t)*s, n);
680648
}
681649

682-
return n;
650+
return (int)n;
683651
}
684652

685-
int vsnprintf(char *dst, int32_t sz_arg, const char *fmt, va_list args)
653+
int vsnprintf(char *dst, size_t sz, const char *fmt, va_list args)
686654
{
687-
char c[1];
688-
/* the result of this function */
689-
int32_t sz = sz_arg;
690655
int res = 0;
691656

692-
if (sz <= 0 || (dst == NULL)) {
693-
dst = c;
694-
sz = 1;
657+
if (sz == 0U || (dst == NULL)) {
658+
return -1;
695659
}
696660

697661
/* struct to store all necessary parameters */
@@ -722,7 +686,7 @@ int vsnprintf(char *dst, int32_t sz_arg, const char *fmt, va_list args)
722686
}
723687

724688
/* return the number of chars which would be written */
725-
res = snparam.wrtn;
689+
res = (int)snparam.wrtn;
726690

727691
/* done */
728692
return res;
@@ -738,7 +702,7 @@ int snprintf(char *dest, int sz, const char *fmt, ...)
738702
va_start(args, fmt);
739703

740704
/* execute the printf() */
741-
res = vsnprintf(dest, sz, fmt, args);
705+
res = vsnprintf(dest, (size_t)sz, fmt, args);
742706

743707
/* destroy parameter list */
744708
va_end(args);

0 commit comments

Comments
 (0)