console-server: Validate values and fix narrowing conversions
Reported by clang-tidy:
```
/usr/bin/clang-tidy -checks=-*, bugprone-narrowing-conversions -export-fixes /tmp/tmpnat1i6r_/tmpxkxj0mkr.yaml -p=build /mnt/host/andrew/home/andrew/src/openbmc/obmc-console/console-server.c
../console-server.c:290:27: error: narrowing conversion from 'unsigned long' to signed type 'int' is implementation-defined [bugprone-narrowing-conversions,-warnings-as-errors]
console->tty_lpc_addr = strtoul(val, &endp, 0);
^
../console-server.c:299:23: error: narrowing conversion from 'unsigned long' to signed type 'int' is implementation-defined [bugprone-narrowing-conversions,-warnings-as-errors]
console->tty_sirq = strtoul(val, &endp, 0);
^
```
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
Change-Id: Icddb4039b95cf865bc144ec4fa85770a50b97fb3
diff --git a/console-server.c b/console-server.c
index bcbd4db..32e164c 100644
--- a/console-server.c
+++ b/console-server.c
@@ -49,7 +49,7 @@
char *tty_sysfs_devnode;
char *tty_dev;
int tty_sirq;
- int tty_lpc_addr;
+ uint16_t tty_lpc_addr;
speed_t tty_baud;
int tty_fd;
@@ -281,13 +281,27 @@
static int tty_init(struct console *console, struct config *config)
{
+ unsigned long parsed;
const char *val;
char *endp;
int rc;
val = config_get_value(config, "lpc-address");
if (val) {
- console->tty_lpc_addr = strtoul(val, &endp, 0);
+ errno = 0;
+ parsed = strtoul(val, &endp, 0);
+ if (parsed == ULONG_MAX && errno == ERANGE) {
+ warn("Cannot interpret 'lpc-address' value as an unsigned long: '%s'",
+ val);
+ return -1;
+ }
+
+ if (parsed > UINT16_MAX) {
+ warn("Invalid LPC address '%s'", val);
+ return -1;
+ }
+
+ console->tty_lpc_addr = (uint16_t)parsed;
if (endp == optarg) {
warn("Invalid LPC address: '%s'", val);
return -1;
@@ -296,7 +310,17 @@
val = config_get_value(config, "sirq");
if (val) {
- console->tty_sirq = strtoul(val, &endp, 0);
+ errno = 0;
+ parsed = strtoul(val, &endp, 0);
+ if (parsed == ULONG_MAX && errno == ERANGE) {
+ warn("Cannot interpret 'sirq' value as an unsigned long: '%s'",
+ val);
+ }
+
+ if (parsed > 16)
+ warn("Invalid LPC SERIRQ: '%s'", val);
+
+ console->tty_sirq = (int)parsed;
if (endp == optarg)
warn("Invalid sirq: '%s'", val);
}