Skip to content

Commit d1ff29a

Browse files
committed
Fix an error when compiling with GCC due to strncpy_s()
1 parent c6f2beb commit d1ff29a

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,20 @@ Change log
33

44
This project follows [Semantic Versioning](http://semver.org/).
55

6+
1.0.3 (2025-04-04)
7+
==================
8+
9+
### New features
10+
11+
### Changes
12+
13+
### Fixes
14+
15+
* Fix an error when compiling with GCC due to strncpy_s().
16+
* Fix links to headings in README.md not working in GitHub.
17+
Now these do not work in Bitbucket as Markdown format is
18+
not compatible.
19+
620
1.0.2 (2024-12-02)
721
==================
822

ujson.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <stdexcept>
1717
#include <cstdlib>
1818
#include <utility>
19+
#include "string.h"
1920

2021
namespace ujson {
2122

@@ -194,6 +195,18 @@ class ObjImpl : public ArrImpl
194195
}
195196
};
196197

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+
197210
template<class T, uint32_t E>
198211
const T& val_cast(const Val* v)
199212
{
@@ -853,9 +866,9 @@ static std::string type_to_str(ValType t)
853866
case vtBool: str = "bool"; break;
854867
case vtInt : str = "int"; break;
855868
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;
859872
default:
860873
return std::to_string(t);
861874
}
@@ -985,7 +998,7 @@ const Val& Json::parse(const char* str, size_t len)
985998
len = strlen(str);
986999
}
9871000
m_buf = new char[len + 1];
988-
strncpy_s(m_buf, len + 1, str, len);
1001+
str_copy(m_buf, str, len);
9891002
m_buf[len] = 0;
9901003
return parse_in_place(m_buf);
9911004
}

0 commit comments

Comments
 (0)