Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix warning on non MS systems for using sprintf instead of snprintf #580

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions include/crow/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -1830,19 +1830,18 @@ namespace crow
CROW_LOG_WARNING << "Invalid JSON value detected (" << v.num.d << "), value set to null";
break;
}
#ifdef _MSC_VER
#define MSC_COMPATIBLE_SPRINTF(BUFFER_PTR, FORMAT_PTR, VALUE) sprintf_s((BUFFER_PTR), 128, (FORMAT_PTR), (VALUE))
#else
#define MSC_COMPATIBLE_SPRINTF(BUFFER_PTR, FORMAT_PTR, VALUE) sprintf((BUFFER_PTR), (FORMAT_PTR), (VALUE))
#endif
enum
{
start,
decp, // Decimal point
zero
} f_state;
char outbuf[128];
MSC_COMPATIBLE_SPRINTF(outbuf, "%f", v.num.d);
#ifdef _MSC_VER
sprintf_s(outbuf, sizeof(outbuf), "%f", v.num.d);
#else
snprintf(outbuf, sizeof(outbuf), "%f", v.num.d);
#endif
char *p = &outbuf[0], *o = nullptr; // o is the position of the first trailing 0
f_state = start;
while (*p != '\0')
Expand Down Expand Up @@ -1882,7 +1881,6 @@ namespace crow
if (o != nullptr) // if any trailing 0s are found, terminate the string where they begin
*o = '\0';
out += outbuf;
#undef MSC_COMPATIBLE_SPRINTF
}
else if (v.nt == num_type::Signed_integer)
{
Expand Down