From: Herwin Weststrate Date: Sat, 12 Nov 2016 17:02:19 +0000 (+0100) Subject: Use stack based char array to generate JSON keys X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bde9c0838db10559cff5d81b5f571f2f150baaa6;p=thirdparty%2Ffreeradius-server.git Use stack based char array to generate JSON keys This saves us from having to allocate memory in a loop. It simplifies the code a little bit too, by removing an if statement that was far away from the code that generated the requirement to call it. --- diff --git a/src/modules/rlm_json/json.c b/src/modules/rlm_json/json.c index 17f24ba1437..95c17b55924 100644 --- a/src/modules/rlm_json/json.c +++ b/src/modules/rlm_json/json.c @@ -333,6 +333,7 @@ const char *fr_json_afrom_pair_list(TALLOC_CTX *ctx, VALUE_PAIR **vps, const cha VALUE_PAIR *vp; struct json_object *obj; const char *p; + char buf[FR_DICT_ATTR_MAX_NAME_LEN + 32]; MEM(obj = json_object_new_object()); @@ -341,10 +342,12 @@ const char *fr_json_afrom_pair_list(TALLOC_CTX *ctx, VALUE_PAIR **vps, const cha fr_dict_enum_t const *dv; struct json_object *vp_object, *values, *value, *type_name; + name_with_prefix = vp->da->name; if (prefix) { - MEM(name_with_prefix = talloc_asprintf(ctx, "%s:%s", prefix, vp->da->name)); - } else { - name_with_prefix = vp->da->name; + int len = snprintf(buf, sizeof(buf), "%s:%s", prefix, vp->da->name); + if (len == (int)strlen(buf)) { + name_with_prefix = buf; + } } /* @@ -370,8 +373,6 @@ const char *fr_json_afrom_pair_list(TALLOC_CTX *ctx, VALUE_PAIR **vps, const cha return NULL; } - if (prefix) talloc_const_free(name_with_prefix); - MEM(value = json_object_from_value_data(ctx, vp->da->type, vp->da, &vp->data)); json_object_array_add(values, value);