Skip to content

Commit

Permalink
fix build warnings
Browse files Browse the repository at this point in the history
➜  /Users/junbozheng/project/cNV git:(master) ✗ cmake --build build -j16
[ 20%] Building C object CMakeFiles/cNV.dir/cJSON/cJSON_Utils.c.o
[ 60%] Building C object CMakeFiles/cNV.dir/test.c.o
[ 60%] Building C object CMakeFiles/cNV.dir/cJSON/cJSON.c.o
[ 80%] Building C object CMakeFiles/cNV.dir/nv/nv.c.o
/Users/junbozheng/project/cNV/nv/nv.c:100:32: error: passing 'uint8_t[1024]' (aka 'unsigned char[1024]') to parameter of type 'const char *' converts between pointers to integer types where one is of the unique plain 'char' type and the other is not [-Werror,-Wpointer-sign]
            json = cJSON_Parse(nv_buffer);
                               ^~~~~~~~~
/Users/junbozheng/project/cNV/cJSON/cJSON.h:148:47: note: passing argument to parameter 'value' here
CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value);
                                              ^
/Users/junbozheng/project/cNV/nv/nv.c:244:17: error: passing 'uint8_t[1024]' (aka 'unsigned char[1024]') to parameter of type 'char *' converts between pointers to integer types where one is of the unique plain 'char' type and the other is not [-Werror,-Wpointer-sign]
        sprintf(nv_buffer, "%d.%d.%d.%d", *((uint32_t *)value),
                ^~~~~~~~~
/Library/Developer/CommandLineTools/SDKs/MacOSX14.2.sdk/usr/include/secure/_stdio.h:47:28: note: expanded from macro 'sprintf'
  __builtin___sprintf_chk (str, 0, __darwin_obsz(str), __VA_ARGS__)
                           ^~~
/Users/junbozheng/project/cNV/nv/nv.c:248:44: error: passing 'uint8_t[1024]' (aka 'unsigned char[1024]') to parameter of type 'const char *' converts between pointers to integer types where one is of the unique plain 'char' type and the other is not [-Werror,-Wpointer-sign]
            cJSON_SetValuestring(key_item, nv_buffer);
                                           ^~~~~~~~~
/Users/junbozheng/project/cNV/cJSON/cJSON.h:280:69: note: passing argument to parameter 'valuestring' here
CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring);
                                                                    ^
/Users/junbozheng/project/cNV/nv/nv.c:250:48: error: passing 'uint8_t[1024]' (aka 'unsigned char[1024]') to parameter of type 'const char *' converts between pointers to integer types where one is of the unique plain 'char' type and the other is not [-Werror,-Wpointer-sign]
            cJSON_AddStringToObject(json, key, nv_buffer);
                                               ^~~~~~~~~
/Users/junbozheng/project/cNV/cJSON/cJSON.h:269:112: note: passing argument to parameter 'string' here
CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string);
                                                                                                               ^
/Users/junbozheng/project/cNV/nv/nv.c:255:17: error: passing 'uint8_t[1024]' (aka 'unsigned char[1024]') to parameter of type 'char *' converts between pointers to integer types where one is of the unique plain 'char' type and the other is not [-Werror,-Wpointer-sign]
        sprintf(nv_buffer, "%d-%d-%d-%d-%d-%d", *((uint32_t *)value),
                ^~~~~~~~~
/Library/Developer/CommandLineTools/SDKs/MacOSX14.2.sdk/usr/include/secure/_stdio.h:47:28: note: expanded from macro 'sprintf'
  __builtin___sprintf_chk (str, 0, __darwin_obsz(str), __VA_ARGS__)
                           ^~~
/Users/junbozheng/project/cNV/nv/nv.c:261:44: error: passing 'uint8_t[1024]' (aka 'unsigned char[1024]') to parameter of type 'const char *' converts between pointers to integer types where one is of the unique plain 'char' type and the other is not [-Werror,-Wpointer-sign]
            cJSON_SetValuestring(key_item, nv_buffer);
                                           ^~~~~~~~~
/Users/junbozheng/project/cNV/cJSON/cJSON.h:280:69: note: passing argument to parameter 'valuestring' here
CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring);
                                                                    ^
/Users/junbozheng/project/cNV/nv/nv.c:263:48: error: passing 'uint8_t[1024]' (aka 'unsigned char[1024]') to parameter of type 'const char *' converts between pointers to integer types where one is of the unique plain 'char' type and the other is not [-Werror,-Wpointer-sign]
            cJSON_AddStringToObject(json, key, nv_buffer);
                                               ^~~~~~~~~
