comparison src/mbyte.c @ 29761:0cea0cdcce92 v9.0.0220

patch 9.0.0220: invalid memory access with for loop over NULL string Commit: https://github.com/vim/vim/commit/f6d39c31d2177549a986d170e192d8351bd571e2 Author: Bram Moolenaar <Bram@vim.org> Date: Tue Aug 16 17:50:38 2022 +0100 patch 9.0.0220: invalid memory access with for loop over NULL string Problem: Invalid memory access with for loop over NULL string. Solution: Make sure mb_ptr2len() consistently returns zero for NUL.
author Bram Moolenaar <Bram@vim.org>
date Tue, 16 Aug 2022 19:00:04 +0200
parents 6b8aaf16af99
children adb0de8be4ce
comparison
equal deleted inserted replaced
29760:946d96da668b 29761:0cea0cdcce92
1075 buf[0] = c; 1075 buf[0] = c;
1076 return 1; 1076 return 1;
1077 } 1077 }
1078 1078
1079 /* 1079 /*
1080 * mb_ptr2len() function pointer. 1080 * Get byte length of character at "*p". Returns zero when "*p" is NUL.
1081 * Get byte length of character at "*p" but stop at a NUL. 1081 * Used for mb_ptr2len() when 'encoding' latin.
1082 * For UTF-8 this includes following composing characters.
1083 * Returns 0 when *p is NUL.
1084 */ 1082 */
1085 int 1083 int
1086 latin_ptr2len(char_u *p) 1084 latin_ptr2len(char_u *p)
1087 { 1085 {
1088 return MB_BYTE2LEN(*p); 1086 return *p == NUL ? 0 : 1;
1089 } 1087 }
1090 1088
1089 /*
1090 * Get byte length of character at "*p". Returns zero when "*p" is NUL.
1091 * Used for mb_ptr2len() when 'encoding' DBCS.
1092 */
1091 static int 1093 static int
1092 dbcs_ptr2len( 1094 dbcs_ptr2len(char_u *p)
1093 char_u *p)
1094 { 1095 {
1095 int len; 1096 int len;
1096 1097
1097 // Check if second byte is not missing. 1098 if (*p == NUL)
1099 return 0;
1100
1101 // if the second byte is missing the length is 1
1098 len = MB_BYTE2LEN(*p); 1102 len = MB_BYTE2LEN(*p);
1099 if (len == 2 && p[1] == NUL) 1103 if (len == 2 && p[1] == NUL)
1100 len = 1; 1104 len = 1;
1101 return len; 1105 return len;
1102 } 1106 }
2103 } 2107 }
2104 2108
2105 /* 2109 /*
2106 * Return the number of bytes the UTF-8 encoding of the character at "p" takes. 2110 * Return the number of bytes the UTF-8 encoding of the character at "p" takes.
2107 * This includes following composing characters. 2111 * This includes following composing characters.
2112 * Returns zero for NUL.
2108 */ 2113 */
2109 int 2114 int
2110 utfc_ptr2len(char_u *p) 2115 utfc_ptr2len(char_u *p)
2111 { 2116 {
2112 int len; 2117 int len;