Permalink
Browse files

sidebar-utf8: rewrites make_sidebar_entry() to allow correct padding …

…of utf-8 strings and also prevents segfaults due to overflows (Closes: 584581, 603287)
  • Loading branch information...
1 parent 753c3b3 commit 4a5815dd9679193ec1fe1d6794778d9dbf13eb96 @aradici aradici committed May 2, 2011
Showing with 129 additions and 0 deletions.
  1. +3 −0 debian/changelog
  2. +125 −0 debian/patches/mutt-patched/sidebar-utf8
  3. +1 −0 debian/patches/series
View
@@ -9,6 +9,9 @@ mutt (1.5.21-5) unstable; urgency=low
settings of the system, patch by Arnaud Riess (Closes: 589240)
+ multiple-fccs: added a patch that allows multiple FCC separated by commas,
written by Omen Wild (Closes: 586454)
+ + sidebar-utf8: rewrites make_sidebar_entry() to allow correct padding of
+ utf-8 strings and also prevents segfaults due to overflows
+ (Closes: 584581, 603287)
* debian/extra/samples/sidebar.muttrc: documented the options that
the sidebar-{sorted,dotted} patches are introducing; documentation
submitted by Julien Valroff (Closes: 603186)
@@ -0,0 +1,125 @@
+This patch fixes a problem with utf-8 strings and the sidebar, it rewrites
+entirely make_sidebar_entry so it also fixes some segfaults due to
+misallocations and overflows.
+
+See:
+http://bugs.debian.org/584581
+http://bugs.debian.org/603287
+
+Author: Antonio Radici <antonio@dyne.org>
+
+Index: mutt/sidebar.c
+===================================================================
+--- mutt.orig/sidebar.c 2011-05-02 13:36:10.000000000 +0000
++++ mutt/sidebar.c 2011-05-02 13:36:30.000000000 +0000
+@@ -30,6 +30,7 @@
+ #include <libgen.h>
+ #include "keymap.h"
+ #include <stdbool.h>
++#include <wchar.h>
+
+ /*BUFFY *CurBuffy = 0;*/
+ static BUFFY *TopBuffy = 0;
+@@ -111,36 +112,72 @@
+
+ char *make_sidebar_entry(char *box, int size, int new, int flagged)
+ {
+- static char *entry = 0;
+- char *c;
+- int i = 0;
+- int delim_len = strlen(SidebarDelim);
+-
+- c = realloc(entry, SidebarWidth - delim_len + 2);
+- if ( c ) entry = c;
+- entry[SidebarWidth - delim_len + 1] = 0;
+- for (; i < SidebarWidth - delim_len + 1; entry[i++] = ' ' );
+- i = strlen(box);
+- strncpy( entry, box, i < (SidebarWidth - delim_len + 1) ? i : (SidebarWidth - delim_len + 1) );
+-
+- if (size == -1)
+- sprintf(entry + SidebarWidth - delim_len - 3, "?");
+- else if ( new ) {
+- if (flagged > 0) {
+- sprintf(
+- entry + SidebarWidth - delim_len - 5 - quick_log10(size) - quick_log10(new) - quick_log10(flagged),
+- "% d(%d)[%d]", size, new, flagged);
+- } else {
+- sprintf(
+- entry + SidebarWidth - delim_len - 3 - quick_log10(size) - quick_log10(new),
+- "% d(%d)", size, new);
+- }
+- } else if (flagged > 0) {
+- sprintf( entry + SidebarWidth - delim_len - 3 - quick_log10(size) - quick_log10(flagged), "% d[%d]", size, flagged);
+- } else {
+- sprintf( entry + SidebarWidth - delim_len - 1 - quick_log10(size), "% d", size);
+- }
+- return entry;
++ char int_store[20]; // up to 64 bits integers
++ int right_width, left_width;
++ int box_len, box_bytes;
++ int int_len;
++ int right_offset = 0;
++ int delim_len = strlen(SidebarDelim);
++ static char *entry;
++
++ right_width = left_width = 0;
++ box_len = box_bytes = 0;
++
++ // allocate an entry big enough to contain SidebarWidth wide chars
++ entry = malloc((SidebarWidth*4)+1); // TODO: error check
++
++ // determine the right space (i.e.: how big are the numbers that we want to print)
++ if ( size > 0 ) {
++ int_len = snprintf(int_store, sizeof(int_store), "%d", size);
++ right_width += int_len;
++ } else {
++ right_width = 1; // to represent 0
++ }
++ if ( new > 0 ) {
++ int_len = snprintf(int_store, sizeof(int_store), "%d", new);
++ right_width += int_len + 2; // 2 is for ()
++ }
++ if ( flagged > 0 ) {
++ int_len = snprintf(int_store, sizeof(int_store), "%d", flagged);
++ right_width += int_len + 2; // 2 is for []
++ }
++
++ // determine how much space we have for *box and its padding (if any)
++ left_width = SidebarWidth - right_width - 1 - delim_len; // 1 is for the space
++ //fprintf(stdout, "left_width: %d right_width: %d\n", left_width, right_width);
++ // right side overflow case
++ if ( left_width <= 0 ) {
++ snprintf(entry, SidebarWidth*4, "%-*.*s ...", SidebarWidth-4-delim_len, SidebarWidth-4-delim_len, box);
++ return entry;
++ }
++ right_width -= delim_len;
++
++ // to support utf-8 chars we need to add enough space padding in case there
++ // are less chars than bytes in *box
++ box_len = mbstowcs(NULL, box, 0);
++ box_bytes = strlen(box);
++ // debug
++ //fprintf(stdout, "box_len: %d box_bytes: %d (diff: %d)\n", box_len, box_bytes, (box_bytes-box_len));
++ // if there is less string than the space we allow, then we will add the
++ // spaces
++ if ( box_len != -1 && box_len < left_width ) {
++ left_width += (box_bytes - box_len);
++ }
++ // otherwise sprintf will truncate the string for us (therefore, no else case)
++
++ // print the sidebar entry (without new and flagged messages, at the moment)
++ //fprintf(stdout, "left_width: %d right_width: %d\n", left_width, right_width);
++ right_offset = snprintf(entry, SidebarWidth*4, "%-*.*s %d", left_width, left_width, box, size);
++
++ // then pad new and flagged messages if any
++ if ( new > 0 ) {
++ right_offset += snprintf(entry+right_offset, SidebarWidth*4-right_offset, "(%d)", new);
++ }
++ if ( flagged > 0 ) {
++ right_offset += snprintf(entry+right_offset, SidebarWidth*4-right_offset, "[%d]", flagged);
++ }
++
++ return entry;
+ }
+
+ void set_curbuffy(char buf[LONG_STRING])
View
@@ -54,6 +54,7 @@ mutt.org
mutt-patched/sidebar
mutt-patched/sidebar-dotted
mutt-patched/sidebar-sorted
+mutt-patched/sidebar-utf8
mutt-patched/multiple-fcc
# not applying cleanly at the moment

0 comments on commit 4a5815d

Please sign in to comment.