Skip to contentSkip to conversation

Don't discard valid UTF-8 following non-valid one

PATCH New Vladimir 'φ-coder/phcoder' Serbinenko 1 participant 1 message Jan 31, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Don't discard valid UTF-8 following non-valid one

This discarding is Unicode-uncompliant and can create problems like accidental discarding of \0 in some cases

Signature · 2 lines
Regards
Vladimir 'φ-coder/phcoder' Serbinenko

=== modified file 'ChangeLog'

ChangeLog +6 -0
@@ -1,5 +1,11 @@
 2010-01-31  Vladimir Serbinenko  <[email protected]>
 
+	* kern/misc.c (grub_utf8_to_ucs4): Don't eat valid characters preceeded
+	by non-valid ones.
+	* kern/term.c (grub_putchar): Likewise.
+
+2010-01-31  Vladimir Serbinenko  <[email protected]>
+
 	* font/font.c (find_glyph): Check that bmp_idx is available before
 	using it.
 	(grub_font_get_string_width): Never call grub_font_get_glyph_internal

=== modified file 'kern/misc.c'

misc.c +4 -0
@@ -975,6 +975,10 @@ grub_utf8_to_ucs4 (grub_uint32_t *dest, 
 	    {
 	      /* invalid */
 	      code = '?';
+	      /* Character c may be valid, don't eat it.  */
+	      src--;
+	      if (srcsize != (grub_size_t)-1)
+		srcsize++;
 	      count = 0;
 	    }
 	  else

=== modified file 'kern/term.c'

term.c +5 -4
@@ -57,16 +57,17 @@ grub_putchar (int c)
 {
   static grub_size_t size = 0;
   static grub_uint8_t buf[6];
+  grub_uint8_t *rest;
   grub_uint32_t code;
-  grub_size_t ret;
 
   buf[size++] = c;
-  ret = grub_utf8_to_ucs4 (&code, 1, buf, size, 0);
 
-  if (ret != 0)
+  while (grub_utf8_to_ucs4 (&code, 1, buf, size, (const grub_uint8_t **) &rest) 
+	 != 0)
     {
       struct grub_term_output *term;
-      size = 0;
+      size -= rest - buf;
+      grub_memmove (buf, rest, size);
       FOR_ACTIVE_TERM_OUTPUTS(term)
 	grub_putcode (code, term);
       if (code == '\n' && grub_newline_hook)