Skip to content

Commit

Permalink
Get rid off the signed/unsigned mismatch warning in the url_idna.cpp
Browse files Browse the repository at this point in the history
Add utility class unsigned_limit to get unsigned max value of (signed)
arithmetic type
  • Loading branch information
rmisev committed Apr 6, 2017
1 parent eaf8566 commit ff23785
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/url_idna.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "url_idna.h"
#include <cassert>
#include <limits>
#include <type_traits>
// ICU
#include "unicode/uidna.h"

Expand Down Expand Up @@ -52,6 +53,14 @@ struct UIDNAWrapper {
UIDNA* value;
};

// utility class to get unsigned max value of (signed) arithmetic type
template <typename T, typename UT = std::make_unsigned<T>::type>
struct unsigned_limit {
static UT max() {
return static_cast<UT>(std::numeric_limits<T>::max());
}
};

} // namespace


Expand Down Expand Up @@ -86,7 +95,7 @@ bool IDNToASCII(const char16_t* src, size_t src_len, simple_buffer<char16_t>& ou

// uidna_nameToASCII uses int32_t length
// http://icu-project.org/apiref/icu4c/uidna_8h.html#a9cc0383836cc8b73d14e86d5014ee7ae
if (src_len > std::numeric_limits<int32_t>::max())
if (src_len > unsigned_limit<int32_t>::max())
return false; // too long

UIDNA* uidna = g_uidna.value;
Expand Down Expand Up @@ -119,7 +128,7 @@ bool IDNToUnicode(const char* src, size_t src_len, simple_buffer<char>& output)

// uidna_nameToUnicodeUTF8 uses int32_t length
// http://icu-project.org/apiref/icu4c/uidna_8h.html#a61648a995cff1f8d626df1c16ad4f3b8
if (src_len > std::numeric_limits<int32_t>::max())
if (src_len > unsigned_limit<int32_t>::max())
return false; // too long

UIDNA* uidna = g_uidna.value;
Expand Down

0 comments on commit ff23785

Please sign in to comment.