From 440390a9a59b86e066b654f95482161fbc402606 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Tue, 28 Nov 2017 01:54:21 +0100 Subject: [PATCH] extract function cast_away_const_from_string --- cJSON.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/cJSON.c b/cJSON.c index d4d2bed..8ad32e2 100644 --- a/cJSON.c +++ b/cJSON.c @@ -1891,6 +1891,14 @@ CJSON_PUBLIC(void) cJSON_AddItemToObject(cJSON *object, const char *string, cJSO #ifdef __GNUC__ #pragma GCC diagnostic ignored "-Wcast-qual" #endif +/* helper function to cast away const */ +static char* cast_away_const_from_string(const char* string) +{ + return (char*)string; +} +#if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))) + #pragma GCC diagnostic pop +#endif /* Add an item to an object with constant string as key */ CJSON_PUBLIC(void) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item) @@ -1903,13 +1911,10 @@ CJSON_PUBLIC(void) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJ { global_hooks.deallocate(item->string); } - item->string = (char*)string; + item->string = cast_away_const_from_string(string); item->type |= cJSON_StringIsConst; cJSON_AddItemToArray(object, item); } -#if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))) - #pragma GCC diagnostic pop -#endif CJSON_PUBLIC(void) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item) {