|
16 | 16 | #include <stdexcept> |
17 | 17 | #include <cstdlib> |
18 | 18 | #include <utility> |
| 19 | +#include "string.h" |
19 | 20 |
|
20 | 21 | namespace ujson { |
21 | 22 |
|
@@ -194,6 +195,18 @@ class ObjImpl : public ArrImpl |
194 | 195 | } |
195 | 196 | }; |
196 | 197 |
|
| 198 | +static void str_copy(char* dst, const char* src, size_t count) |
| 199 | +{ |
| 200 | + // We are not using strncpy() to avoid compiler warning. |
| 201 | + // For eaxmple VC++ warning C4996 "This function or variable may be unsafe". |
| 202 | + // We are not using the safe version strncpy_s() because it is not well supported. |
| 203 | + while (count--) { |
| 204 | + const char c = *src++; |
| 205 | + *dst++ = c; |
| 206 | + if (0 == c) break; |
| 207 | + } |
| 208 | +} |
| 209 | + |
197 | 210 | template<class T, uint32_t E> |
198 | 211 | const T& val_cast(const Val* v) |
199 | 212 | { |
@@ -853,9 +866,9 @@ static std::string type_to_str(ValType t) |
853 | 866 | case vtBool: str = "bool"; break; |
854 | 867 | case vtInt : str = "int"; break; |
855 | 868 | case vtF64 : str = "float"; break; |
856 | | - case vtStr: str = "str"; break; |
857 | | - case vtArr: str = "arr"; break; |
858 | | - case vtObj: str = "obj"; break; |
| 869 | + case vtStr : str = "str"; break; |
| 870 | + case vtArr : str = "arr"; break; |
| 871 | + case vtObj : str = "obj"; break; |
859 | 872 | default: |
860 | 873 | return std::to_string(t); |
861 | 874 | } |
@@ -985,7 +998,7 @@ const Val& Json::parse(const char* str, size_t len) |
985 | 998 | len = strlen(str); |
986 | 999 | } |
987 | 1000 | m_buf = new char[len + 1]; |
988 | | - strncpy_s(m_buf, len + 1, str, len); |
| 1001 | + str_copy(m_buf, str, len); |
989 | 1002 | m_buf[len] = 0; |
990 | 1003 | return parse_in_place(m_buf); |
991 | 1004 | } |
|
0 commit comments