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>
This commit is contained in:
Minggui Cao
2018-07-24 09:25:22 +08:00
committed by lijinxia
parent 88f74b5dbb
commit 10ed599b50
4 changed files with 103 additions and 151 deletions

View File

@@ -6,8 +6,8 @@
#include <hypervisor.h>
#define ULONG_MAX ((uint64_t)(~0UL)) /* 0xFFFFFFFF */
#define LONG_MAX ((long)(ULONG_MAX >> 1)) /* 0x7FFFFFFF */
#define LONG_MIN ((long)(~LONG_MAX)) /* 0x80000000 */
#define LONG_MAX (ULONG_MAX >> 1U) /* 0x7FFFFFFF */
#define LONG_MIN (~LONG_MAX) /* 0x80000000 */
#define ISSPACE(c) ((c == ' ') || (c == '\t'))
@@ -17,11 +17,10 @@
long strtol_deci(const char *nptr)
{
const char *s = nptr;
uint64_t acc;
char c;
uint64_t cutoff;
int neg = 0, any, cutlim;
int base = 10;
uint64_t acc, cutoff, cutlim;
int neg = 0, any;
uint64_t base = 10UL;
/*
* Skip white space and pick up leading +/- sign if any.
@@ -30,6 +29,7 @@ long strtol_deci(const char *nptr)
c = *s;
s++;
} while (ISSPACE(c));
if (c == '-') {
neg = 1;
c = *s;
@@ -58,41 +58,36 @@ long strtol_deci(const char *nptr)
* Set any if any `digits' consumed; make it negative to indicate
* overflow.
*/
cutoff = (neg != 0) ? -(uint64_t)LONG_MIN : LONG_MAX;
cutlim = cutoff % (uint64_t)base;
cutoff /= (uint64_t)base;
acc = 0U;
cutoff = (neg != 0) ? LONG_MIN : LONG_MAX;
cutlim = cutoff % base;
cutoff /= base;
acc = 0UL;
any = 0;
do {
if (c >= '0' && c <= '9') {
c -= '0';
} else {
break;
}
if (c >= base) {
break;
}
if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) {
while (c >= '0' && c <= '9') {
c -= '0';
if ((acc > cutoff) ||
(acc == cutoff && (uint64_t)c > cutlim)) {
any = -1;
break;
} else {
any = 1;
acc *= base;
acc += c;
}
c = *s;
s++;
} while (true);
}
if (any < 0) {
acc = (neg != 0) ? LONG_MIN : LONG_MAX;
} else if (neg != 0) {
acc = -acc;
acc = ~acc + 1UL;
} else {
/* There is no overflow and no leading '-' exists. In such case
* acc already holds the right number. No action required. */
}
return acc;
return (long)acc;
}
/*
@@ -101,10 +96,10 @@ long strtol_deci(const char *nptr)
uint64_t strtoul_hex(const char *nptr)
{
const char *s = nptr;
uint64_t acc;
char c;
uint64_t cutoff;
int base = 16, any, cutlim;
uint64_t acc, cutoff, cutlim;
uint64_t base = 16UL;
int any;
/*
* See strtol for comments as to the logic used.
@@ -119,9 +114,9 @@ uint64_t strtoul_hex(const char *nptr)
s += 2;
}
cutoff = (uint64_t)ULONG_MAX / (uint64_t)base;
cutlim = (uint64_t)ULONG_MAX % (uint64_t)base;
acc = 0U;
cutoff = ULONG_MAX / base;
cutlim = ULONG_MAX % base;
acc = 0UL;
any = 0;
do {
if (c >= '0' && c <= '9') {
@@ -133,13 +128,12 @@ uint64_t strtoul_hex(const char *nptr)
} else {
break;
}
if (c >= base) {
break;
}
if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) {
if ((acc > cutoff) ||
(acc == cutoff && (uint64_t)c > cutlim)) {
any = -1;
break;
} else {
any = 1;
acc *= base;
acc += c;
}
@@ -148,7 +142,7 @@ uint64_t strtoul_hex(const char *nptr)
s++;
} while (true);
if (any <= 0) {
if (any < 0) {
acc = ULONG_MAX;
}
return acc;