Skip to content

Commit b725f31

Browse files
ikegami-tigaw
authored andcommitted
nvme-print-json: use _cleanup_free_
This is to delete the free function call. Signed-off-by: Tokunori Ikegami <ikegami.t@gmail.com>
1 parent c5af2d2 commit b725f31

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

nvme-print-json.c

+12-15
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,9 @@ static void obj_add_result(struct json_object *o, const char *v, ...)
132132
{
133133
va_list ap;
134134

135+
_cleanup_free_ char *value = NULL;
136+
135137
va_start(ap, v);
136-
char *value;
137138

138139
if (vasprintf(&value, v, ap) < 0)
139140
value = NULL;
@@ -144,15 +145,15 @@ static void obj_add_result(struct json_object *o, const char *v, ...)
144145
obj_add_str(o, "Result", "Could not allocate string");
145146

146147
va_end(ap);
147-
free(value);
148148
}
149149

150150
static void obj_add_key(struct json_object *o, const char *k, const char *v, ...)
151151
{
152152
va_list ap;
153153

154+
_cleanup_free_ char *value = NULL;
155+
154156
va_start(ap, v);
155-
char *value;
156157

157158
if (vasprintf(&value, v, ap) < 0)
158159
value = NULL;
@@ -163,7 +164,6 @@ static void obj_add_key(struct json_object *o, const char *k, const char *v, ...
163164
obj_add_str(o, k, "Could not allocate string");
164165

165166
va_end(ap);
166-
free(value);
167167
}
168168

169169
static struct json_object *obj_create_array_obj(struct json_object *o, const char *k)
@@ -2085,10 +2085,11 @@ static void json_phy_rx_eom_descs(struct nvme_phy_rx_eom_log *log,
20852085

20862086
static void json_phy_rx_eom_log(struct nvme_phy_rx_eom_log *log, __u16 controller)
20872087
{
2088-
char **allocated_eyes = NULL;
20892088
int i;
20902089
struct json_object *r = json_create_object();
20912090

2091+
_cleanup_free_ char **allocated_eyes = NULL;
2092+
20922093
obj_add_uint(r, "lid", log->lid);
20932094
obj_add_uint(r, "eomip", log->eomip);
20942095
obj_add_uint(r, "hsize", le16_to_cpu(log->hsize));
@@ -2121,7 +2122,6 @@ static void json_phy_rx_eom_log(struct nvme_phy_rx_eom_log *log, __u16 controlle
21212122
if (allocated_eyes[i])
21222123
free(allocated_eyes[i]);
21232124
}
2124-
free(allocated_eyes);
21252125
}
21262126

21272127
json_print(r);
@@ -4613,18 +4613,17 @@ static void json_output_error_status(int status, const char *msg, va_list ap)
46134613
{
46144614
struct json_object *r;
46154615
char json_str[STR_LEN];
4616-
char *value;
46174616
int val;
46184617
int type;
46194618

4619+
_cleanup_free_ char *value = NULL;
4620+
46204621
if (vasprintf(&value, msg, ap) < 0)
46214622
value = NULL;
46224623

46234624
sprintf(json_str, "Error: %s", value ? value : "Could not allocate string");
46244625
r = obj_create(json_str);
46254626

4626-
free(value);
4627-
46284627
if (status < 0) {
46294628
obj_add_str(r, "error", nvme_strerror(errno));
46304629
obj_print(r);
@@ -4656,22 +4655,22 @@ static void json_output_error_status(int status, const char *msg, va_list ap)
46564655
static void json_output_message(bool error, const char *msg, va_list ap)
46574656
{
46584657
struct json_object *r = json_r ? json_r : json_create_object();
4659-
char *value;
4658+
4659+
_cleanup_free_ char *value = NULL;
46604660

46614661
if (vasprintf(&value, msg, ap) < 0)
46624662
value = NULL;
46634663

46644664
obj_add_str(r, error ? "error" : "result", value ? value : "Could not allocate string");
46654665

4666-
free(value);
4667-
46684666
obj_print(r);
46694667
}
46704668

46714669
static void json_output_perror(const char *msg)
46724670
{
46734671
struct json_object *r = json_create_object();
4674-
char *error;
4672+
4673+
_cleanup_free_ char *error = NULL;
46754674

46764675
if (asprintf(&error, "%s: %s", msg, strerror(errno)) < 0)
46774676
error = NULL;
@@ -4682,8 +4681,6 @@ static void json_output_perror(const char *msg)
46824681
obj_add_str(r, "error", "Could not allocate string");
46834682

46844683
json_output_object(r);
4685-
4686-
free(error);
46874684
}
46884685

46894686
void json_show_init(void)

0 commit comments

Comments
 (0)