fix(libc): typecast operands to match data type
This corrects the MISRA violation C2012-10.3:
The value of an expression shall not be assigned to an object with a
narrower essential type or of a diļ¬erent essential type category.
The condition is explicitly checked against 0U, appending 'U' and
typecasting for unsigned comparison.
In spite of generic guidance for 3rd party libraries
(https://trustedfirmware-a.readthedocs.io/en/latest/process/coding-style.html#misra-compliance)
libc contains some MISRA-C fixes done by commit d5ccb754af86
("libc: Fix some MISRA defects") in 2021.
Also from history it is not clear where libc is
coming from that's why there is no way to fix
violation in base library.
Change-Id: Ibad03a758001b3a7779b488ee5e19c9ceee51134
Signed-off-by: Nithin G <nithing@amd.com>
Signed-off-by: Maheedhar Bollapalli <maheedharsai.bollapalli@amd.com>
diff --git a/lib/libc/printf.c b/lib/libc/printf.c
index a856345..f8c4a26 100644
--- a/lib/libc/printf.c
+++ b/lib/libc/printf.c
@@ -50,7 +50,7 @@
}
do {
- rem = unum % radix;
+ rem = (uint32_t)(unum % radix);
if (rem < 0xa) {
num_buf[i] = '0' + rem;
} else if (uppercase) {
@@ -64,14 +64,14 @@
if (padn > 0) {
while (i < padn) {
- (void)putchar(padc);
+ (void)putchar((int32_t)padc);
count++;
padn--;
}
}
while (--i >= 0) {
- (void)putchar(num_buf[i]);
+ (void)putchar((int32_t)num_buf[i]);
count++;
}
@@ -122,13 +122,13 @@
loop:
switch (*fmt) {
case '%':
- (void)putchar('%');
+ (void)putchar((int32_t)'%');
break;
case 'i': /* Fall through to next one */
case 'd':
num = get_num_va_args(args, l_count);
if (num < 0) {
- (void)putchar('-');
+ (void)putchar((int32_t)'-');
unum = (unsigned long long int)-num;
padn--;
} else