Fix pad function to work for negative widths (RHBZ#634435).
authorRichard W.M. Jones <rjones@redhat.com>
Thu, 6 Jan 2011 12:38:24 +0000 (12:38 +0000)
committerRichard W.M. Jones <rjones@redhat.com>
Thu, 6 Jan 2011 12:38:24 +0000 (12:38 +0000)
The pad function is supposed to pad a string to a width.  Under some
circumstances, the width parameter could be negative resulting in a
call to String.sub such as:

  String.sub str 0 (-1)

which caused an exception to be thrown and not caught, causing
virt-top to exit.  Fix the pad function to return an empty string if
width <= 0 instead of throwing an exception.

See also:
https://bugzilla.redhat.com/show_bug.cgi?id=634435

virt-top/virt_top_utils.ml

index f858e0e..c5dc97d 100644 (file)
@@ -109,10 +109,13 @@ let read_config_file filename =
 
 (* Pad a string to the full width with spaces.  If too long, truncate. *)
 let pad width str =
 
 (* Pad a string to the full width with spaces.  If too long, truncate. *)
 let pad width str =
-  let n = String.length str in
-  if n = width then str
-  else if n > width then String.sub str 0 width
-  else (* if n < width then *) str ^ String.make (width-n) ' '
+  if width <= 0 then ""
+  else (
+    let n = String.length str in
+    if n = width then str
+    else if n > width then String.sub str 0 width
+    else (* if n < width then *) str ^ String.make (width-n) ' '
+  )
 
 module Show = struct
   (* Show a percentage in 4 chars. *)
 
 module Show = struct
   (* Show a percentage in 4 chars. *)