Fix memory leak with redirect_link.

It should be duplicated by who's going to use it, otherwise the
duplicated string will simply leak. Add function to free it, because why
not.

This also fixes a compiler warning about losing the const qualifier.
This commit is contained in:
Érico Rolim 2020-10-15 22:21:13 -03:00
parent 9096022f18
commit b85aaae164
5 changed files with 26 additions and 13 deletions

View File

@ -1,4 +1,3 @@
#define _XOPEN_SOURCE 500 /* strdup */
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
@ -125,7 +124,7 @@ size_t ssl_to_mmap(struct transmission_information ti)
meta++;
if (ti.header_callback) {
ti.header_callback(first, strdup(meta));
ti.header_callback(first, meta);
}
switch (first) {

3
gemi.c
View File

@ -180,7 +180,7 @@ int main(int argc, char **argv)
// pager must always be closed before exec'ing into self
// if redirect_link exists, should also kill pager
// XXX: add some flag to not kill pager when performing redirection?
const char *redirect_link = get_gemini_redirect_link();
char *redirect_link = get_gemini_redirect_link();
if (pager) wait_for_pager(pager_info, (bool)redirect_link);
if (redirect_link) {
@ -306,5 +306,6 @@ int main(int argc, char **argv)
free(domain);
free(path);
free(port);
free_gemini_redirect_link();
return rv;
}

View File

@ -1,3 +1,4 @@
#define _XOPEN_SOURCE 500 /* strdup */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -112,22 +113,30 @@ struct gemini_link_node *get_gemini_node_by_n(struct gemini_link_node *head, int
static char *redirect_link = NULL;
void store_gemini_redirect_link(int s, char *l)
void store_gemini_redirect_link(int s, const char *l)
{
// only store URL if it is a redirect.
// this comes from a header, so will always end with \r\n
// cut \r
if (s == '3') {
redirect_link = l;
*(strchr(redirect_link, '\r')) = 0;
redirect_link = strdup(l);
// if allocation fails, redirection simply won't be detected
if (redirect_link) {
*(strchr(redirect_link, '\r')) = 0;
}
}
}
const char *get_gemini_redirect_link(void)
char *get_gemini_redirect_link(void)
{
return redirect_link;
}
void free_gemini_redirect_link(void)
{
free(redirect_link);
}
/*
* This function joins two paths and resolves the "." ".." and "//" refs in them.
*

View File

@ -16,8 +16,9 @@ struct gemini_link_node {
int get_links_from_gmi(const char *, struct gemini_link_node **);
void print_gemini_nodes(struct gemini_link_node *, FILE *);
struct gemini_link_node *get_gemini_node_by_n(struct gemini_link_node *, int);
void store_gemini_redirect_link(int, char *);
const char *get_gemini_redirect_link(void);
void store_gemini_redirect_link(int, const char *);
char *get_gemini_redirect_link(void);
void free_gemini_redirect_link(void);
char *walk_gemini_path(const char *, const char *);
#endif // __GEMINI_H_

11
purr.h
View File

@ -29,10 +29,13 @@ HTTP_CONN = 0,
GEMINI_CONN
};
// callback for passing header information:
// int - status
// char * - relevant part of header
typedef void (*header_callback_def)(int, char *);
/*
* Callback for storing header information
* Args:
* int status
* const char *information_to_be_copied
*/
typedef void (*header_callback_def)(int, const char *);
struct connection_information {
br_sslio_context *ioc;