To: vim-dev@vim.org Subject: Patch 6.0.179 (extra) Fcc: outbox From: Bram Moolenaar MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit ------------ Patch 6.0.179 Problem: Win32: When displaying UTF-8 characters may read uninitialized memory. Solution: Add utfc_ptr2len_check_len() to avoid reading past the end of a string. Files: src/mbyte.c, src/proto/mbyte.pro, src/gui_w32.c *** ../vim60.178/src/mbyte.c Sun Nov 4 14:31:23 2001 --- src/mbyte.c Mon Feb 4 21:20:34 2002 *************** *** 1265,1270 **** --- 1265,1308 ---- } /* + * Return the number of bytes the UTF-8 encoding of the character at "p[size]" + * takes. This includes following composing characters. + */ + int + utfc_ptr2len_check_len(p, size) + char_u *p; + int size; + { + int len; + + if (*p == NUL) + return 0; + if (p[0] < 0x80 && (size == 1 || p[1] < 0x80)) /* be quick for ASCII */ + return 1; + + /* Skip over first UTF-8 char, stopping at a NUL byte. */ + len = utf_ptr2len_check_len(p, size); + + /* Check for illegal byte. */ + if (len == 1 && p[0] >= 0x80) + return 1; + + /* + * Check for composing characters. We can handle only the first two, but + * skip all of them (otherwise the cursor would get stuck). + */ + while (len < size) + { + if (p[len] < 0x80 || !utf_iscomposing(utf_ptr2char(p + len))) + break; + + /* Skip over composing char */ + len += utf_ptr2len_check_len(p + len, size - len); + } + return len; + } + + /* * Return the number of bytes the UTF-8 encoding of character "c" takes. * This does not include composing characters. */ *** ../vim60.178/src/proto/mbyte.pro Sun Nov 4 14:31:23 2001 --- src/proto/mbyte.pro Mon Feb 4 21:19:58 2002 *************** *** 24,29 **** --- 24,30 ---- int utf_byte2len __ARGS((int b)); int utf_ptr2len_check_len __ARGS((char_u *p, int size)); int utfc_ptr2len_check __ARGS((char_u *p)); + int utfc_ptr2len_check_len __ARGS((char_u *p, int size)); int utf_char2len __ARGS((int c)); int utf_char2bytes __ARGS((int c, char_u *buf)); int utf_iscomposing __ARGS((int c)); *** ../vim60.178/src/gui_w32.c Sun Feb 3 12:33:39 2002 --- src/gui_w32.c Mon Feb 4 21:19:20 2002 *************** *** 1807,1813 **** { unicodebuf[clen] = utf_ptr2char(text + i); cells += utf_char2cells(unicodebuf[clen]); ! i += utfc_ptr2len_check(text + i); ++clen; } ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row), --- 1807,1813 ---- { unicodebuf[clen] = utf_ptr2char(text + i); cells += utf_char2cells(unicodebuf[clen]); ! i += utfc_ptr2len_check_len(text + i, len - i); ++clen; } ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row), *** ../vim60.178/src/version.c Mon Feb 4 22:30:34 2002 --- src/version.c Mon Feb 4 22:30:53 2002 *************** *** 608,609 **** --- 608,611 ---- { /* Add new patch number below this line */ + /**/ + 179, /**/ -- hundred-and-one symptoms of being an internet addict: 207. You're given one phone call in prison and you ask them for a laptop. /// Bram Moolenaar -- Bram@moolenaar.net -- http://www.moolenaar.net \\\ ((( Creator of Vim -- http://vim.sf.net -- ftp://ftp.vim.org/pub/vim ))) \\\ Help me helping AIDS orphans in Uganda - http://iccf-holland.org ///