From d19b6eac792352fc18d04fd3a7421d1b46c3363c Mon Sep 17 00:00:00 2001 From: Rob Hoes Date: Thu, 19 May 2016 16:19:45 +0100 Subject: [PATCH 1/2] CA-211448: When reading a line from sysfs, translate End_of_file to "" Some sysfs files are "empty", for a good reason. Signed-off-by: Rob Hoes --- lib/network_utils.ml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/network_utils.ml b/lib/network_utils.ml index c4b8701b3..f838103ce 100644 --- a/lib/network_utils.ml +++ b/lib/network_utils.ml @@ -82,7 +82,9 @@ module Sysfs = struct let result = input_line inchan in close_in inchan; result - with exn -> close_in inchan; raise (Read_error file) + with + | End_of_file -> close_in inchan; "" + | exn -> error "%s" (Printexc.to_string exn); close_in inchan; raise (Read_error file) let write_one_line file l = let outchan = open_out file in From 7d613a746c437d4fcb2708b2c0a86a74aabc63f0 Mon Sep 17 00:00:00 2001 From: Rob Hoes Date: Thu, 19 May 2016 21:49:15 +0100 Subject: [PATCH 2/2] fixup! CA-211448: When reading a line from sysfs, translate End_of_file to "" --- lib/network_utils.ml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/network_utils.ml b/lib/network_utils.ml index f838103ce..cb21be745 100644 --- a/lib/network_utils.ml +++ b/lib/network_utils.ml @@ -77,14 +77,14 @@ module Sysfs = struct Printf.sprintf "/sys/class/net/%s/%s" dev attr let read_one_line file = - let inchan = open_in file in try - let result = input_line inchan in - close_in inchan; - result + let inchan = open_in file in + finally + (fun () -> input_line inchan) + (fun () -> close_in inchan) with - | End_of_file -> close_in inchan; "" - | exn -> error "%s" (Printexc.to_string exn); close_in inchan; raise (Read_error file) + | End_of_file -> "" + | exn -> error "%s" (Printexc.to_string exn); raise (Read_error file) let write_one_line file l = let outchan = open_out file in