1 This patch fixes a problem with utf-8 strings and the sidebar, it rewrites
2 entirely make_sidebar_entry so it also fixes some segfaults due to
3 misallocations and overflows.
6 http://bugs.debian.org/584581
7 http://bugs.debian.org/603287
9 Author: Antonio Radici <antonio@dyne.org>
12 ===================================================================
13 --- mutt.orig/sidebar.c 2011-05-02 13:36:10.000000000 +0000
14 +++ mutt/sidebar.c 2011-05-02 13:36:30.000000000 +0000
21 /*BUFFY *CurBuffy = 0;*/
22 static BUFFY *TopBuffy = 0;
25 char *make_sidebar_entry(char *box, int size, int new, int flagged)
27 - static char *entry = 0;
30 - int delim_len = strlen(SidebarDelim);
32 - c = realloc(entry, SidebarWidth - delim_len + 2);
34 - entry[SidebarWidth - delim_len + 1] = 0;
35 - for (; i < SidebarWidth - delim_len + 1; entry[i++] = ' ' );
37 - strncpy( entry, box, i < (SidebarWidth - delim_len + 1) ? i : (SidebarWidth - delim_len + 1) );
40 - sprintf(entry + SidebarWidth - delim_len - 3, "?");
44 - entry + SidebarWidth - delim_len - 5 - quick_log10(size) - quick_log10(new) - quick_log10(flagged),
45 - "% d(%d)[%d]", size, new, flagged);
48 - entry + SidebarWidth - delim_len - 3 - quick_log10(size) - quick_log10(new),
49 - "% d(%d)", size, new);
51 - } else if (flagged > 0) {
52 - sprintf( entry + SidebarWidth - delim_len - 3 - quick_log10(size) - quick_log10(flagged), "% d[%d]", size, flagged);
54 - sprintf( entry + SidebarWidth - delim_len - 1 - quick_log10(size), "% d", size);
57 + char int_store[20]; // up to 64 bits integers
58 + int right_width, left_width;
59 + int box_len, box_bytes;
61 + int right_offset = 0;
62 + int delim_len = strlen(SidebarDelim);
65 + right_width = left_width = 0;
66 + box_len = box_bytes = 0;
68 + // allocate an entry big enough to contain SidebarWidth wide chars
69 + entry = malloc((SidebarWidth*4)+1); // TODO: error check
71 + // determine the right space (i.e.: how big are the numbers that we want to print)
73 + int_len = snprintf(int_store, sizeof(int_store), "%d", size);
74 + right_width += int_len;
76 + right_width = 1; // to represent 0
79 + int_len = snprintf(int_store, sizeof(int_store), "%d", new);
80 + right_width += int_len + 2; // 2 is for ()
82 + if ( flagged > 0 ) {
83 + int_len = snprintf(int_store, sizeof(int_store), "%d", flagged);
84 + right_width += int_len + 2; // 2 is for []
87 + // determine how much space we have for *box and its padding (if any)
88 + left_width = SidebarWidth - right_width - 1 - delim_len; // 1 is for the space
89 + //fprintf(stdout, "left_width: %d right_width: %d\n", left_width, right_width);
90 + // right side overflow case
91 + if ( left_width <= 0 ) {
92 + snprintf(entry, SidebarWidth*4, "%-*.*s ...", SidebarWidth-4-delim_len, SidebarWidth-4-delim_len, box);
95 + right_width -= delim_len;
97 + // to support utf-8 chars we need to add enough space padding in case there
98 + // are less chars than bytes in *box
99 + box_len = mbstowcs(NULL, box, 0);
100 + box_bytes = strlen(box);
102 + //fprintf(stdout, "box_len: %d box_bytes: %d (diff: %d)\n", box_len, box_bytes, (box_bytes-box_len));
103 + // if there is less string than the space we allow, then we will add the
105 + if ( box_len != -1 && box_len < left_width ) {
106 + left_width += (box_bytes - box_len);
108 + // otherwise sprintf will truncate the string for us (therefore, no else case)
110 + // print the sidebar entry (without new and flagged messages, at the moment)
111 + //fprintf(stdout, "left_width: %d right_width: %d\n", left_width, right_width);
112 + right_offset = snprintf(entry, SidebarWidth*4, "%-*.*s %d", left_width, left_width, box, size);
114 + // then pad new and flagged messages if any
116 + right_offset += snprintf(entry+right_offset, SidebarWidth*4-right_offset, "(%d)", new);
118 + if ( flagged > 0 ) {
119 + right_offset += snprintf(entry+right_offset, SidebarWidth*4-right_offset, "[%d]", flagged);
125 void set_curbuffy(char buf[LONG_STRING])