Skip to content

Commit 9a881e0

Browse files
author
Keyur Govande
committed
Clean up allocated argv/environ if applicable. This will ensure clean
valgrind runs. (hat-tip to Christopher Jones for the idea)
1 parent 4d2b6f5 commit 9a881e0

File tree

3 files changed

+50
-8
lines changed

3 files changed

+50
-8
lines changed

sapi/cli/php_cli.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,6 +1398,11 @@ int main(int argc, char *argv[])
13981398
tsrm_shutdown();
13991399
#endif
14001400

1401+
/*
1402+
* Do not move this de-initialization. It needs to happen right before
1403+
* exiting.
1404+
*/
1405+
cleanup_ps_args(argv);
14011406
exit(exit_status);
14021407
}
14031408
/* }}} */

sapi/cli/ps_title.c

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,11 @@ static char** save_argv;
122122

123123

124124
/*
125-
* Call this early in startup to save the original argc/argv values.
126-
* If needed, we make a copy of the original argv[] array to preserve it
127-
* from being clobbered by subsequent ps_display actions.
128-
*
129-
* (The original argv[] will not be overwritten by this routine.
130-
* Also, the physical location of the environment strings may be moved,
131-
* so this should be called before any code that might try to hang onto a
132-
* getenv() result.)
125+
* Call this method early, before any code has used the original argv passed in
126+
* from main().
127+
* If needed, this code will make deep copies of argv and environ and return
128+
* these to the caller for further use. The original argv is then 'clobbered'
129+
* to store the process title.
133130
*/
134131
char** save_ps_args(int argc, char** argv)
135132
{
@@ -388,3 +385,41 @@ int get_ps_title(int *displen, const char** string)
388385
return PS_TITLE_SUCCESS;
389386
}
390387

388+
/*
389+
* Clean up the allocated argv and environ if applicable. Only call
390+
* this right before exiting.
391+
* This isn't needed per-se because the OS will clean-up anyway, but
392+
* having and calling this will ensure Valgrind doesn't output 'false
393+
* positives'.
394+
*/
395+
void cleanup_ps_args(char **argv)
396+
{
397+
#ifndef PS_USE_NONE
398+
if (save_argv)
399+
{
400+
save_argv = NULL;
401+
save_argc = 0;
402+
403+
#ifdef PS_USE_CLOBBER_ARGV
404+
{
405+
// clean up environ
406+
int i;
407+
for (i = 0; environ[i] != NULL; i++)
408+
free(environ[i]);
409+
free(environ);
410+
}
411+
#endif /* PS_USE_CLOBBER_ARGV */
412+
413+
#if defined(PS_USE_CHANGE_ARGV) || defined(PS_USE_CLOBBER_ARGV)
414+
{
415+
int i;
416+
for (i=0; argv[i] != NULL; i++)
417+
free(argv[i]);
418+
free(argv);
419+
}
420+
#endif /* PS_USE_CHANGE_ARGV or PS_USE_CLOBBER_ARGV */
421+
}
422+
#endif /* PS_USE_NONE */
423+
424+
return;
425+
}

sapi/cli/ps_title.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,6 @@ extern const char* ps_title_errno(int rc);
3636

3737
extern int is_ps_title_available();
3838

39+
extern void cleanup_ps_args(char **argv);
40+
3941
#endif // PS_TITLE_HEADER

0 commit comments

Comments
 (0)