anonymous: fix segfault loading config item

unlike other functions called from the config parser code,
anonymous_insert() accesses the global config variable rather than
passing it as an argument. however the global variable is only set
after successful loading of the entire config.

we fix this by adding a conf argument to each anonymous_* function,
passing the global pointer in calls done from outside the config
parser.

fixes #292
This commit is contained in:
rofl0r 2020-03-16 13:19:39 +00:00
parent 2e02dce0c3
commit 3230ce0bc2
5 changed files with 20 additions and 20 deletions

View File

@ -28,21 +28,21 @@
#include "log.h" #include "log.h"
#include "conf.h" #include "conf.h"
short int is_anonymous_enabled (void) short int is_anonymous_enabled (struct config_s *conf)
{ {
return (config->anonymous_map != NULL) ? 1 : 0; return (conf->anonymous_map != NULL) ? 1 : 0;
} }
/* /*
* Search for the header. This function returns a positive value greater than * Search for the header. This function returns a positive value greater than
* zero if the string was found, zero if it wasn't and negative upon error. * zero if the string was found, zero if it wasn't and negative upon error.
*/ */
int anonymous_search (const char *s) int anonymous_search (struct config_s *conf, const char *s)
{ {
assert (s != NULL); assert (s != NULL);
assert (config->anonymous_map != NULL); assert (conf->anonymous_map != NULL);
return hashmap_search (config->anonymous_map, s); return hashmap_search (conf->anonymous_map, s);
} }
/* /*
@ -51,23 +51,23 @@ int anonymous_search (const char *s)
* Return -1 if there is an error, otherwise a 0 is returned if the insert was * Return -1 if there is an error, otherwise a 0 is returned if the insert was
* successful. * successful.
*/ */
int anonymous_insert (const char *s) int anonymous_insert (struct config_s *conf, const char *s)
{ {
char data = 1; char data = 1;
assert (s != NULL); assert (s != NULL);
if (!config->anonymous_map) { if (!conf->anonymous_map) {
config->anonymous_map = hashmap_create (32); conf->anonymous_map = hashmap_create (32);
if (!config->anonymous_map) if (!conf->anonymous_map)
return -1; return -1;
} }
if (hashmap_search (config->anonymous_map, s) > 0) { if (hashmap_search (conf->anonymous_map, s) > 0) {
/* The key was already found, so return a positive number. */ /* The key was already found, so return a positive number. */
return 0; return 0;
} }
/* Insert the new key */ /* Insert the new key */
return hashmap_insert (config->anonymous_map, s, &data, sizeof (data)); return hashmap_insert (conf->anonymous_map, s, &data, sizeof (data));
} }

View File

@ -21,8 +21,8 @@
#ifndef _TINYPROXY_ANONYMOUS_H_ #ifndef _TINYPROXY_ANONYMOUS_H_
#define _TINYPROXY_ANONYMOUS_H_ #define _TINYPROXY_ANONYMOUS_H_
extern short int is_anonymous_enabled (void); extern short int is_anonymous_enabled (struct config_s *conf);
extern int anonymous_search (const char *s); extern int anonymous_search (struct config_s *conf, const char *s);
extern int anonymous_insert (const char *s); extern int anonymous_insert (struct config_s *conf, const char *s);
#endif #endif

View File

@ -624,7 +624,7 @@ static HANDLE_FUNC (handle_anonymous)
if (!arg) if (!arg)
return -1; return -1;
anonymous_insert (arg); anonymous_insert (conf, arg);
safefree (arg); safefree (arg);
return 0; return 0;
} }

View File

@ -325,9 +325,9 @@ main (int argc, char **argv)
* in the list of allowed headers, since it is required in a * in the list of allowed headers, since it is required in a
* HTTP/1.0 request. Also add the Content-Type header since it * HTTP/1.0 request. Also add the Content-Type header since it
* goes hand in hand with Content-Length. */ * goes hand in hand with Content-Length. */
if (is_anonymous_enabled ()) { if (is_anonymous_enabled (config)) {
anonymous_insert ("Content-Length"); anonymous_insert (config, "Content-Length");
anonymous_insert ("Content-Type"); anonymous_insert (config, "Content-Type");
} }
if (daemonized == TRUE) { if (daemonized == TRUE) {

View File

@ -919,8 +919,8 @@ process_client_headers (struct conn_s *connptr, hashmap_t hashofheaders)
hashmap_return_entry (hashofheaders, hashmap_return_entry (hashofheaders,
iter, &data, (void **) &header); iter, &data, (void **) &header);
if (!is_anonymous_enabled () if (!is_anonymous_enabled (config)
|| anonymous_search (data) > 0) { || anonymous_search (config, data) > 0) {
ret = ret =
write_message (connptr->server_fd, write_message (connptr->server_fd,
"%s: %s\r\n", data, header); "%s: %s\r\n", data, header);