/Users/junbozheng/project/cNV/cJSON/cJSON.h:269:112: note: passing argument to parameter 'string' here
CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string);
                                                                                                               ^
/Users/junbozheng/project/cNV/nv/nv.c:297:31: error: passing 'uint8_t[1024]' (aka 'unsigned char[1024]') to parameter of type 'const char *' converts between pointers to integer types where one is of the unique plain 'char' type and the other is not [-Werror,-Wpointer-sign]
    cJSON* json = cJSON_Parse(nv_buffer);
                              ^~~~~~~~~
/Users/junbozheng/project/cNV/cJSON/cJSON.h:148:47: note: passing argument to parameter 'value' here
CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value);
                                              ^
/Users/junbozheng/project/cNV/nv/nv.c:289:64: error: unused parameter 'len' [-Werror,-Wunused-parameter]
bool nv_get(const char* file, char* key, char* value, uint32_t len,
                                                               ^
/Users/junbozheng/project/cNV/test.c:341:23: error: comparison of integers of different signs: 'int' and 'unsigned long' [-Werror,-Wsign-compare]
    for (int i = 0; i < ARRAY_SIZE(score_buf); i++) {
                    ~ ^ ~~~~~~~~~~~~~~~~~~~~~/Users/junbozheng/project/cNV/nv/nv.c
:348:32: error: /Users/junbozheng/project/cNV/test.c:357comparison of integers of different signs: 'uint32_t' (aka 'unsigned int') and 'int' [-Werror,-Wsign-compare]:23
: error: comparison of integers of different signs: 'int' and 'unsigned long' [-Werror,-Wsign-compare]        for (uint32_t i = 0; i < size; i++) {

                             ~ ^ ~~~~
    for (int i = 0; i < ARRAY_SIZE(score_buf); i++) {
                    ~ ^ ~~~~~~~~~~~~~~~~~~~~~
/Users/junbozheng/project/cNV/nv/nv.c:400:31: error: passing 'uint8_t[1024]' (aka 'unsigned char[1024]') to parameter of type 'const char *' converts between pointers to integer types where one is of the unique plain 'char' type and the other is not [-Werror,-Wpointer-sign]
    cJSON* json = cJSON_Parse(nv_buffer);
                              ^~~~~~~~~
/Users/junbozheng/project/cNV/cJSON/cJSON.h:148:47: note: passing argument to parameter 'value' here
CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value);
                                              ^
2 errors generated.
11 errors generated.
make[2]: *** [CMakeFiles/cNV.dir/nv/nv.c.o] Error 1
make[2]: *** Waiting for unfinished jobs....
make[2]: *** [CMakeFiles/cNV.dir/test.c.o] Error 1
make[1]: *** [CMakeFiles/cNV.dir/all] Error 2
make: *** [all] Error 2

Signed-off-by: Junbo Zheng <[email protected]>
  • Loading branch information
Junbo-Zheng committed Mar 1, 2024
1 parent 05eb7ba commit 76a0bd3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
36 changes: 21 additions & 15 deletions nv/nv.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@

#include "cJSON.h"

#ifndef UNUSED
#define UNUSED(x) ((void)(x))
#endif /* UNUSED */

#ifndef CONFIG_NV_DATA_BUFFER_SIZE
#define CONFIG_NV_DATA_BUFFER_SIZE 512 // TODO, 1024 Bytes enough?
#endif /* CONFIG_NV_DATA_BUFFER_SIZE */
Expand Down Expand Up @@ -97,7 +101,7 @@ void nv_sync(const char* file, char* key, void* value, uint32_t len,

if (access(file, F_OK) == 0) {
if (nv_read(file, nv_buffer)) {
json = cJSON_Parse(nv_buffer);
json = cJSON_Parse((const char*)nv_buffer);
} else {
nv_log("nv sync, nv read %s fail, error %d %s\n", file, errno,
strerror(errno));
Expand Down Expand Up @@ -241,26 +245,26 @@ void nv_sync(const char* file, char* key, void* value, uint32_t len,
}
case NV_DATA_IP:
memset(nv_buffer, 0, sizeof(nv_buffer));
sprintf(nv_buffer, "%d.%d.%d.%d", *((uint32_t *)value),
*((uint32_t *)value + 1), *((uint32_t *)value + 2),
*((uint32_t *)value + 3));
sprintf((char*)nv_buffer, "%d.%d.%d.%d", *((uint32_t*)value),
*((uint32_t*)value + 1), *((uint32_t*)value + 2),
*((uint32_t*)value + 3));
if (key_item) {
cJSON_SetValuestring(key_item, nv_buffer);
cJSON_SetValuestring(key_item, (const char*)nv_buffer);
} else {
cJSON_AddStringToObject(json, key, nv_buffer);
cJSON_AddStringToObject(json, key, (const char* const)nv_buffer);
}
break;
case NV_DATA_MAC:
memset(nv_buffer, 0, sizeof(nv_buffer));
sprintf(nv_buffer, "%d-%d-%d-%d-%d-%d", *((uint32_t *)value),
*((uint32_t *)value + 1), *((uint32_t *)value + 2),
*((uint32_t *)value + 3), *((uint32_t *)value + 4),
*((uint32_t *)value + 5));
sprintf((char*)nv_buffer, "%d-%d-%d-%d-%d-%d", *((uint32_t*)value),
*((uint32_t*)value + 1), *((uint32_t*)value + 2),
*((uint32_t*)value + 3), *((uint32_t*)value + 4),
*((uint32_t*)value + 5));

if (key_item) {
cJSON_SetValuestring(key_item, nv_buffer);
cJSON_SetValuestring(key_item, (const char*)nv_buffer);
} else {
cJSON_AddStringToObject(json, key, nv_buffer);
cJSON_AddStringToObject(json, key, (const char* const)nv_buffer);
}
break;
default:
Expand Down Expand Up @@ -289,12 +293,14 @@ void nv_sync(const char* file, char* key, void* value, uint32_t len,
bool nv_get(const char* file, char* key, char* value, uint32_t len,
nv_data_type_t type)
{
UNUSED(len);

uint8_t nv_buffer[CONFIG_NV_DATA_BUFFER_SIZE] = { 0 };
if (nv_read(file, nv_buffer) == false) {
return false;
}

cJSON* json = cJSON_Parse(nv_buffer);
cJSON* json = cJSON_Parse((const char*)nv_buffer);
if (json == NULL) {
nv_log("cJSON_Parse fail %s\n", cJSON_GetErrorPtr());
return false;
Expand Down Expand Up @@ -345,7 +351,7 @@ bool nv_get(const char* file, char* key, char* value, uint32_t len,
case NV_DATA_FLOAT_ARRAY:
case NV_DATA_DOUBLE_ARRAY: {
int size = cJSON_GetArraySize(key_item);
for (uint32_t i = 0; i < size; i++) {
for (int i = 0; i < size; i++) {
cJSON* array_json = cJSON_GetArrayItem(key_item, i);
if (array_json) {
if (type == NV_DATA_STRING_ARRAY) {
Expand Down Expand Up @@ -397,7 +403,7 @@ bool nv_delete(const char* file, char* key)
return false;
}

cJSON* json = cJSON_Parse(nv_buffer);
cJSON* json = cJSON_Parse((const char*)nv_buffer);
if (json == NULL) {
nv_log("cJSON_Parse fail %s\n", cJSON_GetErrorPtr());
return false;
Expand Down
8 changes: 4 additions & 4 deletions test.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ typedef struct {

static nv_t nv = { 0 };

static void cjson_create_item(void)
__attribute__((unused)) static void cjson_create_item(void)
{
cJSON* root = cJSON_CreateObject();

Expand All @@ -74,7 +74,7 @@ static void cjson_create_item(void)
cJSON_Delete(root);
}

static void cjson_add_item(void)
__attribute__((unused)) static void cjson_add_item(void)
{
char json[999]
= { "{\"name\":\"ZhangSan\",\"age\":20,\"height\":180,\"weight\":60}" };
Expand Down Expand Up @@ -338,7 +338,7 @@ int main(void)
nv_log("temp_double = %f\n", temp_double);

char* score_buf[16] = {0};
for (int i = 0; i < ARRAY_SIZE(score_buf); i++) {
for (size_t i = 0; i < ARRAY_SIZE(score_buf); i++) {
score_buf[i] = calloc(1, ARRAY_SIZE(score_buf) * sizeof(char));
}

Expand All @@ -354,7 +354,7 @@ int main(void)
nv_log("score float %f %f\n", score_float[0], score_float[1]);
nv_log("score double %f %f\n", score_double[0], score_double[1]);

for (int i = 0; i < ARRAY_SIZE(score_buf); i++) {
for (size_t i = 0; i < ARRAY_SIZE(score_buf); i++) {
if (score_buf[i]) {
free(score_buf[i]);
}
Expand Down

0 comments on commit 76a0bd3

Please sign in to comment.