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

warning: 'sscanf' used to convert a string to a floating-point value, but function will not report conversion errors; consider using 'strtod' instead [cert-err34-c] #872

Open
ClausKlein opened this issue Jul 17, 2024 · 1 comment

Comments

@ClausKlein
Copy link

/usr/local/opt/llvm/bin/clang-tidy -checks=-*,cert-* -p=build /Users/clausklein/Workspace/c/tests/cJSON-src/test.c
4618 warnings generated.
Suppressed 4618 warnings (4618 in non-user code).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
/usr/local/opt/llvm/bin/clang-tidy -checks=-*,cert-* -p=build /Users/clausklein/Workspace/c/tests/cJSON-src/cJSON.c
/Users/clausklein/Workspace/c/tests/cJSON-src/cJSON.c:583:14: warning: 'sscanf' used to convert a string to a floating-point value, but function will not report conversion errors; consider using 'strtod' instead [cert-err34-c]
  583 |         if ((sscanf((char*)number_buffer, "%lg", &test) != 1) || !compare_double((double)test, d))
      |              ^
5033 warnings generated.
Suppressed 5032 warnings (5032 in non-user code).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
bash-5.2$ 
@ljluestc
Copy link

ljluestc commented Feb 9, 2025


#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <math.h>
#include <string.h>
#include "cJSON.h"

// Compare floating point values with small tolerance
static int compare_double(double a, double b) {
    return fabs(a - b) < DBL_EPSILON;
}

// Updated number parsing function using strtod()
static cJSON_bool parse_number(cJSON *item, const unsigned char *num) {
    char *endptr;
    errno = 0;  // Reset error state

    double test = strtod((const char *)num, &endptr);
    if (errno != 0 || num == (unsigned char *)endptr) {
        return false;  // Failed conversion
    }

    if (!compare_double(test, item->valuedouble)) {
        return false;  // Value mismatch
    }

    item->valuedouble = test;
    item->valueint = (int)test;  // Store integer part if applicable
    return true;
}

// Example Usage
void example_usage() {
    cJSON *root = cJSON_CreateObject();
    cJSON_AddNumberToObject(root, "pi", 3.141592653589793);
    cJSON_AddNumberToObject(root, "negative", -42.789);
    cJSON_AddNumberToObject(root, "integer", 12345);

    char *json_str = cJSON_Print(root);
    printf("JSON Output: %s\n", json_str);

    // Clean up
    free(json_str);
    cJSON_Delete(root);
}

int main() {
    example_usage();
    return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants