Some misc -Wconversion fixes
cover Some misc -Wconversion fixes
In the context of the CMake patchset Lev and I discussed lowering the warning level for the MSVC builds since there are so many conversion warnings they drown out everything else.
So I actually tried what happens when you build with -Wconversion on Linux and whether it would be possible to resolve these issues. The short answer is: no, this is not possible with reasonable effort. But while finding this out I at least fixed some of the issues. So here a small patchset that fell out of that.
Can also be reviewed via https://gerrit.openvpn.net/
Frank Lichtenheld (3):
Various fixes for -Wconversion errors
Change type of frame.mss_fix to uint16_t mss/mtu: make all size calculations use size_t
src/openvpn/buffer.c | 4 +- src/openvpn/crypto.c | 10 +++-- src/openvpn/integer.h | 15 ++++++-- src/openvpn/mss.c | 43 +++++++++++----------- src/openvpn/mss.h | 4 +- src/openvpn/mtu.c | 6 +-- src/openvpn/mtu.h | 2 +- src/openvpn/options.c | 1 + src/openvpn/otime.c | 2 +- src/openvpn/otime.h | 2 +- src/openvpn/packet_id.c | 4 +- src/openvpn/reliable.c | 7 ++-- src/openvpn/socket.h | 4 +- src/openvpn/tls_crypt.c | 2 +- src/openvpn/xkey_helper.c | 14 +++---- tests/unit_tests/openvpn/mock_get_random.c | 2 +- tests/unit_tests/openvpn/test_crypto.c | 6 +-- tests/unit_tests/openvpn/test_packet_id.c | 6 +-- tests/unit_tests/openvpn/test_provider.c | 2 +- tests/unit_tests/openvpn/test_tls_crypt.c | 2 +- 20 files changed, 74 insertions(+), 64 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/3 Various fixes for -Wconversion errors
These are all fixes I considered "safe". They either
- Have sufficient checks/shifts for a cast to be safe
- Fix the type of a variable without requiring code changes
- Are in non-critical unittest code
--- src/openvpn/buffer.c | 4 ++-- src/openvpn/crypto.c | 10 ++++++---- src/openvpn/integer.h | 4 ++-- src/openvpn/mss.c | 2 +- src/openvpn/otime.c | 2 +- src/openvpn/otime.h | 2 +- src/openvpn/packet_id.c | 4 ++-- src/openvpn/reliable.c | 7 +++---- src/openvpn/socket.h | 4 ++-- src/openvpn/tls_crypt.c | 2 +- src/openvpn/xkey_helper.c | 14 +++++++------- tests/unit_tests/openvpn/mock_get_random.c | 2 +- tests/unit_tests/openvpn/test_crypto.c | 6 +++--- tests/unit_tests/openvpn/test_packet_id.c | 6 +++--- tests/unit_tests/openvpn/test_provider.c | 2 +- tests/unit_tests/openvpn/test_tls_crypt.c | 2 +- 16 files changed, 37 insertions(+), 36 deletions(-)
src/openvpn/buffer.c +2 -2
@@ -354,7 +354,7 @@ buffer_write_file(const char *filename, const struct buffer *buf)return false; } -const int size =write (fd,BPTR (buf),BLEN (buf)); +const ssize_t size =write (fd,BPTR (buf),BLEN (buf));if (size !=BLEN (buf)) {msg (M_ERRNO ,"Write error on file '%s'" , filename);
@@ -891,7 +891,7 @@ buf_parse(struct buffer *buf, const int delim, char *line, const int size) {break ; } - line[n++] = c; + line[n++] = (char )c; }while (c);
src/openvpn/crypto.c +6 -4
@@ -28,6 +28,8 @@#include "config-msvc.h" #endif +#include <inttypes.h> +#include "syshead.h" #include "crypto.h"
@@ -1293,8 +1295,8 @@ read_key_file(struct key2 *key2, const char *file, const unsigned int flags) hex_byte[hb_index++] = c;if (hb_index ==2 ) { -unsigned int u; -ASSERT (sscanf ((const char *)hex_byte,"%x" , &u) ==1 ); +uint8_t u; +ASSERT (sscanf ((const char *)hex_byte,"%" SCNx8, &u) ==1 ); *out++ = u; hb_index =0 ;if (++count == keylen)
@@ -1556,13 +1558,13 @@ write_key(const struct key *key, const struct key_type *kt,ASSERT (cipher_kt_key_size (kt->cipher) <=MAX_CIPHER_KEY_LENGTH &&md_kt_size (kt->digest) <=MAX_HMAC_KEY_LENGTH ); -const uint8_t cipher_length =cipher_kt_key_size (kt->cipher); +const uint8_t cipher_length = (uint8_t )cipher_kt_key_size (kt->cipher);if (!buf_write (buf, &cipher_length,1 )) {return false; } -uint8_t hmac_length =md_kt_size (kt->digest); +uint8_t hmac_length = (uint8_t )md_kt_size (kt->digest);if (!buf_write (buf, &hmac_length,1 )) {
src/openvpn/integer.h +2 -2
@@ -28,12 +28,12 @@#ifndef htonll#define htonll (x) ((1==htonl(1)) ? (x) : \ - ((uint64_t)htonl((x) & 0xFFFFFFFF) << 32) | htonl((x) >> 32)) + ((uint64_t)htonl((uint32_t)((x) & 0xFFFFFFFF)) << 32) | htonl((uint32_t)((x) >> 32)))#endif #ifndef ntohll#define ntohll (x) ((1==ntohl(1)) ? (x) : \ - ((uint64_t)ntohl((x) & 0xFFFFFFFF) << 32) | ntohl((x) >> 32)) + ((uint64_t)ntohl((uint32_t)((x) & 0xFFFFFFFF)) << 32) | ntohl((uint32_t)((x) >> 32)))#endif /*
src/openvpn/mss.c +1 -1
@@ -167,7 +167,7 @@ mss_fixup_dowork(struct buffer *buf, uint16_t maxmss)return ; } -for (olen = hlen -sizeof (struct openvpn_tcphdr ), +for (olen = hlen - (int )sizeof (struct openvpn_tcphdr ), opt = (uint8_t *)(tc +1 ); olen >1 ; olen -= optlen, opt += optlen)
src/openvpn/otime.c +1 -1
@@ -107,7 +107,7 @@ tv_string_abs(const struct timeval *tv, struct gc_arena *gc)/* format a time_t as ascii, or use current time if 0 */ const char * -time_string (time_t t,int usec,bool show_usec,struct gc_arena *gc) +time_string (time_t t,long usec,bool show_usec,struct gc_arena *gc) {struct buffer out =alloc_buf_gc (64 , gc);struct timeval tv;
src/openvpn/otime.h +1 -1
@@ -43,7 +43,7 @@ void frequency_limit_free(struct frequency_limit *f);bool frequency_limit_event_allowed (struct frequency_limit *f);/* format a time_t as ascii, or use current time if 0 */ -const char *time_string (time_t t,int usec,bool show_usec,struct gc_arena *gc); +const char *time_string (time_t t,long usec,bool show_usec,struct gc_arena *gc);/* struct timeval functions */
src/openvpn/packet_id.c +2 -2
@@ -590,14 +590,14 @@ packet_id_debug_print(int msglevel, }else { - diff = (int ) prev_now - v; + diff = (int )(prev_now - v);if (diff <0 ) { c ='N' ; }else if (diff <10 ) { - c ='0' + diff; + c = (char )('0' + diff); }else {
src/openvpn/reliable.c +3 -4
@@ -259,8 +259,7 @@ reliable_ack_write(struct reliable_ack *ack,struct buffer *buf, const struct session_id *sid, int max, bool prepend) { - int i, j; -uint8_t n; + int i, j, n;struct buffer sub; n = ack->len;
@@ -272,9 +271,9 @@ reliable_ack_write(struct reliable_ack *ack,copy_acks_to_mru (ack, ack_mru, n);/* Number of acks we can resend that still fit into the packet */ -uint8_t total_acks =min_int (max, ack_mru->len); +uint8_t total_acks = (uint8_t )min_int (max, ack_mru->len); - sub =buf_sub (buf,ACK_SIZE (total_acks), prepend); + sub =buf_sub (buf, (int )ACK_SIZE (total_acks), prepend);if (!BDEF (&sub)) { goto error;
src/openvpn/socket.h +2 -2
@@ -1181,7 +1181,7 @@ link_socket_write_udp(struct link_socket *sock, }/* write a TCP or UDP packet to link */ -static inline int +static inline size_t link_socket_write (struct link_socket *sock,struct buffer *buf,struct link_socket_actual *to)
@@ -1198,7 +1198,7 @@ link_socket_write(struct link_socket *sock,else {ASSERT (0 ); -return -1 ;/* NOTREACHED */ +return 0 ;/* NOTREACHED */ } }
src/openvpn/tls_crypt.c +1 -1
@@ -634,7 +634,7 @@ tls_crypt_v2_extract_client_key(struct buffer *buf,memcpy (&net_len,BEND (&wrapped_client_key) -sizeof (net_len),sizeof (net_len)); -size_t wkc_len =ntohs (net_len); +uint16_t wkc_len =ntohs (net_len);if (!buf_advance (&wrapped_client_key,BLEN (&wrapped_client_key) - wkc_len)) {msg (D_TLS_ERRORS ,"Can not locate tls-crypt-v2 client key" );
src/openvpn/xkey_helper.c +7 -7
@@ -294,7 +294,7 @@ xkey_management_sign(void *unused, unsigned char *sig, size_t *siglen,* @return false on error, true on success * * On return enc_len is set to actual size of the result. -* enc is NULL or enc_len is not enough to store the result, it is set +* If enc is NULL or enc_len is not enough to store the result, it is set * to the required size and false is returned. */ bool
@@ -339,8 +339,8 @@ encode_pkcs1(unsigned char *enc, size_t *enc_len, const char *mdname,MAKE_DI (sha512),MAKE_DI (sha224),MAKE_DI (sha512_224), MAKE_DI(sha512_256), {0, NULL, 0}}; -int out_len =0 ; -int ret =0 ; +size_t out_len =0 ; +bool ret = false;int nid =OBJ_sn2nid (mdname);if (nid == NID_undef)
@@ -356,7 +356,7 @@ encode_pkcs1(unsigned char *enc, size_t *enc_len, const char *mdname,if (tbslen !=EVP_MD_size (EVP_get_digestbyname (mdname))) { -msg (M_WARN ,"Error: encode_pkcs11: invalid input length <%d>" , (int )tbslen); +msg (M_WARN ,"Error: encode_pkcs11: invalid input length <%zu>" , tbslen); goto done; }
@@ -385,13 +385,13 @@ encode_pkcs1(unsigned char *enc, size_t *enc_len, const char *mdname, out_len = tbslen + di->sz; -if (enc && (out_len <= (int ) *enc_len)) +if (enc && (out_len <= *enc_len)) {/* combine header and digest */ memcpy (enc, di->header, di->sz);memcpy (enc + di->sz, tbs, tbslen); -dmsg (D_XKEY ,"encode_pkcs1: digest length = %d encoded length = %d" , - (int ) tbslen, (int ) out_len); +dmsg (D_XKEY ,"encode_pkcs1: digest length = %zu encoded length = %zu" , + tbslen, out_len); ret = true; }
tests/unit_tests/openvpn/mock_get_random.c +1 -1
@@ -41,6 +41,6 @@ prng_bytes(uint8_t *output, int len) {for (int i =0 ; i < len; i++) { - output[i] =rand (); + output[i] = (uint8_t )rand (); } }
tests/unit_tests/openvpn/test_crypto.c +3 -3
@@ -99,8 +99,8 @@ test_cipher_names(const char *ciphername, const char *openvpn_name)for (int i =0 ; i <strlen (ciphername); i++) { - upper[i] =toupper (ciphername[i]); - lower[i] =tolower (ciphername[i]); + upper[i] = (char )toupper ((unsigned char )ciphername[i]); + lower[i] = (char )tolower ((unsigned char )ciphername[i]);if (rand () &0x1 ) { random_case[i] = upper[i];
@@ -162,7 +162,7 @@ crypto_test_tls_prf(void **state)uint8_t out[32 ]; -ssl_tls1_PRF (seed, seed_len, secret, secret_len, out,sizeof (out)); +ssl_tls1_PRF (seed, (int )seed_len, secret, (int )secret_len, out,sizeof (out));assert_memory_equal (good_prf, out,sizeof (out)); }
tests/unit_tests/openvpn/test_packet_id.c +3 -3
@@ -96,7 +96,7 @@ test_packet_id_write_long(void **state)assert (data->pis.id ==1 );assert (data->pis.time == now);assert_true (data->test_buf_data.buf_id ==htonl (1 )); -assert_true (data->test_buf_data.buf_time ==htonl (now)); +assert_true (data->test_buf_data.buf_time ==htonl ((uint32_t )now)); } static void
@@ -123,7 +123,7 @@ test_packet_id_write_long_prepend(void **state)assert (data->pis.id ==1 );assert (data->pis.time == now);assert_true (data->test_buf_data.buf_id ==htonl (1 )); -assert_true (data->test_buf_data.buf_time ==htonl (now)); +assert_true (data->test_buf_data.buf_time ==htonl ((uint32_t )now)); } static void
@@ -154,7 +154,7 @@ test_packet_id_write_long_wrap(void **state)assert (data->pis.id ==1 );assert (data->pis.time == now);assert_true (data->test_buf_data.buf_id ==htonl (1 )); -assert_true (data->test_buf_data.buf_time ==htonl (now)); +assert_true (data->test_buf_data.buf_time ==htonl ((uint32_t )now)); } static void
tests/unit_tests/openvpn/test_provider.c +1 -1
@@ -367,7 +367,7 @@ xkey_sign(void *handle, unsigned char *sig, size_t *siglen, }/* return a predefined string as sig */ -memcpy (sig, good_sig,min_int (sizeof (good_sig), *siglen)); +memcpy (sig, good_sig,min_int ((int )sizeof (good_sig), (int )*siglen));return 1 ; }
tests/unit_tests/openvpn/test_tls_crypt.c +1 -1
@@ -140,7 +140,7 @@ __wrap_rand_bytes(uint8_t *output, int len) {for (int i =0 ; i < len; i++) { - output[i] = i; + output[i] = (uint8_t )i; }return true; }
Signature · 1 line
2.34.1
PATCH 2/3 Change type of frame.mss_fix to uint16_t
In reply to Frank LichtenheldSince in the end this always ends up as an uint16_t anyway, just make the conversion much earlier. Cleans up the code and removes some -Wconversion warnings.
--- src/openvpn/mss.c | 21 +++++++++++---------- src/openvpn/mss.h | 4 ++-- src/openvpn/mtu.c | 2 +- src/openvpn/mtu.h | 2 +- src/openvpn/options.c | 1 + 5 files changed, 16 insertions(+), 14 deletions(-)
src/openvpn/mss.c +11 -10
@@ -46,7 +46,7 @@* if yes, hand to mss_fixup_dowork() */ void -mss_fixup_ipv4 (struct buffer *buf,int maxmss) +mss_fixup_ipv4 (struct buffer *buf,uint16_t maxmss) {const struct openvpn_iphdr *pip;int hlen;
@@ -74,7 +74,7 @@ mss_fixup_ipv4(struct buffer *buf, int maxmss)struct openvpn_tcphdr *tc = (struct openvpn_tcphdr *)BPTR (&newbuf);if (tc->flags &OPENVPN_TCPH_SYN_MASK ) { -mss_fixup_dowork (&newbuf, (uint16_t ) maxmss); +mss_fixup_dowork (&newbuf, maxmss); } } }
@@ -86,7 +86,7 @@ mss_fixup_ipv4(struct buffer *buf, int maxmss)* (IPv6 header structure is sufficiently different from IPv4...) */ void -mss_fixup_ipv6 (struct buffer *buf,int maxmss) +mss_fixup_ipv6 (struct buffer *buf,uint16_t maxmss) {const struct openvpn_ipv6hdr *pip6;struct buffer newbuf;
@@ -132,7 +132,7 @@ mss_fixup_ipv6(struct buffer *buf, int maxmss)struct openvpn_tcphdr *tc = (struct openvpn_tcphdr *)BPTR (&newbuf);if (tc->flags &OPENVPN_TCPH_SYN_MASK ) { -mss_fixup_dowork (&newbuf, (uint16_t ) maxmss-20 ); +mss_fixup_dowork (&newbuf, maxmss-20 ); } } }
@@ -193,13 +193,14 @@ mss_fixup_dowork(struct buffer *buf, uint16_t maxmss) {continue ; } - mssval = (opt[2 ]<<8 )+opt[3 ]; + mssval = opt[2 ] <<8 ; + mssval += opt[3 ];if (mssval > maxmss) { -dmsg (D_MSS ,"MSS: %d -> %d" , (int ) mssval, (int ) maxmss); +dmsg (D_MSS ,"MSS: %" PRIu16" -> %" PRIu16, mssval, maxmss); accumulate =htons (mssval); - opt[2 ] = (maxmss>>8 )&0xff ; - opt[3 ] = maxmss&0xff ; + opt[2 ] = (uint8_t )((maxmss>>8 )&0xff ); + opt[3 ] = (uint8_t )(maxmss&0xff ); accumulate -=htons (maxmss);ADJUST_CHECKSUM (accumulate, tc->check); }
@@ -293,7 +294,7 @@ frame_calculate_mssfix(struct frame *frame, struct key_type *kt, {/* we subtract IPv4 and TCP overhead here, mssfix method will add the * extra 20 for IPv6 */ - frame->mss_fix = options->ce.mssfix - (20 +20 ); + frame->mss_fix = (uint16_t )(options->ce.mssfix - (20 +20 ));return ; }
@@ -327,7 +328,7 @@ frame_calculate_mssfix(struct frame *frame, struct key_type *kt,/* This is the target value our payload needs to be smaller */ unsigned int target = options->ce.mssfix - overhead; - frame->mss_fix =adjust_payload_max_cbc (kt, target) - payload_overhead; + frame->mss_fix = (uint16_t )(adjust_payload_max_cbc (kt, target) - payload_overhead); }
src/openvpn/mss.h +2 -2
@@ -29,9 +29,9 @@#include "mtu.h" #include "ssl_common.h" -void mss_fixup_ipv4 (struct buffer *buf,int maxmss); +void mss_fixup_ipv4 (struct buffer *buf,uint16_t maxmss); -void mss_fixup_ipv6 (struct buffer *buf,int maxmss); +void mss_fixup_ipv6 (struct buffer *buf,uint16_t maxmss);void mss_fixup_dowork (struct buffer *buf,uint16_t maxmss);
src/openvpn/mtu.c +1 -1
@@ -212,7 +212,7 @@ frame_print(const struct frame *frame,buf_printf (&out,"%s " , prefix); }buf_printf (&out,"[" ); -buf_printf (&out," mss_fix:%d" , frame->mss_fix); +buf_printf (&out," mss_fix:%" PRIu16, frame->mss_fix);#ifdef ENABLE_FRAGMENT buf_printf (&out," max_frag:%d" , frame->max_fragment_size);#endif
src/openvpn/mtu.h +1 -1
@@ -115,7 +115,7 @@ struct frame {* decryption/encryption or compression. */ } buf; -unsigned int mss_fix;/**< The actual MSS value that should be +uint16_t mss_fix;/**< The actual MSS value that should be * written to the payload packets. This * is the value for IPv4 TCP packets. For * IPv6 packets another 20 bytes must
src/openvpn/options.c +1 -0
@@ -7260,6 +7260,7 @@ add_option(struct options *options,/* value specified, assume encapsulation is not * included unless "mtu" follows later */ options->ce.mssfix =positive_atoi (p[1 ]); +ASSERT (options->ce.mssfix <=UINT16_MAX ); options->ce.mssfix_encap = false; options->ce.mssfix_default = false; }
Signature · 1 line
2.34.1
Re: [Openvpn-devel] [PATCH 2/3] Change type of frame.mss_fix to uint16_t
Hi,
src/openvpn/mss.c mss_fixup_dowork(struct buffer *buf, uint16_t maxmss) · line 197
Quoted 4 lines · Frank Lichtenheld
}src/openvpn/mss.c +2
-1- mssval = (opt[2 ]<<8 )+opt[3 ]; + mssval = opt[2 ] <<8 ; + mssval += opt[3 ];
Is this an intentional change, or just a side effect of "something intermediate"?
src/openvpn/options.c add_option(struct options *options, · line 7265
Quoted 7 lines
src/openvpn/options.c +1
-0@@ -7260,6 +7260,7 @@ add_option(struct options *options,/* value specified, assume encapsulation is not * included unless "mtu" follows later */ options->ce.mssfix =positive_atoi (p[1 ]); +ASSERT (options->ce.mssfix <=UINT16_MAX ); options->ce.mssfix_encap = false; options->ce.mssfix_default = false;
This part of the patch is making me unhappy, thus, NAK. We do have a way to signal option errors, and ASSERT() is not... your code would make a client ASSERT() if a server pushes "mssfix 70000".
gert
Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you
feed honest figures into a computer, honest figures come out. Never doubted
it myself till I met a computer with a sense of humor."
Robert A. Heinlein, The Moon is a Harsh Mistress
Gert Doering - Munich, Germany [email protected]...Re: [Openvpn-devel] [PATCH 2/3] Change type of frame.mss_fix to uint16_t
Quoted 10 lines · Gert Doering
Hi,
src/openvpn/mss.c mss_fixup_dowork(struct buffer *buf, uint16_t maxmss) · line 197
Quoted 4 lines · Frank Lichtenheld
}src/openvpn/mss.c +2
-1- mssval = (opt[2 ]<<8 )+opt[3 ]; + mssval = opt[2 ] <<8 ; + mssval += opt[3 ];Is this an intentional change, or just a side effect of "something intermediate"?
This fixes a conversion warning:../../../src/openvpn/mss.c:196:26: warning:
conversion from ‘int’ to ‘uint16_t’ {aka ‘short unsigned int’} may change value [-Wconversion]
196 | mssval = (opt[2] << 8) + opt[3];
| ^
So basically the compiler goes up to int and then realises it needs to go back down to uint16_t. By splitting the statement this is avoided.
One could also fix that with casts, but that would be much uglier, IMHO.
Quoted 12 lines
src/openvpn/options.c add_option(struct options *options, · line 7265
Quoted 8 lines
src/openvpn/options.c +1
-0@@ -7260,6 +7260,7 @@ add_option(struct options *options,/* value specified, assume encapsulation is not * included unless "mtu" follows later */ options->ce.mssfix =positive_atoi (p[1 ]); +ASSERT (options->ce.mssfix <=UINT16_MAX ); options->ce.mssfix_encap = false; options->ce.mssfix_default = false;This part of the patch is making me unhappy, thus, NAK. We do have a way to signal option errors, and
ASSERT()is not... your code would make a clientASSERT()if a server pushes "mssfix 70000".
Okay, will change.
Regards,
Signature · 1 line
Frank Lichtenheld
PATCH v2 2/3 Change type of frame.mss_fix to uint16_t
In reply to Frank LichtenheldSince in the end this always ends up as an uint16_t anyway, just make the conversion much earlier. Cleans up the code and removes some -Wconversion warnings.
v2:
- proper error handling in options.c
--- src/openvpn/mss.c | 21 +++++++++++---------- src/openvpn/mss.h | 4 ++-- src/openvpn/mtu.c | 2 +- src/openvpn/mtu.h | 2 +- src/openvpn/options.c | 9 ++++++++- 5 files changed, 23 insertions(+), 15 deletions(-)
src/openvpn/mss.c +11 -10
@@ -46,7 +46,7 @@* if yes, hand to mss_fixup_dowork() */ void -mss_fixup_ipv4 (struct buffer *buf,int maxmss) +mss_fixup_ipv4 (struct buffer *buf,uint16_t maxmss) {const struct openvpn_iphdr *pip;int hlen;
@@ -74,7 +74,7 @@ mss_fixup_ipv4(struct buffer *buf, int maxmss)struct openvpn_tcphdr *tc = (struct openvpn_tcphdr *)BPTR (&newbuf);if (tc->flags &OPENVPN_TCPH_SYN_MASK ) { -mss_fixup_dowork (&newbuf, (uint16_t ) maxmss); +mss_fixup_dowork (&newbuf, maxmss); } } }
@@ -86,7 +86,7 @@ mss_fixup_ipv4(struct buffer *buf, int maxmss)* (IPv6 header structure is sufficiently different from IPv4...) */ void -mss_fixup_ipv6 (struct buffer *buf,int maxmss) +mss_fixup_ipv6 (struct buffer *buf,uint16_t maxmss) {const struct openvpn_ipv6hdr *pip6;struct buffer newbuf;
@@ -132,7 +132,7 @@ mss_fixup_ipv6(struct buffer *buf, int maxmss)struct openvpn_tcphdr *tc = (struct openvpn_tcphdr *)BPTR (&newbuf);if (tc->flags &OPENVPN_TCPH_SYN_MASK ) { -mss_fixup_dowork (&newbuf, (uint16_t ) maxmss-20 ); +mss_fixup_dowork (&newbuf, maxmss-20 ); } } }
@@ -193,13 +193,14 @@ mss_fixup_dowork(struct buffer *buf, uint16_t maxmss) {continue ; } - mssval = (opt[2 ]<<8 )+opt[3 ]; + mssval = opt[2 ] <<8 ; + mssval += opt[3 ];if (mssval > maxmss) { -dmsg (D_MSS ,"MSS: %d -> %d" , (int ) mssval, (int ) maxmss); +dmsg (D_MSS ,"MSS: %" PRIu16" -> %" PRIu16, mssval, maxmss); accumulate =htons (mssval); - opt[2 ] = (maxmss>>8 )&0xff ; - opt[3 ] = maxmss&0xff ; + opt[2 ] = (uint8_t )((maxmss>>8 )&0xff ); + opt[3 ] = (uint8_t )(maxmss&0xff ); accumulate -=htons (maxmss);ADJUST_CHECKSUM (accumulate, tc->check); }
@@ -293,7 +294,7 @@ frame_calculate_mssfix(struct frame *frame, struct key_type *kt, {/* we subtract IPv4 and TCP overhead here, mssfix method will add the * extra 20 for IPv6 */ - frame->mss_fix = options->ce.mssfix - (20 +20 ); + frame->mss_fix = (uint16_t )(options->ce.mssfix - (20 +20 ));return ; }
@@ -327,7 +328,7 @@ frame_calculate_mssfix(struct frame *frame, struct key_type *kt,/* This is the target value our payload needs to be smaller */ unsigned int target = options->ce.mssfix - overhead; - frame->mss_fix =adjust_payload_max_cbc (kt, target) - payload_overhead; + frame->mss_fix = (uint16_t )(adjust_payload_max_cbc (kt, target) - payload_overhead); }
src/openvpn/mss.h +2 -2
@@ -29,9 +29,9 @@#include "mtu.h" #include "ssl_common.h" -void mss_fixup_ipv4 (struct buffer *buf,int maxmss); +void mss_fixup_ipv4 (struct buffer *buf,uint16_t maxmss); -void mss_fixup_ipv6 (struct buffer *buf,int maxmss); +void mss_fixup_ipv6 (struct buffer *buf,uint16_t maxmss);void mss_fixup_dowork (struct buffer *buf,uint16_t maxmss);
src/openvpn/mtu.c +1 -1
@@ -212,7 +212,7 @@ frame_print(const struct frame *frame,buf_printf (&out,"%s " , prefix); }buf_printf (&out,"[" ); -buf_printf (&out," mss_fix:%d" , frame->mss_fix); +buf_printf (&out," mss_fix:%" PRIu16, frame->mss_fix);#ifdef ENABLE_FRAGMENT buf_printf (&out," max_frag:%d" , frame->max_fragment_size);#endif
src/openvpn/mtu.h +1 -1
@@ -115,7 +115,7 @@ struct frame {* decryption/encryption or compression. */ } buf; -unsigned int mss_fix;/**< The actual MSS value that should be +uint16_t mss_fix;/**< The actual MSS value that should be * written to the payload packets. This * is the value for IPv4 TCP packets. For * IPv6 packets another 20 bytes must
src/openvpn/options.c +8 -1
@@ -7257,9 +7257,16 @@ add_option(struct options *options,VERIFY_PERMISSION (OPT_P_GENERAL |OPT_P_CONNECTION );if (p[1 ]) { +int mssfix =positive_atoi (p[1 ]); +if (mssfix >UINT16_MAX ) + { +msg (msglevel,"--mssfix value '%s' is invalid" , p[1 ]); + goto err; + } +/* value specified, assume encapsulation is not * included unless "mtu" follows later */ - options->ce.mssfix =positive_atoi (p[1 ]); + options->ce.mssfix = mssfix; options->ce.mssfix_encap = false; options->ce.mssfix_default = false; }
Signature · 1 line
2.34.1
PATCH 3/3 mss/mtu: make all size calculations use size_t
In reply to Frank LichtenheldHalf of them used unsigned int, the other half size_t. Standardize on one. Could've also standardized on the other, both are much too big for the expected numbers anyway.
Add a new utility function clamp_size_to_int for cases we need to change from size_t to int (there are a lot of those all over our codebase).
Resolves some -Wconversion warnings.
--- src/openvpn/integer.h | 11 +++++++++-- src/openvpn/mss.c | 20 ++++++++++---------- src/openvpn/mtu.c | 4 ++-- 3 files changed, 21 insertions(+), 14 deletions(-)
src/openvpn/integer.h +9 -2
@@ -36,6 +36,13 @@ ((uint64_t)ntohl((uint32_t)((x) & 0xFFFFFFFF)) << 32) | ntohl((uint32_t)((x) >> 32)))#endif +static inline int +clamp_size_to_int (size_t size) +{ +ASSERT (size <=INT_MAX ); +return (int )size; +} +/* * min/max functions */
@@ -188,8 +195,8 @@ index_verify(int index, int size, const char *file, int line)/** * Rounds down num to the nearest multiple of multiple */ -static inline unsigned int -round_down_uint (unsigned int num,unsigned int multiple) +static inline size_t +round_down_size (size_t num,size_t multiple) {return (num / multiple) * multiple; }
src/openvpn/mss.c +10 -10
@@ -209,8 +209,8 @@ mss_fixup_dowork(struct buffer *buf, uint16_t maxmss) } } -static inline unsigned int -adjust_payload_max_cbc (const struct key_type *kt,unsigned int target) +static inline size_t +adjust_payload_max_cbc (const struct key_type *kt,size_t target) {if (!cipher_kt_mode_cbc (kt->cipher)) {
@@ -223,13 +223,13 @@ adjust_payload_max_cbc(const struct key_type *kt, unsigned int target)/* With CBC we need at least one extra byte for padding and then need * to ensure that the resulting CBC ciphertext length, which is always * a multiple of the block size, is not larger than the target value */ -unsigned int block_size =cipher_kt_block_size (kt->cipher); - target =round_down_uint (target, block_size); +size_t block_size =cipher_kt_block_size (kt->cipher); + target =round_down_size (target, block_size);return target -1 ; } } -static unsigned int +static size_t get_ip_encap_overhead (const struct options *options,const struct link_socket_info *lsi) {
@@ -260,7 +260,7 @@ frame_calculate_fragment(struct frame *frame, struct key_type *kt, struct link_socket_info *lsi) {#if defined(ENABLE_FRAGMENT ) -unsigned int overhead; +size_t overhead; overhead =frame_calculate_protocol_header_size (kt, options, false);
@@ -269,12 +269,12 @@ frame_calculate_fragment(struct frame *frame, struct key_type *kt, overhead +=get_ip_encap_overhead (options, lsi); } -unsigned int target = options->ce.fragment - overhead; +size_t target = options->ce.fragment - overhead;/* The 4 bytes of header that fragment adds itself. The other extra payload * bytes (Ethernet header/compression) are handled by the fragment code * just as part of the payload and therefore automatically taken into * account if the packet needs to fragmented */ - frame->max_fragment_size =adjust_payload_max_cbc (kt, target) -4 ; + frame->max_fragment_size =clamp_size_to_int (adjust_payload_max_cbc (kt, target)) -4 ;if (cipher_kt_mode_cbc (kt->cipher)) {
@@ -298,7 +298,7 @@ frame_calculate_mssfix(struct frame *frame, struct key_type *kt,return ; } -unsigned int overhead, payload_overhead; +size_t overhead, payload_overhead; overhead =frame_calculate_protocol_header_size (kt, options, false);
@@ -327,7 +327,7 @@ frame_calculate_mssfix(struct frame *frame, struct key_type *kt,* by ce.mssfix */ /* This is the target value our payload needs to be smaller */ -unsigned int target = options->ce.mssfix - overhead; +size_t target = options->ce.mssfix - overhead; frame->mss_fix = (uint16_t )(adjust_payload_max_cbc (kt, target) - payload_overhead);
src/openvpn/mtu.c +2 -2
@@ -176,7 +176,7 @@ calc_options_string_link_mtu(const struct options *o, const struct frame *frame)*/ const char *ciphername = o->ciphername; -unsigned int overhead =0 ; +size_t overhead =0 ;if (strcmp (o->ciphername,"BF-CBC" ) ==0 ) {
@@ -194,7 +194,7 @@ calc_options_string_link_mtu(const struct options *o, const struct frame *frame)* the ciphers are actually valid for non tls in occ calucation */ init_key_type (&occ_kt, ciphername, o->authname, true, false); -unsigned int payload =frame_calculate_payload_size (frame, o, &occ_kt); +size_t payload =frame_calculate_payload_size (frame, o, &occ_kt); overhead +=frame_calculate_protocol_header_size (&occ_kt, o, true);return payload + overhead;
Signature · 1 line
2.34.1