[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#778822: patch for handling non ASCII and non UTF8 headers in headache



Package: headache
Version: 1.03-24

The utf8-length patch appeared in version 1.03-22 systematically uses
UTF8.length to compute string lengths, which will produce wrong
results if the header uses another encoding, such as iso-latin-1. The
attached patch proposes to fall back to plain String.length if the
given string is
not a well formed utf-8 string.

This issue has already been reported on Ubuntu's bts
(https://bugs.launchpad.net/ubuntu/+source/headache/+bug/1083216) as
I'm using Xubuntu 14.10, but it seems that noone there is interested
in fixing it (issue was opened on 2012-11-26).

-- 
E tutto per oggi, a la prossima volta
Virgile
diff -u headache-1.03/main.ml headache-1.03-new/main.ml
--- headache-1.03/main.ml	2012-11-26 14:56:50.000000000 +0100
+++ headache-1.03-new/main.ml	2012-11-26 14:58:03.783386678 +0100
@@ -103,7 +103,7 @@
   in
   let header_width =
     List.fold_left 
-      (fun w line -> max (UTF8.length line) w)  
+      (fun w line -> max (Model.string_length line) w)  
       0
       header
   in
diff -u headache-1.03/model.ml headache-1.03-new/model.ml
--- headache-1.03/model.ml	2012-11-26 14:56:50.000000000 +0100
+++ headache-1.03-new/model.ml	2013-03-21 15:39:10.109974264 +0100
@@ -19,7 +19,9 @@
 
 exception Error of string
 
-
+let string_length s =
+  try UTF8.validate s; UTF8.length s
+  with UTF8.Malformed_code -> String.length s
 
 (***************************************************************************)
 (** {2 Headers generators} *)
@@ -27,7 +29,7 @@
 type generator =
     { remove: in_channel -> string;
       create: out_channel -> string list -> int -> unit;
-    } 
+    }
 
 (***************************************************************************)
 (** {2 Models} *)
@@ -65,7 +67,7 @@
 
 let arg_char args ?default name =
   let s = arg_string args ?default name in
-  if UTF8.length s = 1 then s.[0]
+  if string_length s = 1 then s.[0]
   else raise (Error (sprintf "parameter %s expects a character" name))
 
     
@@ -103,7 +105,7 @@
       output_string oc open_comment;
       output_string oc margin;
       output_string oc string;
-      output oc white 0 (max 0 (real_width - UTF8.length string));
+      output oc white 0 (max 0 (real_width - string_length string));
       output_string oc margin;
       output_string oc close_comment;
       output_char oc '\n'
@@ -150,7 +152,7 @@
 	while
           let s = input_line ic in
             not (Str.string_match regexp_end s
-                   (max 0 (UTF8.length s - end_length)))
+                   (max 0 (string_length s - end_length)))
         do () done;
 	""
       end

Reply to: