From ff1b155731ff8f790f12d980911d9fd84d0e1598 Mon Sep 17 00:00:00 2001 From: Will Cosgrove Date: Fri, 5 Apr 2019 09:46:03 -0700 Subject: [PATCH] Simplified _libssh2_check_length (#350) * Simplified _libssh2_check_length misc.c : _libssh2_check_length() Removed cast and improved bounds checking and format. Credit : Yuriy M. Kaminskiy --- src/misc.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/misc.c b/src/misc.c index 8d80456c..76644a3c 100644 --- a/src/misc.c +++ b/src/misc.c @@ -811,10 +811,9 @@ int _libssh2_get_bignum_bytes(struct string_buf *buf, unsigned char **outbuf) int _libssh2_check_length(struct string_buf *buf, size_t len) { - if(len > buf->len) - return 0; - - return ((int)(buf->dataptr - buf->data) <= (int)(buf->len - len)) ? 1 : 0; + unsigned char *endp = &buf->data[buf->len]; + size_t left = endp - buf->dataptr; + return ((len <= left) && (left <= buf->len)); } /* Wrappers */