niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » *nix » BSD » FreeBSD
dirname/basename leaks in FreeBSD 5.x
Post new topic   Reply to topic Page 1 of 1 [2 Posts] View previous topic :: View next topic
Author Message
iralee3c@gmail.com
*nix forums beginner


Joined: 07 Feb 2005
Posts: 1

PostPosted: Mon Feb 07, 2005 2:07 pm    Post subject: dirname/basename leaks in FreeBSD 5.x Reply with quote

Based on the documentation and older 4.x implementations, libc versions
of dirname and basename make use of static buffer as such.

static char bname[MAXPATHLEN];

The man page describes this:

ERRORS
The following error codes may be set in errno:

[ENAMETOOLONG] The path component to be returned was larger
than
MAXPATHLEN.

WARNINGS
The dirname() function returns a pointer to internal static
storage space
that will be overwritten by subsequent calls (each function has
its own
separate storage).

Other vendor implementations of dirname() may modify the contents
of the
string passed to dirname(); this should be taken into account when
writ-
ing code which calls this function if portability is desired.


In FreeBSD 5.x the implementation has changed, but the documentation
has not. This is apparently not serious since subsequent calls to
dirname/basename check for and allocate memory using a static POINTER,
but nevertheless, the memory buffer will linger until exit time and be
reported by memory leak detector tools (such as valgrind) as a leak.

static char *bname = NULL;

if (bname == NULL) {
bname = (char *)malloc(MAXPATHLEN);
if (bname == NULL)
return(NULL);
}

I'm not sure why this change was made in 5.x but do think the
implementation change is at least worth documenting in 5.x man pages
since the functions may return NULL with ENOMEM.

To exit cleanly, applications must call dirname/basename to obtain the
static pointer and then free it.

-Ira
Back to top
jpd
*nix forums Guru


Joined: 22 Feb 2005
Posts: 877

PostPosted: Mon Feb 07, 2005 3:07 pm    Post subject: Re: dirname/basename leaks in FreeBSD 5.x Reply with quote

Begin <1107788858.244817.188970@g14g2000cwa.googlegroups.com>
On 2005-02-07, iralee3c@gmail.com <iralee3c@gmail.com> wrote:
Quote:
I'm not sure why this change was made in 5.x but do think the
implementation change is at least worth documenting in 5.x man pages
since the functions may return NULL with ENOMEM.

Time to file a PR then. Such a report usually works out better in
the database of problems than in a newsgroup where few committers
will see it.


Quote:
To exit cleanly, applications must call dirname/basename to obtain the
static pointer and then free it.

Is it? I think you might be missing some exit code in the library.
However, I'm not all that well versed in the mechanics of dynamic
libraries, so you'll have to look somewhere else for a more precise
explanation. (froup fill-in welcome)


--
j p d (at) d s b (dot) t u d e l f t (dot) n l .
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [2 Posts] View previous topic :: View next topic
The time now is Fri Jan 09, 2009 11:13 am | All times are GMT
navigation Forum index » *nix » BSD » FreeBSD
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Berkeley db XML & FreeBSD mo Berkeley DB 0 Thu Jul 20, 2006 9:15 pm
No new posts Squid 2.6 WCCPv2 and FreeBSD Carlos Filipe Squid 0 Thu Jul 20, 2006 7:48 pm
No new posts freebsd with apache, php and mysql Yves Savoie FreeBSD 5 Wed Jul 19, 2006 11:39 pm
No new posts problems with drac for exim on freebsd Sebastian Inacker Exim 2 Wed Jul 19, 2006 3:36 pm
No new posts Mplayer-gtk-esound 0.99.8 cannot compile on FreeBSD 4.11 Young Coot FreeBSD 8 Tue Jul 18, 2006 7:21 pm

Bad Credit Mortgages | Myspace Layouts | Mobile Phone | Rebuildable cars | Loans
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1318s ][ Queries: 16 (0.0581s) ][ GZIP on - Debug on ]