obmc-console: Fix bugprone-narrowing-conversions
For example:
```
/mnt/host/andrew/home/andrew/src/openbmc/obmc-console/build/../console-server.c:769:9: error: narrowing conversion from 'ssize_t' (aka 'long') to signed type 'int' is implementation-defined [bugprone-narrowing-conversions,-warnings-as-errors]
rc = read(console->tty_fd, buf, sizeof(buf));
^
```
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
Change-Id: I67c158b411f1533ca3b5a62803116e95907e8c5b
diff --git a/config.c b/config.c
index 1174eaa..6fc840c 100644
--- a/config.c
+++ b/config.c
@@ -99,8 +99,8 @@
{
struct config *config;
size_t size, len;
+ ssize_t rc;
char *buf;
- int rc;
size = 4096;
len = 0;
diff --git a/console-client.c b/console-client.c
index 13bd20d..c63677a 100644
--- a/console-client.c
+++ b/console-client.c
@@ -156,7 +156,8 @@
static int process_console(struct console_client *client)
{
uint8_t buf[4096];
- int len, rc;
+ ssize_t len;
+ int rc;
len = read(client->console_sd, buf, sizeof(buf));
if (len < 0) {
diff --git a/console-server.c b/console-server.c
index 32e164c..02219cf 100644
--- a/console-server.c
+++ b/console-server.c
@@ -56,10 +56,10 @@
struct ringbuffer *rb;
struct handler **handlers;
- int n_handlers;
+ long n_handlers;
struct poller **pollers;
- int n_pollers;
+ long n_pollers;
struct pollfd *pollfds;
struct sd_bus *bus;
@@ -461,7 +461,7 @@
console->n_handlers = &__stop_handlers - &__start_handlers;
console->handlers = &__start_handlers;
- printf("%d handler%s\n", console->n_handlers,
+ printf("%ld handler%s\n", console->n_handlers,
console->n_handlers == 1 ? "" : "s");
for (i = 0; i < console->n_handlers; i++) {
@@ -524,7 +524,7 @@
int events, void *data)
{
struct poller *poller;
- int n;
+ long n;
poller = malloc(sizeof(*poller));
poller->remove = false;
@@ -552,7 +552,7 @@
sizeof(*console->pollfds) * MAX_INTERNAL_POLLFD);
console->pollfds[n].fd = fd;
- console->pollfds[n].events = events;
+ console->pollfds[n].events = (short)(events & 0x7fff);
return poller;
}
@@ -601,7 +601,7 @@
if (console->pollers[i] == poller)
break;
- console->pollfds[i].events = events;
+ console->pollfds[i].events = (short)(events & 0x7fff);
}
void console_poller_set_timeout(struct console *console __attribute__((unused)),
@@ -617,7 +617,7 @@
timeradd(&now, tv, &poller->timeout);
}
-static int get_poll_timeout(struct console *console, struct timeval *cur_time)
+static long get_poll_timeout(struct console *console, struct timeval *cur_time)
{
struct timeval *earliest, interval;
struct poller *poller;
@@ -729,11 +729,10 @@
int run_console(struct console *console)
{
- sighandler_t sighandler_save;
+ sighandler_t sighandler_save = signal(SIGINT, sighandler);
struct timeval tv;
- int rc, timeout;
-
- sighandler_save = signal(SIGINT, sighandler);
+ long timeout;
+ ssize_t rc;
rc = 0;
@@ -756,7 +755,8 @@
timeout = get_poll_timeout(console, &tv);
rc = poll(console->pollfds,
- console->n_pollers + MAX_INTERNAL_POLLFD, timeout);
+ console->n_pollers + MAX_INTERNAL_POLLFD,
+ (int)timeout);
if (rc < 0) {
if (errno == EINTR) {
diff --git a/console-socket.c b/console-socket.c
index 461f151..7785688 100644
--- a/console-socket.c
+++ b/console-socket.c
@@ -17,11 +17,13 @@
#include "console-server.h"
#include <errno.h>
+#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/types.h>
+#include <unistd.h>
#define CONSOLE_SOCKET_PREFIX "obmc-console"
@@ -57,10 +59,14 @@
size_t addrlen, socket_path_t path)
{
const char *src = (const char *)addr;
- const size_t len = addrlen - sizeof(addr->sun_family) - 1;
+ size_t len;
+ if (addrlen > SSIZE_MAX)
+ return -EINVAL;
+
+ len = addrlen - sizeof(addr->sun_family) - 1;
memcpy(path, src + sizeof(addr->sun_family) + 1, len);
path[len] = '\0';
- return len; /* strlen() style */
+ return (ssize_t)len; /* strlen() style */
}
diff --git a/ringbuffer.c b/ringbuffer.c
index 996dc49..5c485fc 100644
--- a/ringbuffer.c
+++ b/ringbuffer.c
@@ -125,7 +125,7 @@
size_t len)
{
enum ringbuffer_poll_ret prc;
- int force_len;
+ size_t force_len;
if (ringbuffer_space(rbc) >= len)
return 0;
diff --git a/socket-handler.c b/socket-handler.c
index b10615b..79e1641 100644
--- a/socket-handler.c
+++ b/socket-handler.c
@@ -19,6 +19,7 @@
#include <assert.h>
#include <err.h>
#include <errno.h>
+#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
@@ -112,9 +113,13 @@
static ssize_t send_all(struct client *client, void *buf, size_t len,
bool block)
{
- int fd, rc, flags;
+ int fd, flags;
+ ssize_t rc;
size_t pos;
+ if (len > SSIZE_MAX)
+ return -EINVAL;
+
fd = client->fd;
flags = MSG_NOSIGNAL;
@@ -139,7 +144,7 @@
return -1;
}
- return pos;
+ return (ssize_t)pos;
}
/* Drain the queue to the socket and update the queue buffer. If force_len is
@@ -189,7 +194,8 @@
size_t force_len)
{
struct client *client = arg;
- int rc, len;
+ size_t len;
+ int rc;
len = ringbuffer_len(client->rbc);
if (!force_len && (len < SOCKET_HANDLER_PKT_SIZE)) {
@@ -238,7 +244,7 @@
struct socket_handler *sh = to_socket_handler(handler);
struct client *client = data;
uint8_t buf[4096];
- int rc;
+ ssize_t rc;
if (events & POLLIN) {
rc = recv(client->fd, buf, sizeof(buf), MSG_DONTWAIT);
diff --git a/test/ringbuffer-test-utils.c b/test/ringbuffer-test-utils.c
index 9172648..56e37ee 100644
--- a/test/ringbuffer-test-utils.c
+++ b/test/ringbuffer-test-utils.c
@@ -5,7 +5,7 @@
bool force_only;
int count;
uint8_t *data;
- int len;
+ size_t len;
};
void ringbuffer_test_context_init(struct rb_test_ctx *ctx)
diff --git a/test/test-client-escape.c b/test/test-client-escape.c
index d7a9957..b818889 100644
--- a/test/test-client-escape.c
+++ b/test/test-client-escape.c
@@ -15,9 +15,13 @@
*/
#include <assert.h>
+#include <errno.h>
+#include <limits.h>
#include <stdint.h>
#include <stdio.h>
+#include <unistd.h>
+static ssize_t __read(int fd, void *buf, size_t len);
#define read __read
#include "config.c"
#include "console-socket.c"
@@ -165,12 +169,17 @@
return 0;
}
-ssize_t __read(int fd, void *buf, size_t len)
+static ssize_t __read(int fd, void *buf, size_t len)
{
struct test_ctx *ctx = &ctxs[fd];
const char *inbuf;
size_t inlen;
+ if (len > SSIZE_MAX) {
+ errno = EINVAL;
+ return -1;
+ }
+
if (ctx->cur_in >= ctx->test->n_in)
return 0;
@@ -179,10 +188,10 @@
assert(inlen <= len);
memcpy(buf, inbuf, inlen);
ctx->cur_in++;
- return inlen;
+ return (ssize_t)inlen;
}
-void run_one_test(int idx, struct test *test, struct test_ctx *ctx)
+void run_one_test(size_t idx, struct test *test, struct test_ctx *ctx)
{
size_t exp_out_len;
int rc;
@@ -190,8 +199,9 @@
/* we store the index into the context array as a FD, so we
* can refer to it through the read & write callbacks.
*/
- ctx->client.console_sd = idx;
- ctx->client.fd_in = idx;
+ assert(idx < INT_MAX);
+ ctx->client.console_sd = (int)idx;
+ ctx->client.fd_in = (int)idx;
ctx->client.esc_type = test->esc_type;
memcpy(&ctx->client.esc_state, &test->esc_state,
sizeof(test->esc_state));