Просмотр исходного кода

Fixed `deserializeJson()` silently accepting a `Stream*` (issue #978)

Benoit Blanchon 6 лет назад
Родитель
Сommit
2af003e4e2

+ 1 - 0
CHANGELOG.md

@@ -4,6 +4,7 @@ ArduinoJson: change log
 HEAD
 ----
 
+* Fixed `deserializeJson()` silently accepting a `Stream*` (issue #978)
 * Fixed invalid result from `operator|` (issue #981)
 
 > ### BREAKING CHANGE

+ 1 - 1
appveyor.yml

@@ -17,4 +17,4 @@ before_build:
 build_script:
 - cmake --build . --config %CONFIGURATION%
 test_script:
-- ctest --output-on-failure .
+- ctest -C %CONFIGURATION% --output-on-failure .

+ 16 - 2
src/ArduinoJson/Deserialization/CharPointerReader.hpp

@@ -6,6 +6,16 @@
 
 namespace ARDUINOJSON_NAMESPACE {
 
+template <typename T>
+struct IsCharOrVoid {
+  static const bool value =
+      is_same<T, void>::value || is_same<T, char>::value ||
+      is_same<T, unsigned char>::value || is_same<T, signed char>::value;
+};
+
+template <typename T>
+struct IsCharOrVoid<const T> : IsCharOrVoid<T> {};
+
 class UnsafeCharPointerReader {
   const char* _ptr;
 
@@ -41,12 +51,16 @@ class SafeCharPointerReader {
 };
 
 template <typename TChar>
-inline UnsafeCharPointerReader makeReader(TChar* input) {
+inline typename enable_if<IsCharOrVoid<TChar>::value,
+                          UnsafeCharPointerReader>::type
+makeReader(TChar* input) {
   return UnsafeCharPointerReader(reinterpret_cast<const char*>(input));
 }
 
 template <typename TChar>
-inline SafeCharPointerReader makeReader(TChar* input, size_t n) {
+inline
+    typename enable_if<IsCharOrVoid<TChar>::value, SafeCharPointerReader>::type
+    makeReader(TChar* input, size_t n) {
   return SafeCharPointerReader(reinterpret_cast<const char*>(input), n);
 }
 

+ 21 - 0
test/Misc/CMakeLists.txt

@@ -13,3 +13,24 @@ add_executable(MiscTests
 
 target_link_libraries(MiscTests catch)
 add_test(Misc MiscTests)
+
+
+add_executable(Issue978
+	Issue978.cpp
+)
+set_target_properties(Issue978
+	PROPERTIES
+    	EXCLUDE_FROM_ALL TRUE
+        EXCLUDE_FROM_DEFAULT_BUILD TRUE
+)
+add_test(
+	NAME
+		Issue978
+    COMMAND
+    	${CMAKE_COMMAND} --build . --target Issue978 --config $<CONFIGURATION>
+    WORKING_DIRECTORY
+    	${CMAKE_BINARY_DIR}
+)
+set_tests_properties(Issue978 
+	PROPERTIES
+		WILL_FAIL TRUE)

+ 13 - 0
test/Misc/Issue978.cpp

@@ -0,0 +1,13 @@
+// ArduinoJson - arduinojson.org
+// Copyright Benoit Blanchon 2014-2019
+// MIT License
+
+#include <ArduinoJson.h>
+
+struct Stream {};
+
+int main() {
+  Stream* stream = 0;
+  DynamicJsonDocument doc(1024);
+  deserializeJson(doc, stream);
+}