1414
1515#include <json.h>
1616
17+ #include "cleanup.h"
1718#include "fabrics.h"
1819#include "log.h"
1920#include "private.h"
@@ -189,31 +190,34 @@ static void json_parse_host(nvme_root_t r, struct json_object *host_obj)
189190 }
190191}
191192
193+ static DEFINE_CLEANUP_FUNC (cleanup_tokener , json_tokener * , json_tokener_free )
194+ #define _cleanup_tokener_ __cleanup__(cleanup_tokener)
195+
192196static struct json_object * parse_json (nvme_root_t r , int fd )
193197{
194198 char buf [JSON_FILE_BUF_SIZE ];
195- struct json_object * obj = NULL ;
199+ struct json_object * obj ;
196200 char * str = NULL ;
197- json_tokener * tok = NULL ;
201+ _cleanup_tokener_ json_tokener * tok = NULL ;
198202 int ret ;
199- void * ptr = NULL ;
203+ _cleanup_free_ void * ptr = NULL ;
200204 int len = 0 ;
201205
202206 while ((ret = read (fd , buf , JSON_FILE_BUF_SIZE )) > 0 ) {
203207 str = realloc (ptr , len + ret );
204208 if (!str )
205- goto out ;
209+ return NULL ;
206210 memcpy (& str [len ], buf , ret );
207211 len += ret ;
208212 ptr = str ;
209213 }
210214
211215 if (ret < 0 || !len )
212- goto out ;
216+ return NULL ;
213217
214218 tok = json_tokener_new_ex (JSON_TOKENER_DEFAULT_DEPTH );
215219 if (!tok )
216- goto out ;
220+ return NULL ;
217221
218222 /* Enforce correctly formatted JSON */
219223 tok -> flags = JSON_TOKENER_STRICT ;
@@ -222,10 +226,6 @@ static struct json_object *parse_json(nvme_root_t r, int fd)
222226 if (!obj )
223227 nvme_msg (r , LOG_DEBUG , "JSON parsing failed: %s\n" ,
224228 json_util_get_last_err ());
225- out :
226- if (tok )
227- json_tokener_free (tok );
228- free (ptr );
229229
230230 return obj ;
231231}
@@ -335,21 +335,21 @@ static void json_update_port(struct json_object *ctrl_array, nvme_ctrl_t c)
335335 * Store the keyring description in the JSON config file.
336336 */
337337 if (cfg -> keyring ) {
338- char * desc = nvme_describe_key_serial (cfg -> keyring );
338+ _cleanup_free_ char * desc =
339+ nvme_describe_key_serial (cfg -> keyring );
339340
340341 if (desc ) {
341342 json_object_object_add (port_obj , "keyring" ,
342343 json_object_new_string (desc ));
343- free (desc );
344344 }
345345 }
346346 if (cfg -> tls_key ) {
347- char * desc = nvme_describe_key_serial (cfg -> tls_key );
347+ _cleanup_free_ char * desc =
348+ nvme_describe_key_serial (cfg -> tls_key );
348349
349350 if (desc ) {
350351 json_object_object_add (port_obj , "tls_key" ,
351352 json_object_new_string (desc ));
352- free (desc );
353353 }
354354 }
355355
0 commit comments