Unix Technical Forum

Re: How important is it to free() on run once programs?

This is a discussion on Re: How important is it to free() on run once programs? within the mailing.openbsd.tech forums, part of the OpenBSD category; --> On Fri, Aug 26, 2005 at 12:44:37PM +0200, Han Boetes wrote: > Hi, > > I was looking for ...


Go Back   Unix Technical Forum > Unix Operating Systems > OpenBSD > mailing.openbsd.tech

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-18-2008, 08:01 AM
Claudio Jeker
 
Posts: n/a
Default Re: How important is it to free() on run once programs?

On Fri, Aug 26, 2005 at 12:44:37PM +0200, Han Boetes wrote:
> Hi,
>
> I was looking for an example of a cleanup with free() while
> running into a fatal error which causes a direct errx() etc.
>


Don't cleanup on error pathes that exit the program. Exit does that for
you and doing free() in case of errors may help heap overflows.

> Not that I found it, but I found in df a malloc() without a
> free(). Now I wonder how important it is to free() for a oneshot
> program like df.
>


It may make sense for some oneshot programs to clean up allocations during
runtime to keep the memory footprint low. There is no need to cleanup
before any kind of exit (or exec).

> If it is important I'd like to suggest this patch:
>
> Index: df.c
> ================================================== =================
> RCS file: /cvs/src/bin/df/df.c,v
> retrieving revision 1.43
> diff -u -p -r1.43 df.c
> --- df.c 20 Feb 2005 01:34:56 -0000 1.43
> +++ df.c 26 Aug 2005 10:26:43 -0000
> @@ -118,8 +118,10 @@ main(int argc, char *argv[])
> Pflag = 1;
> break;
> case 't':
> - if (typelist != NULL)
> + if (typelist != NULL) {
> + free(typelist);
> errx(1, "only one -t option may be specified.");
> + }
> maketypelist(optarg);
> break;
> default:
> @@ -188,6 +190,9 @@ main(int argc, char *argv[])
> else
> bsdprint(mntbuf, mntsize, maxwidth);
> }
> +
> + if (typelist != NULL)
> + free(typelist);
>
> exit(mntsize ? 0 : 1);
> }
>
>


In both cases some sort of exit is called and so the cleanup is done
automaticaly.

--
:wq Claudio

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 11:32 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com