Skip to content

Commit

Permalink
Fix #449: edge case in normalize_link.
Browse files Browse the repository at this point in the history
src/tools.cpp:
When the iterator points to '%', we need to check that there
are 2 valid characters before the null terminator. Since
string::end() points to the null terminator of the underlying
c-string:

['%','A','B','\0']
  ^
p:0   1   2   3

p+2:      ^

test/tools-test.cpp:
* Change expect to assert now that all tests are passing
  • Loading branch information
ThisIsFineTM committed Feb 2, 2025
1 parent 28ac2bc commit 506c2b4
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -450,8 +450,10 @@ std::string normalize_link(const std::string& input, const std::string& baseUrl)

if ( *p == '%')
{
if( (p+3) >= input.cend()){
if( (p+2) >= input.cend()){
// if the %XX token would go off the end of the string, just break
// string::end() returns an iterator pointing to the null terminator
// of the underlying c-string (guarenteed as of C++11)
break;
}
// hhx only officially supports hex unsigned char
Expand Down
4 changes: 2 additions & 2 deletions test/tools-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ TEST(tools, normalize_link)
ASSERT_EQ(normalize_link("%", "/"), "/");
ASSERT_EQ(normalize_link("%1", ""), "");

EXPECT_EQ(normalize_link("%26", ""), "&");
EXPECT_EQ(normalize_link("%27", "/"), "/\'");
ASSERT_EQ(normalize_link("%26", ""), "&");
ASSERT_EQ(normalize_link("%27", "/"), "/\'");

// ../test/tools-test.cpp:260: Failure
// Expected equality of these values:
Expand Down

0 comments on commit 506c2b4

Please sign in to comment.