mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 16:14:11 +01:00 
			
		
		
		
	Added a test that appends a string longer that the capacity of the builder
This commit is contained in:
		| @@ -10,9 +10,12 @@ void StringBuilder::append(const char* s) | |||||||
| { | { | ||||||
|     char* tail = buffer + length; |     char* tail = buffer + length; | ||||||
|  |  | ||||||
|     strncpy(tail, s, capacity - length); |     while (*s && length<capacity) | ||||||
|  |     { | ||||||
|  |         buffer[length++] = *s++; | ||||||
|  |     } | ||||||
|  |  | ||||||
|     length += strlen(tail); |     buffer[length] = 0; | ||||||
| } | } | ||||||
|  |  | ||||||
| void StringBuilder::appendEscaped(const char* s) | void StringBuilder::appendEscaped(const char* s) | ||||||
| @@ -24,7 +27,7 @@ void StringBuilder::appendEscaped(const char* s) | |||||||
|     // keep one slot for the end quote |     // keep one slot for the end quote | ||||||
|     capacity--; |     capacity--; | ||||||
|  |  | ||||||
|     while (*s && length<capacity) |     while (*s && length < capacity) | ||||||
|     { |     { | ||||||
|         switch (*s) |         switch (*s) | ||||||
|         { |         { | ||||||
|   | |||||||
| @@ -13,7 +13,7 @@ class StringBuilder | |||||||
| { | { | ||||||
| public: | public: | ||||||
|     StringBuilder(char* buf, size_t size) |     StringBuilder(char* buf, size_t size) | ||||||
|         : buffer(buf), capacity(size), length(0) |         : buffer(buf), capacity(size-1), length(0) | ||||||
|     { |     { | ||||||
|         buffer[0] = 0; |         buffer[0] = 0; | ||||||
|     } |     } | ||||||
| @@ -24,7 +24,7 @@ public: | |||||||
|  |  | ||||||
| private: | private: | ||||||
|     char* buffer; |     char* buffer; | ||||||
|     int capacity; |     size_t capacity; | ||||||
|     int length; |     size_t length; | ||||||
| }; | }; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -42,6 +42,12 @@ namespace JsonGeneratorTests | |||||||
|             assertResultIs("ABCDEFGH"); |             assertResultIs("ABCDEFGH"); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|  |         TEST_METHOD(AppendOverCapacity) | ||||||
|  |         { | ||||||
|  |             append("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); | ||||||
|  |             assertResultIs("ABCDEFGHIJKLMNO"); | ||||||
|  |         } | ||||||
|  |  | ||||||
|         TEST_METHOD(AppendSpecialChars) |         TEST_METHOD(AppendSpecialChars) | ||||||
|         { |         { | ||||||
|             append("\\\"\/\b\f\n\r"); |             append("\\\"\/\b\f\n\r"); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user