md_build_ref_def_hashtable: Do not allocate more memory then needed. Fixes #94.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
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;