From f00d9c45e0db962aa93e9d2609c037a6f7166966 Mon Sep 17 00:00:00 2001 From: akallabeth Date: Thu, 3 Oct 2024 14:48:10 +0200 Subject: [PATCH] [warnings] fix integer narrowing fix function freerdp_tls_write_all --- libfreerdp/core/gateway/arm.c | 4 +--- libfreerdp/core/gateway/rdg.c | 10 +++------- libfreerdp/core/gateway/wst.c | 12 +++--------- libfreerdp/crypto/tls.c | 22 +++++++++++----------- libfreerdp/crypto/tls.h | 2 +- 5 files changed, 19 insertions(+), 31 deletions(-) diff --git a/libfreerdp/core/gateway/arm.c b/libfreerdp/core/gateway/arm.c index 49d0c183a..55d4186c1 100644 --- a/libfreerdp/core/gateway/arm.c +++ b/libfreerdp/core/gateway/arm.c @@ -239,9 +239,7 @@ static BOOL arm_send_http_request(rdpArm* arm, rdpTls* tls, const char* method, return FALSE; const size_t sz = Stream_Length(s); - - if (sz <= INT_MAX) - status = freerdp_tls_write_all(tls, Stream_Buffer(s), sz); + status = freerdp_tls_write_all(tls, Stream_Buffer(s), sz); Stream_Free(s, TRUE); if (status >= 0 && content_length > 0 && data) diff --git a/libfreerdp/core/gateway/rdg.c b/libfreerdp/core/gateway/rdg.c index a58a13448..219adaf81 100644 --- a/libfreerdp/core/gateway/rdg.c +++ b/libfreerdp/core/gateway/rdg.c @@ -1238,18 +1238,14 @@ static BOOL rdg_auth_init(rdpRdg* rdg, rdpTls* tls, TCHAR* authPkg) static BOOL rdg_send_http_request(rdpRdg* rdg, rdpTls* tls, const char* method, TRANSFER_ENCODING transferEncoding) { - size_t sz = 0; - wStream* s = NULL; int status = -1; - s = rdg_build_http_request(rdg, method, transferEncoding); + wStream* s = rdg_build_http_request(rdg, method, transferEncoding); if (!s) return FALSE; - sz = Stream_Length(s); - - if (sz <= INT_MAX) - status = freerdp_tls_write_all(tls, Stream_Buffer(s), sz); + const size_t sz = Stream_Length(s); + status = freerdp_tls_write_all(tls, Stream_Buffer(s), sz); Stream_Free(s, TRUE); return (status >= 0); diff --git a/libfreerdp/core/gateway/wst.c b/libfreerdp/core/gateway/wst.c index b1d3463ba..2c4430cda 100644 --- a/libfreerdp/core/gateway/wst.c +++ b/libfreerdp/core/gateway/wst.c @@ -332,21 +332,15 @@ out: static BOOL wst_send_http_request(rdpWst* wst, rdpTls* tls) { - size_t sz = 0; - wStream* s = NULL; - int status = -1; WINPR_ASSERT(wst); WINPR_ASSERT(tls); - s = wst_build_http_request(wst); - + wStream* s = wst_build_http_request(wst); if (!s) return FALSE; - sz = Stream_Length(s); - - if (sz <= INT_MAX) - status = freerdp_tls_write_all(tls, Stream_Buffer(s), sz); + const size_t sz = Stream_Length(s); + int status = freerdp_tls_write_all(tls, Stream_Buffer(s), sz); Stream_Free(s, TRUE); return (status >= 0); diff --git a/libfreerdp/crypto/tls.c b/libfreerdp/crypto/tls.c index 7693aa34f..26fa50832 100644 --- a/libfreerdp/crypto/tls.c +++ b/libfreerdp/crypto/tls.c @@ -1236,22 +1236,22 @@ BOOL freerdp_tls_send_alert(rdpTls* tls) return TRUE; } -int freerdp_tls_write_all(rdpTls* tls, const BYTE* data, int length) +int freerdp_tls_write_all(rdpTls* tls, const BYTE* data, size_t length) { WINPR_ASSERT(tls); - int offset = 0; + size_t offset = 0; BIO* bio = tls->bio; + if (length > INT32_MAX) + return -1; + while (offset < length) { ERR_clear_error(); - const int rc = BIO_write(bio, &data[offset], length - offset); + const int status = BIO_write(bio, &data[offset], (int)(length - offset)); - if (rc < 0) - return rc; - - if (rc > 0) - offset += rc; + if (status > 0) + offset += (size_t)status; else { if (!BIO_should_retry(bio)) @@ -1259,8 +1259,8 @@ int freerdp_tls_write_all(rdpTls* tls, const BYTE* data, int length) if (BIO_write_blocked(bio)) { - const long status = BIO_wait_write(bio, 100); - if (status < 0) + const long rc = BIO_wait_write(bio, 100); + if (rc < 0) return -1; } else if (BIO_read_blocked(bio)) @@ -1270,7 +1270,7 @@ int freerdp_tls_write_all(rdpTls* tls, const BYTE* data, int length) } } - return length; + return (int)length; } int freerdp_tls_set_alert_code(rdpTls* tls, int level, int description) diff --git a/libfreerdp/crypto/tls.h b/libfreerdp/crypto/tls.h index 654249446..99e5224b1 100644 --- a/libfreerdp/crypto/tls.h +++ b/libfreerdp/crypto/tls.h @@ -115,7 +115,7 @@ extern "C" FREERDP_LOCAL BOOL freerdp_tls_send_alert(rdpTls* tls); - FREERDP_LOCAL int freerdp_tls_write_all(rdpTls* tls, const BYTE* data, int length); + FREERDP_LOCAL int freerdp_tls_write_all(rdpTls* tls, const BYTE* data, size_t length); FREERDP_LOCAL int freerdp_tls_set_alert_code(rdpTls* tls, int level, int description);