From 09bbc3ed009639150f4cd387d50adf8b46d7958d Mon Sep 17 00:00:00 2001 From: estkme <145633413+estkme@users.noreply.github.com> Date: Tue, 19 Dec 2023 23:29:22 +0800 Subject: [PATCH] add cJSON_AddStringOrNullToObject to make datastruct stable --- cjson/cJSON_ex.c | 13 +++++++++++++ cjson/cJSON_ex.h | 5 +++++ euicc/es9p.c | 4 ++-- interface/apdu/pcsc.c | 10 +++++----- interface/apdu/stdio.c | 2 +- interface/http/stdio.c | 8 ++++---- src/applet/chip/info.c | 20 ++++++++++---------- src/applet/notification/list.c | 6 +++--- src/applet/profile/list.c | 18 +++++++++--------- src/jprint.c | 14 +++++++------- src/jprint.h | 2 +- 11 files changed, 60 insertions(+), 42 deletions(-) create mode 100644 cjson/cJSON_ex.c create mode 100644 cjson/cJSON_ex.h diff --git a/cjson/cJSON_ex.c b/cjson/cJSON_ex.c new file mode 100644 index 0000000..eb890bf --- /dev/null +++ b/cjson/cJSON_ex.c @@ -0,0 +1,13 @@ +#include "cJSON_ex.h" + +CJSON_PUBLIC(cJSON *) cJSON_AddStringOrNullToObject(cJSON *const object, const char *const name, const char *const string) +{ + if (string) + { + return cJSON_AddStringToObject(object, name, string); + } + else + { + return cJSON_AddNullToObject(object, name); + } +} diff --git a/cjson/cJSON_ex.h b/cjson/cJSON_ex.h new file mode 100644 index 0000000..ca9277d --- /dev/null +++ b/cjson/cJSON_ex.h @@ -0,0 +1,5 @@ +#pragma once + +#include "cJSON.h" + +CJSON_PUBLIC(cJSON *) cJSON_AddStringOrNullToObject(cJSON *const object, const char *const name, const char *const string); diff --git a/euicc/es9p.c b/euicc/es9p.c index 3cccc26..64acfc0 100644 --- a/euicc/es9p.c +++ b/euicc/es9p.c @@ -4,7 +4,7 @@ #include #include -#include +#include static const char *lpa_header[] = { "User-Agent: gsma-rsp-lpad", @@ -99,7 +99,7 @@ static int es9p_trans_json(struct euicc_ctx *ctx, const char *smdp, const char * for (int i = 0; ikey[i] != NULL; i++) { - if (!cJSON_AddStringToObject(sjroot, ikey[i], idata[i])) + if (!cJSON_AddStringOrNullToObject(sjroot, ikey[i], idata[i])) { goto err; } diff --git a/interface/apdu/pcsc.c b/interface/apdu/pcsc.c index 27d43da..68b3362 100644 --- a/interface/apdu/pcsc.c +++ b/interface/apdu/pcsc.c @@ -12,7 +12,7 @@ #include -#include +#include #define INTERFACE_SELECT_ENV "DRIVER_IFID" @@ -274,7 +274,7 @@ static int json_print(cJSON *jpayload) goto err; } - if (cJSON_AddStringToObject(jroot, "type", "driver") == NULL) + if (cJSON_AddStringOrNullToObject(jroot, "type", "driver") == NULL) { goto err; } @@ -371,12 +371,12 @@ static int pcsc_list_iter(int index, const char *reader, void *userdata) return -1; } - if (!cJSON_AddStringToObject(jreader, "env", index_str)) + if (!cJSON_AddStringOrNullToObject(jreader, "env", index_str)) { return -1; } - if (!cJSON_AddStringToObject(jreader, "name", reader)) + if (!cJSON_AddStringOrNullToObject(jreader, "name", reader)) { return -1; } @@ -426,7 +426,7 @@ EUICC_SHARED_EXPORT int libapduinterface_main(int argc, char **argv) return -1; } - if (!cJSON_AddStringToObject(payload, "env", INTERFACE_SELECT_ENV)) + if (!cJSON_AddStringOrNullToObject(payload, "env", INTERFACE_SELECT_ENV)) { return -1; } diff --git a/interface/apdu/stdio.c b/interface/apdu/stdio.c index 24f8df6..f3812ea 100644 --- a/interface/apdu/stdio.c +++ b/interface/apdu/stdio.c @@ -4,7 +4,7 @@ #include #include -#include +#include #include diff --git a/interface/http/stdio.c b/interface/http/stdio.c index 6b2ef98..798dea9 100644 --- a/interface/http/stdio.c +++ b/interface/http/stdio.c @@ -4,7 +4,7 @@ #include #include -#include +#include #include @@ -152,7 +152,7 @@ static int json_print(cJSON *jpayload) goto err; } - if (cJSON_AddStringToObject(jroot, "type", "http") == NULL) + if (cJSON_AddStringOrNullToObject(jroot, "type", "http") == NULL) { goto err; } @@ -206,11 +206,11 @@ static int json_request(const char *url, const uint8_t *tx, uint32_t tx_len, con { goto err; } - if (cJSON_AddStringToObject(jpayload, "url", url) == NULL) + if (cJSON_AddStringOrNullToObject(jpayload, "url", url) == NULL) { goto err; } - if (cJSON_AddStringToObject(jpayload, "tx", tx_hex) == NULL) + if (cJSON_AddStringOrNullToObject(jpayload, "tx", tx_hex) == NULL) { goto err; } diff --git a/src/applet/chip/info.c b/src/applet/chip/info.c index 9c085b9..f7269e3 100644 --- a/src/applet/chip/info.c +++ b/src/applet/chip/info.c @@ -32,18 +32,18 @@ static int applet_main(int argc, char **argv) } jdata = cJSON_CreateObject(); - cJSON_AddStringToObject(jdata, "eid", eid); - cJSON_AddStringToObject(jdata, "default_smds", default_smds); - cJSON_AddStringToObject(jdata, "default_smdp", default_smdp); + cJSON_AddStringOrNullToObject(jdata, "eid", eid); + cJSON_AddStringOrNullToObject(jdata, "default_smds", default_smds); + cJSON_AddStringOrNullToObject(jdata, "default_smdp", default_smdp); if (jeuiccinfo2) { - cJSON_AddStringToObject(jeuiccinfo2, "profile_version", euiccinfo2.profile_version); - cJSON_AddStringToObject(jeuiccinfo2, "sgp22_version", euiccinfo2.sgp22_version); - cJSON_AddStringToObject(jeuiccinfo2, "euicc_firmware_version", euiccinfo2.euicc_firmware_version); - cJSON_AddStringToObject(jeuiccinfo2, "uicc_firmware_version", euiccinfo2.uicc_firmware_version); - cJSON_AddStringToObject(jeuiccinfo2, "global_platform_version", euiccinfo2.global_platform_version); - cJSON_AddStringToObject(jeuiccinfo2, "protection_profile_version", euiccinfo2.pp_version); - cJSON_AddStringToObject(jeuiccinfo2, "sas_accreditation_number", euiccinfo2.sas_accreditation_number); + cJSON_AddStringOrNullToObject(jeuiccinfo2, "profile_version", euiccinfo2.profile_version); + cJSON_AddStringOrNullToObject(jeuiccinfo2, "sgp22_version", euiccinfo2.sgp22_version); + cJSON_AddStringOrNullToObject(jeuiccinfo2, "euicc_firmware_version", euiccinfo2.euicc_firmware_version); + cJSON_AddStringOrNullToObject(jeuiccinfo2, "uicc_firmware_version", euiccinfo2.uicc_firmware_version); + cJSON_AddStringOrNullToObject(jeuiccinfo2, "global_platform_version", euiccinfo2.global_platform_version); + cJSON_AddStringOrNullToObject(jeuiccinfo2, "protection_profile_version", euiccinfo2.pp_version); + cJSON_AddStringOrNullToObject(jeuiccinfo2, "sas_accreditation_number", euiccinfo2.sas_accreditation_number); cJSON_AddNumberToObject(jeuiccinfo2, "free_nvram", euiccinfo2.free_nvram); cJSON_AddNumberToObject(jeuiccinfo2, "free_ram", euiccinfo2.free_ram); } diff --git a/src/applet/notification/list.c b/src/applet/notification/list.c index ee0c6e6..7dccc22 100644 --- a/src/applet/notification/list.c +++ b/src/applet/notification/list.c @@ -24,9 +24,9 @@ static int applet_main(int argc, char **argv) jnotification = cJSON_CreateObject(); cJSON_AddNumberToObject(jnotification, "seqNumber", notifications[i].seqNumber); - cJSON_AddStringToObject(jnotification, "profileManagementOperation", notifications[i].profileManagementOperation); - cJSON_AddStringToObject(jnotification, "notificationAddress", notifications[i].notificationAddress); - cJSON_AddStringToObject(jnotification, "iccid", notifications[i].iccid); + cJSON_AddStringOrNullToObject(jnotification, "profileManagementOperation", notifications[i].profileManagementOperation); + cJSON_AddStringOrNullToObject(jnotification, "notificationAddress", notifications[i].notificationAddress); + cJSON_AddStringOrNullToObject(jnotification, "iccid", notifications[i].iccid); cJSON_AddItemToArray(jdata, jnotification); } diff --git a/src/applet/profile/list.c b/src/applet/profile/list.c index 8809e5b..8aa12fe 100644 --- a/src/applet/profile/list.c +++ b/src/applet/profile/list.c @@ -23,15 +23,15 @@ static int applet_main(int argc, char **argv) cJSON *jprofile = NULL; jprofile = cJSON_CreateObject(); - cJSON_AddStringToObject(jprofile, "iccid", profiles[i].iccid); - cJSON_AddStringToObject(jprofile, "isdpAid", profiles[i].isdpAid); - cJSON_AddStringToObject(jprofile, "profileState", profiles[i].profileState); - cJSON_AddStringToObject(jprofile, "profileNickname", profiles[i].profileNickname); - cJSON_AddStringToObject(jprofile, "serviceProviderName", profiles[i].serviceProviderName); - cJSON_AddStringToObject(jprofile, "profileName", profiles[i].profileName); - cJSON_AddStringToObject(jprofile, "iconType", profiles[i].iconType); - cJSON_AddStringToObject(jprofile, "icon", profiles[i].icon); - cJSON_AddStringToObject(jprofile, "profileClass", profiles[i].profileClass); + cJSON_AddStringOrNullToObject(jprofile, "iccid", profiles[i].iccid); + cJSON_AddStringOrNullToObject(jprofile, "isdpAid", profiles[i].isdpAid); + cJSON_AddStringOrNullToObject(jprofile, "profileState", profiles[i].profileState); + cJSON_AddStringOrNullToObject(jprofile, "profileNickname", profiles[i].profileNickname); + cJSON_AddStringOrNullToObject(jprofile, "serviceProviderName", profiles[i].serviceProviderName); + cJSON_AddStringOrNullToObject(jprofile, "profileName", profiles[i].profileName); + cJSON_AddStringOrNullToObject(jprofile, "iconType", profiles[i].iconType); + cJSON_AddStringOrNullToObject(jprofile, "icon", profiles[i].icon); + cJSON_AddStringOrNullToObject(jprofile, "profileClass", profiles[i].profileClass); cJSON_AddItemToArray(jdata, jprofile); } es10c_profile_info_free_all(profiles, profiles_count); diff --git a/src/jprint.c b/src/jprint.c index 795dd3d..a41040f 100644 --- a/src/jprint.c +++ b/src/jprint.c @@ -16,11 +16,11 @@ void jprint_error(const char *function_name, const char *detail) } jroot = cJSON_CreateObject(); - cJSON_AddStringToObject(jroot, "type", "lpa"); + cJSON_AddStringOrNullToObject(jroot, "type", "lpa"); jpayload = cJSON_CreateObject(); cJSON_AddNumberToObject(jpayload, "code", -1); - cJSON_AddStringToObject(jpayload, "message", function_name); - cJSON_AddStringToObject(jpayload, "data", detail); + cJSON_AddStringOrNullToObject(jpayload, "message", function_name); + cJSON_AddStringOrNullToObject(jpayload, "data", detail); cJSON_AddItemToObject(jroot, "payload", jpayload); jstr = cJSON_PrintUnformatted(jroot); @@ -37,10 +37,10 @@ void jprint_progress(const char *function_name) char *jstr = NULL; jroot = cJSON_CreateObject(); - cJSON_AddStringToObject(jroot, "type", "progress"); + cJSON_AddStringOrNullToObject(jroot, "type", "progress"); jpayload = cJSON_CreateObject(); cJSON_AddNumberToObject(jpayload, "code", 0); - cJSON_AddStringToObject(jpayload, "message", function_name); + cJSON_AddStringOrNullToObject(jpayload, "message", function_name); cJSON_AddNullToObject(jpayload, "data"); cJSON_AddItemToObject(jroot, "payload", jpayload); @@ -58,10 +58,10 @@ void jprint_success(cJSON *jdata) char *jstr = NULL; jroot = cJSON_CreateObject(); - cJSON_AddStringToObject(jroot, "type", "lpa"); + cJSON_AddStringOrNullToObject(jroot, "type", "lpa"); jpayload = cJSON_CreateObject(); cJSON_AddNumberToObject(jpayload, "code", 0); - cJSON_AddStringToObject(jpayload, "message", "success"); + cJSON_AddStringOrNullToObject(jpayload, "message", "success"); if (jdata) { cJSON_AddItemToObject(jpayload, "data", jdata); diff --git a/src/jprint.h b/src/jprint.h index 5caae03..e8f5460 100644 --- a/src/jprint.h +++ b/src/jprint.h @@ -1,5 +1,5 @@ #pragma once -#include +#include void jprint_error(const char *function_name, const char *detail); void jprint_progress(const char *function_name);