Skip to content

Commit

Permalink
session: Added API function to free session internals
Browse files Browse the repository at this point in the history
This fixes a possible memory leak when working with sessions (especially
with some non-mainline fcgi patches). Quoting from the ticket:

The global variable `sess_fname` is a pointer to dynamically allocated
memory which happens either in `sess_generate_id()` or in
`cgi_session_start()`. However this memory is never freed.

To not expose the variable globally or even just to `cgi.c` where e.g.
`cgi_end()` could access it, a new API function is introduced. We leave
bumping the API version up to a follow up commit.

fixes rafaelsteil#25

Signed-off-by: Alexander Dahl <post@lespocky.de>
  • Loading branch information
LeSpocky committed Jul 12, 2018
1 parent 51a4bc0 commit 1ca33a0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/cgi.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,17 @@ extern void cgi_session_cookie_name(const char *cookie_name);
extern char *cgi_session_var(const char *name);
extern void cgi_session_save_path(const char *path);

/**
* Free all remaining things explicitly or implicitly allocated by a
* session.
*
* @note This is some kind of workaround, you still have to call
* cgi_end(), but maybe not cgi_destroy() … :-/
*
* @see https://github.com/rafaelsteil/libcgi/issues/25
*/
void cgi_session_free( void );

#ifdef __cplusplus
}
#endif
Expand Down
9 changes: 8 additions & 1 deletion src/session.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
* @{
*/

#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
Expand All @@ -76,7 +77,7 @@
FILE *sess_file;

static char sess_id[SESS_ID_LEN + 1];
static char *sess_fname;
static char *sess_fname = NULL;
static unsigned int save_path_len;

char SESSION_SAVE_PATH[255] = "/tmp/";
Expand Down Expand Up @@ -550,6 +551,12 @@ int cgi_session_start()
return 1;
}

void cgi_session_free( void )
{
free( sess_fname );
sess_fname = NULL;
}

/**
* @}
*/

0 comments on commit 1ca33a0

Please sign in to comment.