From 0da7c045b3fa8e7c9d29be59ac1d4ac836f4eebf Mon Sep 17 00:00:00 2001 From: Mindy Preston Date: Sat, 5 Jul 2014 22:32:31 -0500 Subject: [PATCH] Add try block for get_uint16 failure in MSS checks; separate check_mss to own function. --- tcp/options.ml | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/tcp/options.ml b/tcp/options.ml index 06b386274..1ec3c19e4 100644 --- a/tcp/options.ml +++ b/tcp/options.ml @@ -33,6 +33,17 @@ let report_error n = let error = Printf.sprintf "Invalid option %d presented" n in raise (Bad_option error) +let check_mss buf = + let min_mss_size = 88 in + try + let mss_size = Cstruct.BE.get_uint16 buf 2 in + if mss_size < min_mss_size then + let err = (Printf.sprintf "Invalid MSS %d received" mss_size) in + raise (Bad_option err) + else + MSS mss_size + with Failure _ -> report_error 2 + let unmarshal buf = let open Cstruct in let i = iter @@ -51,14 +62,7 @@ let unmarshal buf = let option_length = (get_uint8 buf 1) in match option_number, option_length with | _, 0 | _, 1 -> report_error option_number - | 2, 4 -> - let mss_size = BE.get_uint16 buf 2 in - let min_mss_size = 88 in - if mss_size < min_mss_size then - let err = Printf.sprintf "Invalid MSS %d received" mss_size in - raise (Bad_option err) - else - MSS mss_size + | 2, 4 -> check_mss buf | 2, _ -> report_error option_number | 3, 3 -> Window_size_shift (get_uint8 buf 2) | 3, _ -> report_error option_number