Skip to content

Commit bc2182d

Browse files
committed
fix(cstdlib/mblen): バッファオーバーランの修正
残り MB_CUR_MAX バイトが読み取れることが保証されていないと未定義動作
1 parent a4b0b47 commit bc2182d

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

reference/cstdlib/mblen.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ namespace std {
3333
int main() {
3434
std::setlocale(LC_ALL, "ja_JP.UTF-8");
3535
const char *str = "こんにちは";
36-
int result = std::mblen(str, MB_CUR_MAX);
36+
int result = std::mblen(str, std::strlen(s));
3737
std::cout << result << std::endl;
3838
return 0;
3939
}
@@ -56,8 +56,9 @@ int count_chars_mblen(const char* s) {
5656

5757
int count = 0;
5858
std::size_t i = 0;
59-
while (s[i] != '\0') {
60-
int len = std::mblen(&s[i], MB_CUR_MAX);
59+
std::size_t bytes = std::strlen(s);
60+
while (i < bytes) {
61+
int len = std::mblen(&s[i], bytes - i);
6162
if (len < 0) {
6263
len = 1;
6364
}

0 commit comments

Comments
 (0)