Browse code

options.c: fix broken unary minus usage

In Visual Studio when unary minus is applied to unsigned,
result is still unsigned. This means that when we use result
as function formal parameter, we pass incorrect value.

Fix by introducing frame_remove_from_extra_frame(),
which makes code semantically more clear and eliminates
the need in negative value and cast.

Since GCC didn't complain (and users too :), it probably performed
cast to signed automatically.

Signed-off-by: Lev Stipakov <lev@openvpn.net>
Acked-by: Gert Doering <gert@greenie.muc.de>
Message-Id: <1539258702-15427-1-git-send-email-lstipakov@gmail.com>
URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg17739.html
Signed-off-by: Gert Doering <gert@greenie.muc.de>

Lev Stipakov authored on 2018/10/11 20:51:42
Showing 5 changed files
... ...
@@ -700,7 +700,7 @@ crypto_adjust_frame_parameters(struct frame *frame,
700 700
                                bool packet_id,
701 701
                                bool packet_id_long_form)
702 702
 {
703
-    size_t crypto_overhead = 0;
703
+    unsigned int crypto_overhead = 0;
704 704
 
705 705
     if (packet_id)
706 706
     {
... ...
@@ -725,10 +725,10 @@ crypto_adjust_frame_parameters(struct frame *frame,
725 725
     frame_add_to_extra_frame(frame, crypto_overhead);
726 726
 
727 727
     msg(D_MTU_DEBUG, "%s: Adjusting frame parameters for crypto by %u bytes",
728
-        __func__, (unsigned int) crypto_overhead);
728
+        __func__, crypto_overhead);
729 729
 }
730 730
 
731
-size_t
731
+unsigned int
732 732
 crypto_max_overhead(void)
733 733
 {
734 734
     return packet_id_size(true) + OPENVPN_MAX_IV_LENGTH
... ...
@@ -418,7 +418,7 @@ void crypto_adjust_frame_parameters(struct frame *frame,
418 418
                                     bool packet_id_long_form);
419 419
 
420 420
 /** Return the worst-case OpenVPN crypto overhead (in bytes) */
421
-size_t crypto_max_overhead(void);
421
+unsigned int crypto_max_overhead(void);
422 422
 
423 423
 /* Minimum length of the nonce used by the PRNG */
424 424
 #define NONCE_SECRET_LEN_MIN 16
... ...
@@ -271,12 +271,18 @@ frame_add_to_link_mtu(struct frame *frame, const int increment)
271 271
 }
272 272
 
273 273
 static inline void
274
-frame_add_to_extra_frame(struct frame *frame, const int increment)
274
+frame_add_to_extra_frame(struct frame *frame, const unsigned int increment)
275 275
 {
276 276
     frame->extra_frame += increment;
277 277
 }
278 278
 
279 279
 static inline void
280
+frame_remove_from_extra_frame(struct frame *frame, const unsigned int decrement)
281
+{
282
+    frame->extra_frame -= decrement;
283
+}
284
+
285
+static inline void
280 286
 frame_add_to_extra_tun(struct frame *frame, const int increment)
281 287
 {
282 288
     frame->extra_tun += increment;
... ...
@@ -3511,7 +3511,7 @@ calc_options_string_link_mtu(const struct options *o, const struct frame *frame)
3511 3511
         struct key_type fake_kt;
3512 3512
         init_key_type(&fake_kt, o->ciphername, o->authname, o->keysize, true,
3513 3513
                       false);
3514
-        frame_add_to_extra_frame(&fake_frame, -(crypto_max_overhead()));
3514
+        frame_remove_from_extra_frame(&fake_frame, crypto_max_overhead());
3515 3515
         crypto_adjust_frame_parameters(&fake_frame, &fake_kt, o->replay,
3516 3516
                                        cipher_kt_mode_ofb_cfb(fake_kt.cipher));
3517 3517
         frame_finalize(&fake_frame, o->ce.link_mtu_defined, o->ce.link_mtu,
... ...
@@ -1984,7 +1984,7 @@ tls_session_update_crypto_params(struct tls_session *session,
1984 1984
     }
1985 1985
 
1986 1986
     /* Update frame parameters: undo worst-case overhead, add actual overhead */
1987
-    frame_add_to_extra_frame(frame, -(crypto_max_overhead()));
1987
+    frame_remove_from_extra_frame(frame, crypto_max_overhead());
1988 1988
     crypto_adjust_frame_parameters(frame, &session->opt->key_type,
1989 1989
                                    options->replay, packet_id_long_form);
1990 1990
     frame_finalize(frame, options->ce.link_mtu_defined, options->ce.link_mtu,