]> granicus.if.org Git - json-c/commitdiff
json_object: cleanup of *set_string* functions
authorRamiro Polla <ramiro.polla@gmail.com>
Sat, 8 Dec 2018 22:30:19 +0000 (23:30 +0100)
committerRamiro Polla <ramiro.polla@gmail.com>
Thu, 20 Dec 2018 23:30:26 +0000 (00:30 +0100)
This commit also has the side-effect that errno is set on failed calls
to json_object_set_string(_len).

json_object.c

index 9dd9c084bbf055f1cdbb610b7e57d717faf1d870..70a0f26bad27c592ee3870b470e5c39be80e137c 100644 (file)
@@ -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_objectjson_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 (len<LEN_DIRECT_STRING_DATA) {
-               dstbuf=jso->o.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 */