aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPau Espin Pedrol <pespin@sysmocom.de>2023-01-03 11:40:04 +0100
committerPau Espin Pedrol <pespin@sysmocom.de>2023-01-03 11:41:17 +0100
commit38047fd20aa7322631b322bfe802b70463e7334d (patch)
tree9eeff4fa3287f1f0430e6cdd7f390cd75295964c
parenta1c9fbfabce809a3dba869e37109021d11c1de2f (diff)
osmo-amr-inspect: Improve robustness reading from stdin
Fixes printing hexbuf which might not have been null-terminated. Related: SYS#6161 Fixes: Coverity CID#302068 Change-Id: I460f1deb7455b3b6a85a090bdcad8e21a883db68
-rw-r--r--utils/osmo-amr-inspect.c16
1 files changed, 11 insertions, 5 deletions
diff --git a/utils/osmo-amr-inspect.c b/utils/osmo-amr-inspect.c
index a677b56..dbf612d 100644
--- a/utils/osmo-amr-inspect.c
+++ b/utils/osmo-amr-inspect.c
@@ -241,26 +241,32 @@ free_ret:
static int read_stdin(void)
{
ssize_t rc;
+ size_t hex_buflen;
char hex_buf[4096];
uint8_t buf[2048];
- rc = read(0, hex_buf, sizeof(hex_buf));
+ rc = read(0, hex_buf, sizeof(hex_buf) - 1);
if (rc < 0) {
fprintf(stderr, "Failed reading stdin: %s\n", strerror(errno));
return -EIO;
}
- if (rc == sizeof(hex_buf)) {
- fprintf(stderr, "Failed parsing (input too long)\n");
+ hex_buflen = rc;
+ hex_buf[hex_buflen] = '\0';
+
+ if (hex_buflen == sizeof(hex_buf) - 1) {
+ fprintf(stderr, "Failed parsing (input too long > %lu): %s\n", hex_buflen, hex_buf);
return -ENOMEM;
}
- rc = osmo_hexparse(hex_buf, buf, rc);
+
+ rc = osmo_hexparse(hex_buf, buf, sizeof(buf));
if (rc < 0) {
fprintf(stderr, "Failed parsing (hexparse error): %s\n", hex_buf);
return -1;
}
if (rc < 2) {
- fprintf(stderr, "Too short to be an AMR buffer (%ld bytes): %s\n", rc, buf);
+ fprintf(stderr, "Too short to be an AMR buffer (%ld bytes): %s\n", rc, hex_buf);
return -1;
}
+
inspect_amr(0, buf, rc);
return 0;
}