Skip to content

Commit 5312fd3

Browse files
committed
Fix CRT_SECURE warnings in clang
1 parent 23dd0bd commit 5312fd3

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

benchmark/benchmarker.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define __BENCHMARKER_H
33

44
#include "event_counter.h"
5+
#include "simdjson.h" // For SIMDJSON_DISABLE_DEPRECATED_WARNINGS
56

67
#include <cassert>
78
#include <cctype>
@@ -435,7 +436,11 @@ struct benchmarker {
435436
void print(bool tabbed_output) const {
436437
if (tabbed_output) {
437438
char* filename_copy = (char*)malloc(strlen(filename)+1);
439+
SIMDJSON_PUSH_DISABLE_WARNINGS
440+
SIMDJSON_DISABLE_DEPRECATED_WARNING // Validated CRT_SECURE safe here
438441
strcpy(filename_copy, filename);
442+
SIMDJSON_POP_DISABLE_WARNINGS
443+
439444
#if defined(__linux__)
440445
char* base = ::basename(filename_copy);
441446
#else

tests/basictests.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ namespace number_tests {
7474
uint64_t maxulp = 0;
7575
for (int i = -1075; i < 1024; ++i) {// large negative values should be zero.
7676
double expected = pow(2, i);
77-
auto n = sprintf(buf, "%.*e", std::numeric_limits<double>::max_digits10 - 1, expected);
78-
buf[n] = '\0';
77+
size_t n = snprintf(buf, sizeof(buf), "%.*e", std::numeric_limits<double>::max_digits10 - 1, expected);
78+
if (n >= sizeof(buf)) { abort(); }
7979
fflush(NULL);
8080
auto [actual, error] = parser.parse(buf, n).get<double>();
8181
if (error) { std::cerr << error << std::endl; return false; }
@@ -167,8 +167,8 @@ namespace number_tests {
167167
char buf[1024];
168168
simdjson::dom::parser parser;
169169
for (int i = -1000000; i <= 308; ++i) {// large negative values should be zero.
170-
auto n = sprintf(buf,"1e%d", i);
171-
buf[n] = '\0';
170+
size_t n = snprintf(buf, sizeof(buf), "1e%d", i);
171+
if (n >= sizeof(buf)) { abort(); }
172172
fflush(NULL);
173173

174174
auto [actual, error] = parser.parse(buf, n).get<double>();
@@ -275,19 +275,22 @@ namespace document_tests {
275275
std::vector<std::string> data;
276276
char buf[1024];
277277
for (size_t i = 0; i < n_records; ++i) {
278-
auto n = sprintf(buf,
278+
size_t n = snprintf(buf, sizeof(buf),
279279
"{\"id\": %zu, \"name\": \"name%zu\", \"gender\": \"%s\", "
280280
"\"school\": {\"id\": %zu, \"name\": \"school%zu\"}}",
281281
i, i, (i % 2) ? "male" : "female", i % 10, i % 10);
282+
if (n >= sizeof(buf)) { abort(); }
282283
data.emplace_back(std::string(buf, n));
283284
}
284285
for (size_t i = 0; i < n_records; ++i) {
285-
auto n = sprintf(buf, "{\"counter\": %f, \"array\": [%s]}", static_cast<double>(i) * 3.1416,
286-
(i % 2) ? "true" : "false");
286+
size_t n = snprintf(buf, sizeof(buf), "{\"counter\": %f, \"array\": [%s]}", static_cast<double>(i) * 3.1416,
287+
(i % 2) ? "true" : "false");
288+
if (n >= sizeof(buf)) { abort(); }
287289
data.emplace_back(std::string(buf, n));
288290
}
289291
for (size_t i = 0; i < n_records; ++i) {
290-
auto n = sprintf(buf, "{\"number\": %e}", static_cast<double>(i) * 10000.31321321);
292+
size_t n = snprintf(buf, sizeof(buf), "{\"number\": %e}", static_cast<double>(i) * 10000.31321321);
293+
if (n >= sizeof(buf)) { abort(); }
291294
data.emplace_back(std::string(buf, n));
292295
}
293296
data.emplace_back(std::string("true"));
@@ -396,10 +399,12 @@ namespace document_stream_tests {
396399
std::string data;
397400
char buf[1024];
398401
for (size_t i = 0; i < n_records; ++i) {
399-
auto n = sprintf(buf,
402+
size_t n = snprintf(buf,
403+
sizeof(buf),
400404
"{\"id\": %zu, \"name\": \"name%zu\", \"gender\": \"%s\", "
401405
"\"ete\": {\"id\": %zu, \"name\": \"eventail%zu\"}}",
402406
i, i, (i % 2) ? "homme" : "femme", i % 10, i % 10);
407+
if (n >= sizeof(buf)) { abort(); }
403408
data += std::string(buf, n);
404409
}
405410
for(size_t batch_size = 1000; batch_size < 2000; batch_size += (batch_size>1050?10:1)) {
@@ -444,10 +449,12 @@ namespace document_stream_tests {
444449
std::string data;
445450
char buf[1024];
446451
for (size_t i = 0; i < n_records; ++i) {
447-
auto n = sprintf(buf,
452+
size_t n = snprintf(buf,
453+
sizeof(buf),
448454
"{\"id\": %zu, \"name\": \"name%zu\", \"gender\": \"%s\", "
449455
"\"été\": {\"id\": %zu, \"name\": \"éventail%zu\"}}",
450456
i, i, (i % 2) ? "" : "", i % 10, i % 10);
457+
if (n >= sizeof(buf)) { abort(); }
451458
data += std::string(buf, n);
452459
}
453460
for(size_t batch_size = 1000; batch_size < 2000; batch_size += (batch_size>1050?10:1)) {

windows/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
add_library(simdjson-windows-headers INTERFACE)
22
if(MSVC)
33
target_include_directories(simdjson-windows-headers INTERFACE .)
4+
# getopt.h triggers bogus CRT_SECURE warnings. If you include them, you need this.
5+
target_compile_definitions(simdjson-windows-headers INTERFACE _CRT_SECURE_NO_WARNINGS)
46
endif()

0 commit comments

Comments
 (0)