Skip to contentSkip to conversation

getchar(): Correct usage

PATCH In review Tom Rini 1 participant 2 messages Jan 9, 2024 – Jan 19, 2024

lore
Share & tools
lore

Tom Rini

author
#0 lore raw

PATCH 1/1 getchar(): Correct usage

The function getchar() returns an 'int' and not a 'char'. Coverity notes that "Assigning the return value of getchar to char ... truncates its value." and so for the most part we can resolve this easily by using 'int' as intended, and often used throughout the codebase. A few places are not so simple and would require further re-architecting of the code in order to change this, so we leave them be.

---
 cmd/load.c            | 4 ++--
 common/cli_readline.c | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
cmd/load.c +2 -2
index 2715cf5957ee..540361b43f02 100644
@@ -230,7 +230,7 @@ static ulong load_serial(long offset)
 static int read_record(char *buf, ulong len)
 {
 	char *p;
-	char c;
+	int c;
 
 	--len;	/* always leave room for terminating '\0' byte */
 
@@ -827,7 +827,7 @@ static void handle_send_packet(int n)
 /* k_recv receives a OS Open image file over kermit line */
 static int k_recv(void)
 {
-	char new_char;
+	int new_char;
 	char k_state, k_state_saved;
 	int sum;
 	int done;
common/cli_readline.c +1 -1
index 85453beed762..2507be229526 100644
@@ -540,7 +540,7 @@ static int cread_line_simple(const char *const prompt, char *p)
 	int n = 0;		/* buffer index */
 	int plen = 0;		/* prompt length */
 	int col;		/* output column cnt */
-	char c;
+	int c;
 
 	/* print prompt */
 	if (prompt) {
Signature · 1 line
2.34.1
author
#1 lore raw
Quoted 8 lines · Tom Rini

The function getchar() returns an 'int' and not a 'char'. Coverity notes that "Assigning the return value of getchar to char ... truncates its value." and so for the most part we can resolve this easily by using 'int' as intended, and often used throughout the codebase. A few places are not so simple and would require further re-architecting of the code in order to change this, so we leave them be.

Applied to u-boot/master, thanks!

Signature · 1 line
Tom