pespin has submitted this change. ( https://gerrit.osmocom.org/c/libosmo-netif/+/30837 )
Change subject: osmo-amr-inspect: Improve robustness reading from stdin ......................................................................
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 --- M utils/osmo-amr-inspect.c 1 file changed, 11 insertions(+), 5 deletions(-)
Approvals: Jenkins Builder: Verified laforge: Looks good to me, but someone else must approve fixeria: Looks good to me, approved
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 @@ 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; }