[hackers] [libgrapheme] Fix memory leaks during header-generation || Laslo Hunhold

From: <git_AT_suckless.org>
Date: Tue, 14 Dec 2021 16:00:45 +0100 (CET)

commit a25a1e7894ccb4f13b1d4f07d65ab172b09df478
Author: Laslo Hunhold <dev_AT_frign.de>
AuthorDate: Tue Dec 14 15:59:25 2021 +0100
Commit: Laslo Hunhold <dev_AT_frign.de>
CommitDate: Tue Dec 14 15:59:25 2021 +0100

    Fix memory leaks during header-generation
    
    For extra-correctness, also clean up memory before exiting. This
    only affects the generation phase, as libgrapheme itself has no memory
    allocations.
    
    Signed-off-by: Laslo Hunhold <dev_AT_frign.de>

diff --git a/gen/grapheme-test.c b/gen/grapheme-test.c
index e05dae6..174d01b 100644
--- a/gen/grapheme-test.c
+++ b/gen/grapheme-test.c
_AT_@ -13,6 +13,7 @@ main(int argc, char *argv[])
 
         segment_test_list_parse("data/GraphemeBreakTest.txt", &st, &numsegtests);
         segment_test_list_print(st, numsegtests, "grapheme_test", argv[0]);
+ segment_test_list_free(st, numsegtests);
 
         return 0;
 }
diff --git a/gen/grapheme.c b/gen/grapheme.c
index 232a156..1b01125 100644
--- a/gen/grapheme.c
+++ b/gen/grapheme.c
_AT_@ -87,6 +87,7 @@ main(int argc, char *argv[])
         property_list_parse(segment_property, LEN(segment_property));
         property_list_print(segment_property, LEN(segment_property),
                             "grapheme_prop", argv[0]);
+ property_list_free(segment_property, LEN(segment_property));
 
         return 0;
 }
diff --git a/gen/util.c b/gen/util.c
index 9bd9985..52de6f2 100644
--- a/gen/util.c
+++ b/gen/util.c
_AT_@ -182,6 +182,9 @@ parse_file_with_callback(char *fname, int (*callback)(char *, char **, size_t, c
                         exit(1);
                 }
         }
+
+ free(line);
+ free(field);
 }
 
 static int
_AT_@ -261,6 +264,18 @@ property_list_print(const struct property *prop, size_t numprops,
         printf("};\n");
 }
 
+void
+property_list_free(struct property *prop, size_t numprops)
+{
+ size_t i;
+
+ for (i = 0; i < numprops; i++) {
+ free(prop[i].table);
+ prop[i].table = NULL;
+ prop[i].tablelen = 0;
+ }
+}
+
 static int
 segment_test_callback(char *fname, char **field, size_t nfields, char *comment, void *payload)
 {
_AT_@ -349,7 +364,7 @@ segment_test_list_parse(char *fname, struct segment_test **st, size_t *numsegtes
 }
 
 void
-segment_test_list_print(struct segment_test *st, size_t numsegtests,
+segment_test_list_print(const struct segment_test *st, size_t numsegtests,
                         const char *identifier, const char *progname)
 {
         size_t i, j;
_AT_@ -390,4 +405,10 @@ segment_test_list_print(struct segment_test *st, size_t numsegtests,
         printf("};\n");
 }
 
+void
+segment_test_list_free(struct segment_test *st, size_t numsegtests)
+{
+ (void)numsegtests;
 
+ free(st);
+}
diff --git a/gen/util.h b/gen/util.h
index e269888..85b7569 100644
--- a/gen/util.h
+++ b/gen/util.h
_AT_@ -30,8 +30,10 @@ struct segment_test {
 
 void property_list_parse(struct property *, size_t);
 void property_list_print(const struct property *, size_t, const char *, const char *);
+void property_list_free(struct property *, size_t);
 
 void segment_test_list_parse(char *, struct segment_test **, size_t *);
-void segment_test_list_print(struct segment_test *, size_t, const char *, const char *);
+void segment_test_list_print(const struct segment_test *, size_t, const char *, const char *);
+void segment_test_list_free(struct segment_test *, size_t);
 
 #endif /* UTIL_H */
Received on Tue Dec 14 2021 - 16:00:45 CET

This archive was generated by hypermail 2.3.0 : Tue Dec 14 2021 - 16:12:31 CET