From patchwork Wed Mar 24 11:23:27 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arne Schwabe X-Patchwork-Id: 1656 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director7.mail.ord1d.rsapps.net ([172.27.255.8]) by backend30.mail.ord1d.rsapps.net with LMTP id eBUvOR68W2DdTgAAIUCqbw (envelope-from ) for ; Wed, 24 Mar 2021 18:24:30 -0400 Received: from proxy2.mail.iad3a.rsapps.net ([172.27.255.8]) by director7.mail.ord1d.rsapps.net with LMTP id qP0HOR68W2CDegAAovjBpQ (envelope-from ) for ; Wed, 24 Mar 2021 18:24:30 -0400 Received: from smtp24.gate.iad3a ([172.27.255.8]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy2.mail.iad3a.rsapps.net with LMTPS id 4CQjMh68W2AGEwAABcWvHw (envelope-from ) for ; Wed, 24 Mar 2021 18:24:30 -0400 X-Spam-Threshold: 95 X-Spam-Score: 0 X-Spam-Flag: NO X-Virus-Scanned: OK X-Orig-To: openvpnslackdevel@openvpn.net X-Originating-Ip: [216.105.38.7] Authentication-Results: smtp24.gate.iad3a.rsapps.net; iprev=pass policy.iprev="216.105.38.7"; spf=pass smtp.mailfrom="openvpn-devel-bounces@lists.sourceforge.net" smtp.helo="lists.sourceforge.net"; dkim=fail (signature verification failed) header.d=sourceforge.net; dkim=fail (signature verification failed) header.d=sf.net; dmarc=none (p=nil; dis=none) header.from=rfc2549.org X-Suspicious-Flag: YES X-Classification-ID: b330198a-8cef-11eb-869e-5254009f6f51-1-1 Received: from [216.105.38.7] ([216.105.38.7:43972] helo=lists.sourceforge.net) by smtp24.gate.iad3a.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id E9/69-00652-E1CBB506; Wed, 24 Mar 2021 18:24:30 -0400 Received: from [127.0.0.1] (helo=sfs-ml-4.v29.lw.sourceforge.com) by sfs-ml-4.v29.lw.sourceforge.com with esmtp (Exim 4.90_1) (envelope-from ) id 1lPBuj-0000rM-GV; Wed, 24 Mar 2021 22:23:45 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-4.v29.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lPBug-0000rE-LU for openvpn-devel@lists.sourceforge.net; Wed, 24 Mar 2021 22:23:42 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Message-Id:Date:Subject:To:From:Sender:Reply-To:Cc: MIME-Version:Content-Type:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=TS5SovzGZgcoSsEE6g83J5PHyuG8hnSgoHQbWY9pQdI=; b=ExenmUQk5m7zJ+NkPCiW13vZaQ MYSoneTAWExm8XyJNhJYB/AXvhRNYwC9SpKjBXDuRB0y2WaFofoWSAkza67/khSMJ7gqPf6iYkOCJ TWqLZJAd9mZecE+Lw33OdPFjw9n299hEPdd448k/CaVH1PGgRRkvl2d/KhPpyPs++pNw=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Message-Id:Date:Subject:To:From:Sender:Reply-To:Cc:MIME-Version: Content-Type:Content-Transfer-Encoding:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=TS5SovzGZgcoSsEE6g83J5PHyuG8hnSgoHQbWY9pQdI=; b=WMLlaVCu8B2RuzJL3f8o/xTASg KGLzgAs8E0T7+4auI0Ub8EXfds1Ry0Wthw3WdbJMzzuAccgE+KuNds5gIdRbYrNcmoso+xbDzcFp7 g7G8tMyruQBCivmfVW0u/goZ9qocB/qHXpTuz4wdzjGCowgV4ci9DPI09TB+QMrOdbbk=; Received: from mail.blinkt.de ([192.26.174.232]) by sfi-mx-1.v28.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.92.2) id 1lPBuc-007CbX-8B for openvpn-devel@lists.sourceforge.net; Wed, 24 Mar 2021 22:23:42 +0000 Received: from kamera.blinkt.de ([2001:638:502:390:20c:29ff:fec8:535c]) by mail.blinkt.de with smtp (Exim 4.94 (FreeBSD)) (envelope-from ) id 1lPBuV-000ExF-1X for openvpn-devel@lists.sourceforge.net; Wed, 24 Mar 2021 23:23:31 +0100 Received: (nullmailer pid 506 invoked by uid 10006); Wed, 24 Mar 2021 22:23:30 -0000 From: Arne Schwabe To: openvpn-devel@lists.sourceforge.net Date: Wed, 24 Mar 2021 23:23:27 +0100 Message-Id: <20210324222330.455-1-arne@rfc2549.org> X-Mailer: git-send-email 2.17.1 X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. 0.0 URIBL_BLOCKED ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [URIs: rfc2549.org] 0.2 HEADER_FROM_DIFFERENT_DOMAINS From and EnvelopeFrom 2nd level mail domains are different 0.0 SPF_NONE SPF: sender does not publish an SPF Record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record X-Headers-End: 1lPBuc-007CbX-8B Subject: [Openvpn-devel] [PATCH 1/4] Make buffer related function conversion explicit when narrowing X-BeenThere: openvpn-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox Clang and gcc do report many of the narrowing conversion that MSVC reports, like these: warning C4267: 'function': conversion from 'size_t' to 'int', possible loss of data This commit changes int to size_t where it is safe (e.g. checked by buf_size_valid) and add casts where necessary. In the function buffer_read_from_file the return value of fread is size_t (at least on Linux/Windows/macOS and cppreference), so fix the check to actually make sense. Signed-off-by: Arne Schwabe Acked-by: Gert Doering --- src/openvpn/buffer.c | 20 ++++++++++---------- src/openvpn/buffer.h | 16 ++++++++-------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/openvpn/buffer.c b/src/openvpn/buffer.c index 35d9ecdc..48bf25d5 100644 --- a/src/openvpn/buffer.c +++ b/src/openvpn/buffer.c @@ -661,7 +661,7 @@ rm_trailing_chars(char *str, const char *what_to_delete) bool modified; do { - const int len = strlen(str); + const size_t len = strlen(str); modified = false; if (len > 0) { @@ -687,7 +687,7 @@ string_alloc(const char *str, struct gc_arena *gc) { if (str) { - const int n = strlen(str) + 1; + const size_t n = strlen(str) + 1; char *ret; if (gc) @@ -814,7 +814,7 @@ string_alloc_buf(const char *str, struct gc_arena *gc) bool buf_string_match_head_str(const struct buffer *src, const char *match) { - const int size = strlen(match); + const size_t size = strlen(match); if (size < 0 || size > src->len) { return false; @@ -827,7 +827,7 @@ buf_string_compare_advance(struct buffer *src, const char *match) { if (buf_string_match_head_str(src, match)) { - buf_advance(src, strlen(match)); + buf_advance(src, (int)strlen(match)); return true; } else @@ -1244,7 +1244,7 @@ buffer_list_push(struct buffer_list *ol, const char *str) struct buffer_entry *e = buffer_list_push_data(ol, str, len+1); if (e) { - e->buf.len = len; /* Don't count trailing '\0' as part of length */ + e->buf.len = (int)len; /* Don't count trailing '\0' as part of length */ } } } @@ -1293,7 +1293,7 @@ void buffer_list_aggregate_separator(struct buffer_list *bl, const size_t max_len, const char *sep) { - const int sep_len = strlen(sep); + const size_t sep_len = strlen(sep); struct buffer_entry *more = bl->head; size_t size = 0; int count = 0; @@ -1400,7 +1400,7 @@ buffer_read_from_file(const char *filename, struct gc_arena *gc) { struct buffer ret = { 0 }; - platform_stat_t file_stat = {0}; + platform_stat_t file_stat = { 0 }; if (platform_stat(filename, &file_stat) < 0) { return ret; @@ -1414,13 +1414,13 @@ buffer_read_from_file(const char *filename, struct gc_arena *gc) const size_t size = file_stat.st_size; ret = alloc_buf_gc(size + 1, gc); /* space for trailing \0 */ - ssize_t read_size = fread(BPTR(&ret), 1, size, fp); - if (read_size < 0) + size_t read_size = fread(BPTR(&ret), 1, size, fp); + if (read_size == 0) { free_buf_gc(&ret, gc); goto cleanup; } - ASSERT(buf_inc_len(&ret, read_size)); + ASSERT(buf_inc_len(&ret, (int)read_size)); buf_null_terminate(&ret); cleanup: diff --git a/src/openvpn/buffer.h b/src/openvpn/buffer.h index 1722ffd5..9ea70284 100644 --- a/src/openvpn/buffer.h +++ b/src/openvpn/buffer.h @@ -332,13 +332,13 @@ buf_set_write(struct buffer *buf, uint8_t *data, int size) } static inline void -buf_set_read(struct buffer *buf, const uint8_t *data, int size) +buf_set_read(struct buffer *buf, const uint8_t *data, size_t size) { if (!buf_size_valid(size)) { buf_size_error(size); } - buf->len = buf->capacity = size; + buf->len = buf->capacity = (int)size; buf->offset = 0; buf->data = (uint8_t *)data; } @@ -538,10 +538,10 @@ struct buffer buf_sub(struct buffer *buf, int size, bool prepend); */ static inline bool -buf_safe(const struct buffer *buf, int len) +buf_safe(const struct buffer *buf, size_t len) { return buf_valid(buf) && buf_size_valid(len) - && buf->offset + buf->len + len <= buf->capacity; + && buf->offset + buf->len + (int)len <= buf->capacity; } static inline bool @@ -549,7 +549,7 @@ buf_safe_bidir(const struct buffer *buf, int len) { if (buf_valid(buf) && buf_size_valid_signed(len)) { - const int newlen = buf->len + len; + int newlen = buf->len + len; return newlen >= 0 && buf->offset + newlen <= buf->capacity; } else @@ -653,7 +653,7 @@ buf_advance(struct buffer *buf, int size) */ static inline uint8_t * -buf_write_alloc(struct buffer *buf, int size) +buf_write_alloc(struct buffer *buf, size_t size) { uint8_t *ret; if (!buf_safe(buf, size)) @@ -661,7 +661,7 @@ buf_write_alloc(struct buffer *buf, int size) return NULL; } ret = BPTR(buf) + buf->len; - buf->len += size; + buf->len += (int)size; return ret; } @@ -686,7 +686,7 @@ buf_read_alloc(struct buffer *buf, int size) } static inline bool -buf_write(struct buffer *dest, const void *src, int size) +buf_write(struct buffer *dest, const void *src, size_t size) { uint8_t *cp = buf_write_alloc(dest, size); if (!cp)