kgabis/parson

sprintf hard-deprecated on MacOS prevents compiling

rplsix opened this issue · 3 comments

rplsix commented

Error given if compiling on MacOS:

parson/parson.c:1247:27: error: 'sprintf' is deprecated: This function is provided for compatibility reasons only.  Due to security concerns inherent in the design of sprintf(3), it is highly recommended that you use snprintf(3) instead. [-Werror,-Wdeprecated-declarations]
                written = sprintf(num_buf, parson_float_format, num);
                          ^

Affected are these lines starting at 1247:

                written = sprintf(num_buf, parson_float_format, num);
            } else {
                written = sprintf(num_buf, PARSON_DEFAULT_FLOAT_FORMAT, num);

If changed to this it would be compiling:

                written = snprintf(num_buf, sizeof(num_buf), parson_float_format, num);
            } else {
                written = snprintf(num_buf, sizeof(num_buf), PARSON_DEFAULT_FLOAT_FORMAT, num);
kgabis commented

Hi, it's not that simple since there's no snprintf in C89. You can use -Wno-deprecated-declarations.
Also, sizeof(num_buf) is wrong.

rilysh commented

macOS by default uses clang, so using pragmas can be a workaround

#ifdef __APPLE__
#ifdef __clang__
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Wdeprecated-declarations"
    /* sprintf(...) function */
    #pragma clang diagnostic pop
#endif
#endif

There's another thing can be done is to create another function that behaves like a snprintf(...); a custom variadic function using vsnprintf(...). Though it's not a part of C89 spec.

kgabis commented

Fixed in ba29f4e