From: Antonio Radici Date: Mon, 2 May 2011 14:01:08 +0000 (+0100) Subject: sidebar-utf8: rewrites make_sidebar_entry() to allow correct padding of utf-8 strings... X-Git-Tag: nntp/1.5.21-5nntp1~22 X-Git-Url: https://git.llucax.com/software/mutt-debian.git/commitdiff_plain/4a5815dd9679193ec1fe1d6794778d9dbf13eb96?ds=sidebyside sidebar-utf8: rewrites make_sidebar_entry() to allow correct padding of utf-8 strings and also prevents segfaults due to overflows (Closes: 584581, 603287) --- diff --git a/debian/changelog b/debian/changelog index c24d2e2..e8ed95f 100644 --- a/debian/changelog +++ b/debian/changelog @@ -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) diff --git a/debian/patches/mutt-patched/sidebar-utf8 b/debian/patches/mutt-patched/sidebar-utf8 new file mode 100644 index 0000000..32241c5 --- /dev/null +++ b/debian/patches/mutt-patched/sidebar-utf8 @@ -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 + +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 + #include "keymap.h" + #include ++#include + + /*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]) diff --git a/debian/patches/series b/debian/patches/series index a767d62..1346bab 100644 --- a/debian/patches/series +++ b/debian/patches/series @@ -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