@@ -193,18 +193,22 @@ static struct json_object *parse_json(nvme_root_t r, int fd)
193193{
194194 char buf [JSON_FILE_BUF_SIZE ];
195195 struct json_object * obj = NULL ;
196- struct printbuf * pb ;
196+ char * str = NULL ;
197197 json_tokener * tok = NULL ;
198198 int ret ;
199+ void * ptr = NULL ;
200+ int len = 0 ;
201+
202+ while ((ret = read (fd , buf , JSON_FILE_BUF_SIZE )) > 0 ) {
203+ str = realloc (ptr , len + ret );
204+ if (!str )
205+ goto out ;
206+ memcpy (& str [len ], buf , ret );
207+ len += ret ;
208+ ptr = str ;
209+ }
199210
200- pb = printbuf_new ();
201- if (!pb )
202- return NULL ;
203-
204- while ((ret = read (fd , buf , JSON_FILE_BUF_SIZE )) > 0 )
205- printbuf_memappend (pb , buf , ret );
206-
207- if (ret < 0 )
211+ if (ret < 0 || !len )
208212 goto out ;
209213
210214 tok = json_tokener_new_ex (JSON_TOKENER_DEFAULT_DEPTH );
@@ -214,14 +218,14 @@ static struct json_object *parse_json(nvme_root_t r, int fd)
214218 /* Enforce correctly formatted JSON */
215219 tok -> flags = JSON_TOKENER_STRICT ;
216220
217- obj = json_tokener_parse_ex (tok , pb -> buf , printbuf_length ( pb ) );
221+ obj = json_tokener_parse_ex (tok , str , len );
218222 if (!obj )
219223 nvme_msg (r , LOG_DEBUG , "JSON parsing failed: %s\n" ,
220224 json_util_get_last_err ());
221225out :
222226 if (tok )
223227 json_tokener_free (tok );
224- printbuf_free ( pb );
228+ free ( ptr );
225229
226230 return obj ;
227231}
0 commit comments