Assert that String() and Key() are given null-terminated strings

Assert in case users attempt to pass a char array to String() or Key()
that is not null terminated; that is not the intended use of the API.
Null terminate your string buffers.
This commit is contained in:
John Stiles 2017-03-05 09:23:03 -08:00
parent 61f8c4ef0d
commit cdea825a0b
2 changed files with 16 additions and 4 deletions

View File

@ -193,9 +193,15 @@ public:
//! The compiler can give us the length of quoted strings for free.
template <typename T, size_t N>
bool String(const T (&str)[N], RAPIDJSON_ENABLEIF((internal::IsSame<Ch, T>))) { return String(str, N-1); }
bool String(const T (&str)[N], RAPIDJSON_ENABLEIF((internal::IsSame<Ch, T>))) {
RAPIDJSON_ASSERT(str[N-1] == '\0'); // you must pass in a null-terminated string (quoted constant strings are always null-terminated)
return String(str, N-1);
}
template <typename T, size_t N>
bool Key(const T (&str)[N], RAPIDJSON_ENABLEIF((internal::IsSame<Ch, T>))) { return Key(str, N-1); }
bool Key(const T (&str)[N], RAPIDJSON_ENABLEIF((internal::IsSame<Ch, T>))) {
RAPIDJSON_ASSERT(str[N-1] == '\0'); // you must pass in a null-terminated string (quoted constant strings are always null-terminated)
return Key(str, N-1);
}
//@}

View File

@ -257,9 +257,15 @@ public:
//! The compiler can give us the length of quoted strings for free.
template <typename T, size_t N>
bool String(const T (&str)[N], RAPIDJSON_ENABLEIF((internal::IsSame<Ch, T>))) { return String(str, N-1); }
bool String(const T (&str)[N], RAPIDJSON_ENABLEIF((internal::IsSame<Ch, T>))) {
RAPIDJSON_ASSERT(str[N-1] == '\0'); // you must pass in a null-terminated string (quoted constant strings are always null-terminated)
return String(str, N-1);
}
template <typename T, size_t N>
bool Key(const T (&str)[N], RAPIDJSON_ENABLEIF((internal::IsSame<Ch, T>))) { return Key(str, N-1); }
bool Key(const T (&str)[N], RAPIDJSON_ENABLEIF((internal::IsSame<Ch, T>))) {
RAPIDJSON_ASSERT(str[N-1] == '\0'); // you must pass in a null-terminated string (quoted constant strings are always null-terminated)
return Key(str, N-1);
}
//@}