Skip to content

Commit

Permalink
Dom: Fix accessing first character for std::string (#305)
Browse files Browse the repository at this point in the history
std::stringの変数について冗長または範囲外なアクセスの可能性を
cppcheckに警告されたため条件文を整理して修正します。

cppcheckのレポート
```
src/xml/dom.cpp:133:41: warning: Either the condition 'open_tag.empty()' is redundant or expression 'open_tag[0]' cause access out of bounds. [containerOutOfBounds]
            bool is_alpha = ( ( open_tag[0] >= 'A' && open_tag[0] <= 'Z' ) || ( open_tag[0] >= 'a' && open_tag[0] <= 'z' ) );
                                        ^
src/xml/dom.cpp:137:31: note: Assuming that condition 'open_tag.empty()' is not redundant
            if( open_tag.empty() || ! is_alpha ) continue;
                              ^
src/xml/dom.cpp:133:41: note: Access out of bounds
            bool is_alpha = ( ( open_tag[0] >= 'A' && open_tag[0] <= 'Z' ) || ( open_tag[0] >= 'a' && open_tag[0] <= 'z' ) );
                                        ^
```
  • Loading branch information
ma8ma committed May 24, 2020
1 parent 0255370 commit 1051f5c
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/xml/dom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,16 @@ void Dom::parse( const std::string& str )

// 開始タグの中身( <element attr="value"> )を取り出す
std::string open_tag = str.substr( tag_lt_pos + 1, tag_gt_pos - tag_lt_pos - 1 );
if( open_tag.empty() ) continue;

// タグの中身がアルファベットで始まっているか
bool is_alpha = ( ( open_tag[0] >= 'A' && open_tag[0] <= 'Z' ) || ( open_tag[0] >= 'a' && open_tag[0] <= 'z' ) );
// タグの中身がアルファベットで始まっているかチェック
if( !( open_tag[0] >= 'A' && open_tag[0] <= 'Z' ) && !( open_tag[0] >= 'a' && open_tag[0] <= 'z' ) ) {
continue;
}

// タグ構造が壊れてる場合
size_t broken_pos = 0;
if( open_tag.empty() || ! is_alpha ) continue;
else if( ( broken_pos = open_tag.find( '<' ) ) != std::string::npos )
if( ( broken_pos = open_tag.find( '<' ) ) != std::string::npos )
{
current_pos += broken_pos;
continue;
Expand Down

0 comments on commit 1051f5c

Please sign in to comment.