Skip to content

Commit

Permalink
test: cover pct_string_view::decode
Browse files Browse the repository at this point in the history
This commit simplifies duplicated code so that we tests all paths for the pct_string_view::decode function. Unreachable paths are also marked.

This is a partial solution to #828, where src/detail/decode.cpp has low coverage.
  • Loading branch information
alandefreitas committed Mar 15, 2024
1 parent 9d4a4a5 commit 5b86565
Showing 1 changed file with 58 additions and 43 deletions.
101 changes: 58 additions & 43 deletions src/detail/decode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,74 +54,73 @@ decode_bytes_unsafe(
return dn;
}

template <bool SpaceAsPlus>
std::size_t
decode_unsafe(
decode_unsafe_is_plus_impl(char c);

template <>
std::size_t
decode_unsafe_is_plus_impl<true>(char c)
{
return c == '+';
}

template <>
std::size_t
decode_unsafe_is_plus_impl<false>(char)
{
return false;
}


template <bool SpaceAsPlus>
std::size_t
decode_unsafe_impl(
char* const dest0,
char const* end,
core::string_view s,
encoding_opts opt) noexcept
core::string_view s) noexcept
{
auto it = s.data();
auto const last = it + s.size();
auto dest = dest0;

if(opt.space_as_plus)
{
while(it != last)
{
if(dest == end)
{
// dest too small
return dest - dest0;
}
if(*it == '+')
{
// plus to space
*dest++ = ' ';
++it;
continue;
}
if(*it == '%')
{
// escaped
++it;
if(last - it < 2)
{
// missing input,
// initialize output
std::memset(dest,
0, end - dest);
return dest - dest0;
}
*dest++ = decode_one(it);
it += 2;
continue;
}
// unescaped
*dest++ = *it++;
}
return dest - dest0;
}

while(it != last)
{
// LCOV_EXCL_START
if(dest == end)
{
// dest too small
/*
* dest too small: unreachable
* public functions always pass
* a buffer of sufficient size
*/
return dest - dest0;
}
// LCOV_EXCL_STOP
if(decode_unsafe_is_plus_impl<SpaceAsPlus>(*it))
{
// plus to space
*dest++ = ' ';
++it;
continue;
}
if(*it == '%')
{
// escaped
++it;
// LCOV_EXCL_START
if(last - it < 2)
{
// missing input,
// `%` not followed by two hex digits
// invalid input: unreachable
// public functions always pass
// a valid string_view.
// initialize output
std::memset(dest,
0, end - dest);
return dest - dest0;
}
// LCOV_EXCL_STOP
*dest++ = decode_one(it);
it += 2;
continue;
Expand All @@ -132,6 +131,22 @@ decode_unsafe(
return dest - dest0;
}

std::size_t
decode_unsafe(
char* const dest0,
char const* end,
core::string_view s,
encoding_opts opt) noexcept
{
if(opt.space_as_plus)
{
return decode_unsafe_impl<true>(
dest0, end, s);
}
return decode_unsafe_impl<false>(
dest0, end, s);
}

} // detail
} // urls
} // boost
Expand Down

0 comments on commit 5b86565

Please sign in to comment.