# Mem leak in virtual_realpath() in tsrm_virtual_cwd.c

- Thread: https://externals.io/message/3618
- Original archive: https://news-web.php.net/php.internals/3618
- Started by Pete Dishman on 2003-07-28T14:48:20+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #3618 — Pete Dishman — 2003-07-28T14:48:20+00:00

URL: https://externals.io/message/3618#3618

Hi,

I'm currently developing an extension and believe I've found a memory leak
in virtual_realpath().  My extension is calling this function via the macro
VCWD_REALPATH().

It seems this function allocates memory with CWD_STATE_COPY() but then never
frees it. This also seems to be the case in virtual_filepath_ex(), which is
the function below.

I've been testing with PHP v4.1.1 but I've checked 4.3.2 and the function
still has the same problem.

virtual_realpath is currently defined as:

CWD_API char *virtual_realpath(const char *path, char *real_path TSRMLS_DC)
{
 cwd_state new_state;
 int retval;

 CWD_STATE_COPY(&new_state, &CWDG(cwd));
 retval = virtual_file_ex(&new_state, path, NULL);

 if (!retval) {
  int len =
new_state.cwd_length>MAXPATHLEN-1?MAXPATHLEN-1:new_state.cwd_length;
  memcpy(real_path, new_state.cwd, len);
  real_path[len] = '\0';
  return real_path;
 }

 return NULL;
}

Changing this to include calls to CWD_STATE_FREE(&new_state) before both
returns seems to be the correct thing to do and resolves my memory leak.

Most of the other functions in this file use CWD_STATE_COPY along with
CWD_STATE_FREE, is it just an oversight that these two functions
(virtual_realpath and virtual_filepath_ex) don't or is there something more
complex going on?

I wasn't sure whether to submit this as a bug report or just to send a note
here, sorry if I picked the wrong one.  Also I'm working on windows and
don't currently have CVS access or the ability to produce diff's if this is
a definite bug.


Thanks in advance,
Pete Dishman

---

## #3619 — Ilia Alshanetsky — 2003-07-28T17:57:03+00:00

URL: https://externals.io/message/3618#3619 · In reply to: #3618

> Hi,
>
> I'm currently developing an extension and believe I've found a memory leak
> in virtual_realpath().  My extension is calling this function via the macro
> VCWD_REALPATH().

You are quite correct, virtual_realpath does indeed appear to leak. I will 
test the patch you've suggested further and if it does not prove to have any 
side effects commit it.

Ilia

---

## #3624 — Pete Dishman — 2003-07-29T07:43:55+00:00

URL: https://externals.io/message/3618#3624 · In reply to: #3619

Excellent,

If this does get committed, will it make it in to 4.3.3 ?

Thanks,

Pete Dishman

"Ilia Alshanetsky" <ilia@prohost.org> wrote in message
news:200307281357.03571.ilia@prohost.org...
> 
> > […]
leak
> > […]
macro
> > […]
>
> You are quite correct, virtual_realpath does indeed appear to leak. I will
> test the patch you've suggested further and if it does not prove to have
any
> side effects commit it.
>
> Ilia

---

## #3626 — Jani Taskinen — 2003-07-29T11:10:17+00:00

URL: https://externals.io/message/3618#3626 · In reply to: #3624

It's already committed and will be in 4.3.3.
    
    --Jani



>Excellent,
>
>If this does get committed, will it make it in to 4.3.3 ?
>
>Thanks,
>
>Pete Dishman
>
>"Ilia Alshanetsky" <ilia@prohost.org> wrote in message
>news:200307281357.03571.ilia@prohost.org...
> > […]
>leak
> > […]
>macro
> > […]
>any
> > […]
