mandoc potential memory leak fix

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

mandoc potential memory leak fix

jes@posteo.de
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 | Threaded
Open this post in threaded view
|

Re: mandoc potential memory leak fix

Florian Obser-2
On Mon, Jun 18, 2018 at 04:37:32PM +0200, Jan Schreiber wrote:
> Hi,
>
> this patch closes potential memory leaks in the mandoc memory wrapper
> functions and follows the examples in the manpages.
>

These are not leaks since mandoc exits via err(3) immediately after an
allocation failure. Which is the whole point of these wrappers I
imagine.

>
> 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;
>  }
>  
>

--
I'm not entirely sure you are real.

Reply | Threaded
Open this post in threaded view
|

Re: mandoc potential memory leak fix

Ingo Schwarze
Hi,

Florian Obser wrote on Mon, Jun 18, 2018 at 06:54:49PM +0200:
> On Mon, Jun 18, 2018 at 04:37:32PM +0200, Jan Schreiber wrote:

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

> These are not leaks since mandoc exits via err(3) immediately after an
> allocation failure. Which is the whole point of these wrappers I
> imagine.

Exactly, so please don't commit these changes, they make the code
less readable for no benefit.

Where these functions are used, the calling code typically already
holds hundreds of pointers to allocated memory for the syntax tree
that is in the process of being built.  These functions save us from
having to call functions that recursively free the syntax tree
from each and every place where memory allocation can fail.

Yours,
  Ingo