|
|
|
|
|
|
| Author |
Message |
iralee3c@gmail.com *nix forums beginner
Joined: 07 Feb 2005
Posts: 1
|
Posted: Mon Feb 07, 2005 2:07 pm Post subject:
dirname/basename leaks in FreeBSD 5.x
|
|
|
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
|
Posted: Mon Feb 07, 2005 3:07 pm Post subject:
Re: dirname/basename leaks in FreeBSD 5.x
|
|
|
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 |
|
 |
|
|
The time now is Fri Jan 09, 2009 11:13 am | All times are GMT
|
|
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
|
|