From cdea825a0bc81531d2cd2758362b606106373477 Mon Sep 17 00:00:00 2001 From: John Stiles Date: Sun, 5 Mar 2017 09:23:03 -0800 Subject: [PATCH] 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. --- include/rapidjson/prettywriter.h | 10 ++++++++-- include/rapidjson/writer.h | 10 ++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/include/rapidjson/prettywriter.h b/include/rapidjson/prettywriter.h index 64301b8..abea404 100644 --- a/include/rapidjson/prettywriter.h +++ b/include/rapidjson/prettywriter.h @@ -193,9 +193,15 @@ public: //! The compiler can give us the length of quoted strings for free. template - bool String(const T (&str)[N], RAPIDJSON_ENABLEIF((internal::IsSame))) { return String(str, N-1); } + bool String(const T (&str)[N], RAPIDJSON_ENABLEIF((internal::IsSame))) { + 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 - bool Key(const T (&str)[N], RAPIDJSON_ENABLEIF((internal::IsSame))) { return Key(str, N-1); } + bool Key(const T (&str)[N], RAPIDJSON_ENABLEIF((internal::IsSame))) { + 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); + } //@} diff --git a/include/rapidjson/writer.h b/include/rapidjson/writer.h index 755f483..c438f71 100644 --- a/include/rapidjson/writer.h +++ b/include/rapidjson/writer.h @@ -257,9 +257,15 @@ public: //! The compiler can give us the length of quoted strings for free. template - bool String(const T (&str)[N], RAPIDJSON_ENABLEIF((internal::IsSame))) { return String(str, N-1); } + bool String(const T (&str)[N], RAPIDJSON_ENABLEIF((internal::IsSame))) { + 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 - bool Key(const T (&str)[N], RAPIDJSON_ENABLEIF((internal::IsSame))) { return Key(str, N-1); } + bool Key(const T (&str)[N], RAPIDJSON_ENABLEIF((internal::IsSame))) { + 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); + } //@}