Commit 728f2af40657a4d15f613f49f1d92214bc1b11dc

Martin Mitas 2019-10-03T20:09:33

md_build_ref_def_hashtable: Do not allocate more memory then needed. Fixes #94.

diff --git a/CHANGELOG.md b/CHANGELOG.md
index fdd3d83..de6bdce 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,6 +11,12 @@ Changes:
    outputs them verbatim.) Thanks for the feature belong to [Tilman Roeder](
    https://github.com/dyedgreen).
 
+Fixes:
+
+ * [#94](https://github.com/mity/md4c/issues/94):
+   `md_build_ref_def_hashtable()`: Do not allocate more memory then strictly
+   needed.
+
 
 ## Version 0.3.4
 
diff --git a/md4c/md4c.c b/md4c/md4c.c
index 170ddcc..f2a24f9 100644
--- a/md4c/md4c.c
+++ b/md4c/md4c.c
@@ -1687,7 +1687,7 @@ md_build_ref_def_hashtable(MD_CTX* ctx)
             }
 
             /* Make the bucket capable of holding more ref. defs. */
-            list = (MD_REF_DEF_LIST*) malloc(sizeof(MD_REF_DEF_LIST) + 4 * sizeof(MD_REF_DEF));
+            list = (MD_REF_DEF_LIST*) malloc(sizeof(MD_REF_DEF_LIST) + 4 * sizeof(MD_REF_DEF*));
             if(list == NULL) {
                 MD_LOG("malloc() failed.");
                 goto abort;
@@ -1704,7 +1704,7 @@ md_build_ref_def_hashtable(MD_CTX* ctx)
         list = (MD_REF_DEF_LIST*) bucket;
         if(list->n_ref_defs >= list->alloc_ref_defs) {
             MD_REF_DEF_LIST* list_tmp = (MD_REF_DEF_LIST*) realloc(list,
-                        sizeof(MD_REF_DEF_LIST) + 2 * list->alloc_ref_defs * sizeof(MD_REF_DEF));
+                        sizeof(MD_REF_DEF_LIST) + 2 * list->alloc_ref_defs * sizeof(MD_REF_DEF*));
             if(list_tmp == NULL) {
                 MD_LOG("realloc() failed.");
                 goto abort;