@@ -225,6 +225,8 @@ bool ini_initialize(ini_context_t *ctx, const char *content, size_t length)
225225 return false;
226226 }
227227
228+ ctx -> content = NULL ;
229+ ctx -> sections = NULL ;
228230 ctx -> content = calloc (1 , length + 1 );
229231
230232 if (!ctx -> content )
@@ -238,6 +240,7 @@ bool ini_initialize(ini_context_t *ctx, const char *content, size_t length)
238240 ini_section_t * currentSection = NULL ;
239241 char line [INI_MAX_LINE_LENGTH ];
240242 const char * ptr = ctx -> content ;
243+ bool has_valid_entries = false;
241244
242245 while (* ptr )
243246 {
@@ -294,6 +297,7 @@ bool ini_initialize(ini_context_t *ctx, const char *content, size_t length)
294297 }
295298
296299 currentSection = newSection ;
300+ has_valid_entries = true;
297301 }
298302 else if (type == INI_LINE_KEY_VALUE && currentSection )
299303 {
@@ -326,6 +330,8 @@ bool ini_initialize(ini_context_t *ctx, const char *content, size_t length)
326330
327331 last -> next = newKv ;
328332 }
333+
334+ has_valid_entries = true;
329335 }
330336
331337 if (* ptr )
@@ -334,6 +340,12 @@ bool ini_initialize(ini_context_t *ctx, const char *content, size_t length)
334340 }
335341 }
336342
343+ if (!has_valid_entries )
344+ {
345+ ini_cleanup (ctx );
346+ return false;
347+ }
348+
337349 return true;
338350}
339351
@@ -344,23 +356,28 @@ void ini_cleanup(ini_context_t *ctx)
344356 return ;
345357 }
346358
347- free (ctx -> content );
359+ if (ctx -> content )
360+ {
361+ free (ctx -> content );
362+ ctx -> content = NULL ;
363+ }
364+
348365 ini_section_t * section = ctx -> sections ;
349366
350367 while (section )
351368 {
352- ini_section_t * nextSection = section -> next ;
369+ ini_section_t * next_section = section -> next ;
353370 ini_keyvalue_t * kv = section -> keyValues ;
354371
355372 while (kv )
356373 {
357- ini_keyvalue_t * nextKv = kv -> next ;
374+ ini_keyvalue_t * next_kv = kv -> next ;
358375 free (kv );
359- kv = nextKv ;
376+ kv = next_kv ;
360377 }
361378
362379 free (section );
363- section = nextSection ;
380+ section = next_section ;
364381 }
365382
366383 ctx -> sections = NULL ;
0 commit comments