Skip to content

Commit

Permalink
Fix StringData::substr with single argument
Browse files Browse the repository at this point in the history
Original version had an overflow bug
  • Loading branch information
RedBeard0531 committed Jan 9, 2013
1 parent b2ab973 commit 0c05bdf
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/mongo/base/string_data-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ namespace mongo {
if ( pos > size() )
throw std::out_of_range( "out of range" );

if ( pos + n > size() )
// truncate to end of string
if ( n > size() - pos )
n = size() - pos;

return StringData( _data + pos, n );
Expand Down
47 changes: 37 additions & 10 deletions src/mongo/base/string_data_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,45 @@ namespace {
}

// this is to verify we match std::string
#define SUBSTR_TEST_HELP(big,small,start,len) \
ASSERT_EQUALS( (string)small, ((string)big).substr( start, len ) ); \
ASSERT_EQUALS( StringData(small), StringData(big).substr( start, len ) );
void SUBSTR_TEST_HELP(StringData big, StringData small, size_t start, size_t len) {
ASSERT_EQUALS(small.toString(), big.toString().substr(start, len));
ASSERT_EQUALS(small, StringData(big).substr(start, len));
}
void SUBSTR_TEST_HELP(StringData big, StringData small, size_t start) {
ASSERT_EQUALS(small.toString(), big.toString().substr(start));
ASSERT_EQUALS(small, StringData(big).substr(start));
}

// [12] is number of args to substr
#define SUBSTR_1_TEST_HELP(big,small,start) \
ASSERT_EQUALS( StringData(small).toString(), StringData(big).toString().substr(start) ); \
ASSERT_EQUALS( StringData(small), StringData(big).substr(start) );

#define SUBSTR_2_TEST_HELP(big,small,start,len) \
ASSERT_EQUALS( StringData(small).toString(), StringData(big).toString().substr(start, len) ); \
ASSERT_EQUALS( StringData(small), StringData(big).substr(start, len) );

TEST(Substr, Simple1 ) {
SUBSTR_TEST_HELP( "abcde", "abcde", 0, 10 );
SUBSTR_TEST_HELP( "abcde", "abcde", 0, 5 );
SUBSTR_TEST_HELP( "abcde", "abc", 0, 3 );
SUBSTR_TEST_HELP( "abcde", "cde", 2, 5 );
SUBSTR_TEST_HELP( "abcde", "cde", 2, 3 );
SUBSTR_TEST_HELP( "abcde", "cd", 2, 2 );
SUBSTR_TEST_HELP( "abcde", "cd", 2, 2 );
SUBSTR_1_TEST_HELP( "abcde", "abcde", 0 );
SUBSTR_2_TEST_HELP( "abcde", "abcde", 0, 10 );
SUBSTR_2_TEST_HELP( "abcde", "abcde", 0, 5 );
SUBSTR_2_TEST_HELP( "abcde", "abc", 0, 3 );
SUBSTR_1_TEST_HELP( "abcde", "cde", 2 );
SUBSTR_2_TEST_HELP( "abcde", "cde", 2, 5 );
SUBSTR_2_TEST_HELP( "abcde", "cde", 2, 3 );
SUBSTR_2_TEST_HELP( "abcde", "cd", 2, 2 );
SUBSTR_2_TEST_HELP( "abcde", "cd", 2, 2 );
SUBSTR_1_TEST_HELP( "abcde", "", 5 );
SUBSTR_2_TEST_HELP( "abcde", "", 5, 0 );
SUBSTR_2_TEST_HELP( "abcde", "", 5, 10 );

// make sure we don't blow past the end of the StringData
SUBSTR_1_TEST_HELP( StringData("abcdeXXX", 5), "abcde", 0);
SUBSTR_2_TEST_HELP( StringData("abcdeXXX", 5), "abcde", 0, 10);
SUBSTR_1_TEST_HELP( StringData("abcdeXXX", 5), "de", 3);
SUBSTR_2_TEST_HELP( StringData("abcdeXXX", 5), "de", 3, 7);
SUBSTR_1_TEST_HELP( StringData("abcdeXXX", 5), "", 5);
SUBSTR_2_TEST_HELP( StringData("abcdeXXX", 5), "", 5, 1);
}

TEST( equalCaseInsensitiveTest, Simple1 ) {
Expand Down

0 comments on commit 0c05bdf

Please sign in to comment.