Skip to content

Commit eda7171

Browse files
kevinbackhouseneheb
authored andcommitted
base64decode should return immediately if the input is empty
1 parent 6f9387c commit eda7171

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/futils.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ static char to_hex(char code) {
9090

9191
/// @brief Convert a hex character to its integer value.
9292
static char from_hex(char ch) {
93-
return isdigit(ch) ? ch - '0' : static_cast<char>(tolower(ch)) - 'a' + 10;
93+
return 0xF & (isdigit(ch) ? ch - '0' : static_cast<char>(tolower(ch)) - 'a' + 10);
9494
}
9595

9696
std::string urlencode(const std::string& str) {
@@ -168,7 +168,7 @@ int base64encode(const void* data_buf, size_t dataLength, char* result, size_t r
168168
size_t base64decode(const char* in, char* out, size_t out_size) {
169169
size_t result = 0;
170170
size_t input_length = in ? ::strlen(in) : 0;
171-
if (!in || input_length % 4 != 0)
171+
if (!in || input_length % 4 != 0 || input_length == 0)
172172
return result;
173173

174174
auto encoding_table = reinterpret_cast<const unsigned char*>(base64_encode);
@@ -324,21 +324,26 @@ Uri Uri::Parse(const std::string& uri) {
324324
auto hostEnd = std::find(authEnd, (pathStart != uriEnd) ? pathStart : queryStart,
325325
':'); // check for port
326326

327-
result.Host = std::string(hostStart, hostEnd);
327+
if (hostStart < hostEnd) {
328+
result.Host = std::string(hostStart, hostEnd);
329+
}
328330

329331
// port
330332
if ((hostEnd != uriEnd) && (*hostEnd == ':')) // we have a port
331333
{
332334
++hostEnd;
333335
auto portEnd = (pathStart != uriEnd) ? pathStart : queryStart;
334-
result.Port = std::string(hostEnd, portEnd);
336+
if (hostEnd < portEnd) {
337+
result.Port = std::string(hostEnd, portEnd);
338+
}
335339
}
336340
if (result.Port.empty() && result.Protocol == "http")
337341
result.Port = "80";
338342

339343
// path
340-
if (pathStart != uriEnd)
344+
if (pathStart < queryStart) {
341345
result.Path = std::string(pathStart, queryStart);
346+
}
342347

343348
// query
344349
if (queryStart != uriEnd)

0 commit comments

Comments
 (0)