Hi,

this patch closes potential memory leaks in the mandoc memory wrapper
functions and follows the examples in the manpages.


diff --git mandoc_aux.c mandoc_aux.c
index 7c23ecfdd01..dbfb83faffc 100644
--- mandoc_aux.c
+++ mandoc_aux.c
@@ -66,27 +66,43 @@ mandoc_malloc(size_t size)
 void *
 mandoc_realloc(void *ptr, size_t size)
 {
-       ptr = realloc(ptr, size);
-       if (ptr == NULL)
+       void    *nptr;
+
+       nptr = realloc(ptr, size);
+       if (nptr == NULL) {
+               free(ptr);
                err((int)MANDOCLEVEL_SYSERR, NULL);
+       }
+
+       ptr = nptr;
        return ptr;
 }
 
 void *
 mandoc_reallocarray(void *ptr, size_t num, size_t size)
 {
-       ptr = reallocarray(ptr, num, size);
-       if (ptr == NULL)
+       void    *nptr;
+
+       nptr = reallocarray(ptr, num, size);
+       if (nptr == NULL) {
+               free(ptr);
                err((int)MANDOCLEVEL_SYSERR, NULL);
+       }
+       ptr = nptr;
        return ptr;
 }
 
 void *
 mandoc_recallocarray(void *ptr, size_t oldnum, size_t num, size_t size)
 {
-       ptr = recallocarray(ptr, oldnum, num, size);
-       if (ptr == NULL)
+       void    *nptr;
+
+       nptr = recallocarray(ptr, oldnum, num, size);
+       if (nptr == NULL) {
+               free(nptr);
                err((int)MANDOCLEVEL_SYSERR, NULL);
+       }
+       ptr = nptr;
        return ptr;
 }
 

Reply via email to