From: Ramiro Polla Date: Sat, 8 Dec 2018 22:30:19 +0000 (+0100) Subject: json_object: cleanup of *set_string* functions X-Git-Tag: json-c-0.16-20220414~40^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=38a112380b9ff49a34335664cef8ffdb52be7d85;p=json-c json_object: cleanup of *set_string* functions This commit also has the side-effect that errno is set on failed calls to json_object_set_string(_len). --- diff --git a/json_object.c b/json_object.c index 9dd9c08..70a0f26 100644 --- a/json_object.c +++ b/json_object.c @@ -1029,49 +1029,45 @@ static void json_object_string_delete(struct json_object* jso) json_object_generic_delete(jso); } -struct json_object* json_object_new_string(const char *s) +static int set_string_len(struct json_object *jso, const char *s, int len) { - struct json_object *jso = json_object_new(json_type_string); - if (!jso) - return NULL; - jso->_to_json_string = &json_object_string_to_json_string; - jso->o.c_string.len = strlen(s); - if(jso->o.c_string.len < LEN_DIRECT_STRING_DATA) { - memcpy(jso->o.c_string.str.data, s, jso->o.c_string.len + 1); - } else { - jso->o.c_string.str.ptr = strdup(s); - if (!jso->o.c_string.str.ptr) + char *dstbuf = NULL; + if (len < LEN_DIRECT_STRING_DATA) + { + dstbuf = jso->o.c_string.str.data; + } + else + { + dstbuf = (char *) malloc(len + 1); + if (dstbuf == NULL) { - json_object_generic_delete(jso); errno = ENOMEM; - return NULL; + return 0; } + jso->o.c_string.str.ptr = dstbuf; } - return jso; + memcpy(dstbuf, (const void *)s, len); + dstbuf[len] = '\0'; + jso->o.c_string.len = len; + return 1; +} + +struct json_object *json_object_new_string(const char *s) +{ + return json_object_new_string_len(s, (int)strlen(s)); } -struct json_object* json_object_new_string_len(const char *s, const int len) +struct json_object *json_object_new_string_len(const char *s, const int len) { - char *dstbuf; struct json_object *jso = json_object_new(json_type_string); if (!jso) return NULL; jso->_to_json_string = &json_object_string_to_json_string; - if(len < LEN_DIRECT_STRING_DATA) { - dstbuf = jso->o.c_string.str.data; - } else { - jso->o.c_string.str.ptr = (char*)malloc(len + 1); - if (!jso->o.c_string.str.ptr) - { - json_object_generic_delete(jso); - errno = ENOMEM; - return NULL; - } - dstbuf = jso->o.c_string.str.ptr; + if (set_string_len(jso, s, len) == 0) + { + json_object_generic_delete(jso); + return NULL; } - memcpy(dstbuf, (const void *)s, len); - dstbuf[len] = '\0'; - jso->o.c_string.len = len; return jso; } @@ -1101,26 +1097,23 @@ int json_object_get_string_len(const struct json_object *jso) } } -int json_object_set_string(json_object* jso, const char* s) { +int json_object_set_string(json_object *jso, const char *s) +{ return json_object_set_string_len(jso, s, (int)(strlen(s))); } -int json_object_set_string_len(json_object* jso, const char* s, int len){ - char *dstbuf; - if (jso==NULL || jso->o_type!=json_type_string) return 0; - if (leno.c_string.str.data; - if (jso->o.c_string.len>=LEN_DIRECT_STRING_DATA) free(jso->o.c_string.str.ptr); - } else { - dstbuf=(char *)malloc(len+1); - if (dstbuf==NULL) return 0; - if (jso->o.c_string.len>=LEN_DIRECT_STRING_DATA) free(jso->o.c_string.str.ptr); - jso->o.c_string.str.ptr=dstbuf; - } - jso->o.c_string.len=len; - memcpy(dstbuf, (const void *)s, len); - dstbuf[len] = '\0'; - return 1; +int json_object_set_string_len(json_object *jso, const char *s, int len) +{ + char *old_ptr = NULL; + int ret; + if (jso == NULL || jso->o_type != json_type_string) + return 0; + if (jso->o.c_string.len >= LEN_DIRECT_STRING_DATA) + old_ptr = jso->o.c_string.str.ptr; + ret = set_string_len(jso, s, len); + if (ret != 0 && old_ptr != NULL) + free(old_ptr); + return ret; } /* json_object_array */