Skip to content

Commit 8faf543

Browse files
committed
fix(cstdlib/mblen): 文字数カウント例を諸々修正
* std::mblen の内部状態をクリアせずに使用しているのを修正 * size_t に対する std::size_t のつけ忘れを修正 * バッファオーバーラン脆弱性の修正 (残り MB_CUR_MAX バイトあるとは保証されない)
1 parent 0b61755 commit 8faf543

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

reference/cstdlib/mblen.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,14 @@ int main() {
5151
#include <clocale>
5252

5353
int count_chars_mblen(const char* s) {
54+
// std::mblen 内部の std::mbstate_t を初期化する必要あり
55+
std::mblen(nullptr, 0);
56+
5457
int count = 0;
55-
size_t i = 0;
58+
std::size_t i = 0;
59+
std::size_t bytes = std::strlen(s);
5660
while (s[i] != '\0') {
57-
int len = std::mblen(&s[i], MB_CUR_MAX);
61+
int len = std::mblen(&s[i], bytes - i);
5862
if (len < 0) {
5963
len = 1;
6064
}

0 commit comments

Comments
 (0)