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/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));