realloc in buf_append

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 2013-02-17 21:44, Kevin Cernekee wrote:
> On Sun, Feb 17, 2013 at 11:32 AM, Mike Miller <mtmiller at ieee.org> wrote:
> [...]
> 
> Good catch - this pattern shows up in a couple of places, e.g.
> 
> body = realloc(body, done + chunklen + 1);
> 
> *lineptr = realloc(*lineptr, *n);
> 
> opt = realloc(opt, sizeof(*opt) + opt->nr_choices * sizeof(*choice));
> 
> I wouldn't normally expect to be able to recover from exhausting the
> heap space anyway, since many other random library calls will start
> breaking.  But for the sake of argument would it make sense to wrap
> realloc() with something like this?
> 

I agree it is an unlikely case and yeah wrapping realloc can be done (I
have seen another upstream do something similar.

> int safe_realloc(void **ptr, size_t size)
> {
> 	void *newptr = realloc(*ptr, size);
> 	if (newptr) {
> 		*ptr = newptr;
> 		return 0;
> 	}
> 	free(*ptr);
Maybe insert:
        *ptr = NULL;

> 	return -ENOMEM;
> }

so a later free of *ptr will not crash the program.

~Niels





[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux