Skip to content

Commit

Permalink
Merge pull request #1244 from berryzplus/feature/amend_pr1034_buffer_…
Browse files Browse the repository at this point in the history
…size_limitation

正規表現キーワードのインポートで許容サイズを超える文字列を無駄にコピーしているのを修正する
  • Loading branch information
berryzplus authored Apr 26, 2020
2 parents 941ca08 + aaad252 commit c0ee803
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
3 changes: 2 additions & 1 deletion sakura_core/String_define.h
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,7 @@
#define STR_IMPEXP_REGEX1 34844
#define STR_IMPEXP_REGEX2 34845
#define STR_IMPEXP_REGEX3 34846
#define STR_IMPEXP_REGEX4 35042
#define STR_IMPEXP_DIC_NOTFOUND 34847
#define STR_IMPEXP_DIC_LENGTH 34848
#define STR_IMPEXP_DIC_RECORD 34849
Expand Down Expand Up @@ -1298,4 +1299,4 @@
#define STR_FILEDIALOG_MRU 35040
#define STR_FILEDIALOG_OPENFOLDER 35041

// Now using max number 35041 by STR_FILEDIALOG_OPENFOLDER
// Now using max number 35042 by STR_IMPEXP_REGEX4
1 change: 1 addition & 0 deletions sakura_core/sakura_rc.rc
Original file line number Diff line number Diff line change
Expand Up @@ -3865,6 +3865,7 @@ BEGIN
STR_IMPEXP_REGEX1 "キーワード数が上限に達したため切り捨てました。"
STR_IMPEXP_REGEX2 "キーワード領域がいっぱいなため切り捨てました。"
STR_IMPEXP_REGEX3 "不正なキーワードを無視しました。"
STR_IMPEXP_REGEX4 "キーワードが長過ぎるため切り捨てました。"
STR_IMPEXP_DIC_NOTFOUND "【辞書ファイルが見つかりません】"
STR_IMPEXP_DIC_LENGTH "辞書の説明は%d文字以内にしてください。\n"
STR_IMPEXP_DIC_RECORD "一部のデータが読み込めませんでした\n不正な行数: %d"
Expand Down
27 changes: 20 additions & 7 deletions sakura_core/typeprop/CImpExpManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -661,21 +661,34 @@ bool CImpExpRegex::Import( const wstring& sFileName, wstring& sErrMsg )
}
if( k != -1 ) /* 3文字カラー名からインデックス番号に変換 */
{
if( 0 < MAX_REGEX_KEYWORDLISTLEN - keywordPos - 1 ){
regexKeyArr[count].m_nColorIndex = k;
wcsncpy_s( &pKeyword[keywordPos], MAX_REGEX_KEYWORDLISTLEN - keywordPos, p, _TRUNCATE );
count++;
keywordPos += wcsnlen( &pKeyword[keywordPos], MAX_REGEX_KEYWORDLISTLEN - keywordPos ) + 1;
// pに入っている文字列の長さ
const auto ncpyLength = ::wcsnlen( p, MAX_REGEX_KEYWORDLEN );
if( ncpyLength == MAX_REGEX_KEYWORDLEN ){
// L"キーワードが長過ぎるため切り捨てました。"
sErrMsg = LS(STR_IMPEXP_REGEX4);
}else{
sErrMsg = LS(STR_IMPEXP_REGEX2);
// pKeywordに書き込める上限サイズ(NUL終端分を含む)
const size_t cchAvailableSize = MAX_REGEX_KEYWORDLISTLEN - 1 - keywordPos;

// 書き込み上限を指定して文字列コピーし、処理結果を受け取る
const auto ncpyResult = ::wcsncpy_s( &pKeyword[keywordPos], std::min<size_t>( MAX_REGEX_KEYWORDLEN, cchAvailableSize ), p, _TRUNCATE );
if( ncpyResult == 0 ){
regexKeyArr[count].m_nColorIndex = k;
count++;
keywordPos += ncpyLength + 1;
}else{
// L"キーワード領域がいっぱいなため切り捨てました。"
sErrMsg = LS(STR_IMPEXP_REGEX2);
}
}
}
}else{
// L"不正なキーワードを無視しました。"
sErrMsg = LS(STR_IMPEXP_REGEX3);
}
}
}
pKeyword[keywordPos] = L'\0';
pKeyword[keywordPos] = L'\0'; // 2個目のNUL終端を書き込む

in.Close();

Expand Down
1 change: 1 addition & 0 deletions sakura_lang_en_US/sakura_lang_rc.rc
Original file line number Diff line number Diff line change
Expand Up @@ -3874,6 +3874,7 @@ BEGIN
STR_IMPEXP_REGEX1 "keyword truncated because the number of keywords has reached the upper limit."
STR_IMPEXP_REGEX2 "keyword truncated because keyword are is full."
STR_IMPEXP_REGEX3 "Ignore invalid keyword."
STR_IMPEXP_REGEX4 "keyword truncated because too long."
STR_IMPEXP_DIC_NOTFOUND "<Could not find dict file>"
STR_IMPEXP_DIC_LENGTH "Dictionary file description should be a maximum of %d characters.\n"
STR_IMPEXP_DIC_RECORD "A part of the data couldn't be read\nIncorrect line: %d"
Expand Down

0 comments on commit c0ee803

Please sign in to comment.