DS: fix regex memory leaks

The regcomp(3) calls had no matching regfree(3) calls.

Change-Id: I5250d62e6ab22821aff18bcdc336cb485df6868e
This commit is contained in:
David van Moolenbroek 2015-08-24 12:18:40 +02:00
parent 4472b590c7
commit 736b88cf53
2 changed files with 28 additions and 9 deletions

View file

@ -27,6 +27,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <signal.h> #include <signal.h>
#include <assert.h>
#include "proto.h" #include "proto.h"
#endif #endif

View file

@ -26,7 +26,7 @@ static struct data_store *alloc_data_slot(void)
*===========================================================================*/ *===========================================================================*/
static struct subscription *alloc_sub_slot(void) static struct subscription *alloc_sub_slot(void)
{ {
/* Allocate a new subscription slot. */ /* Return a free subscription slot. */
int i; int i;
for (i = 0; i < NR_DS_SUBS; i++) { for (i = 0; i < NR_DS_SUBS; i++) {
@ -37,6 +37,20 @@ static struct subscription *alloc_sub_slot(void)
return NULL; return NULL;
} }
/*===========================================================================*
* free_sub_slot *
*===========================================================================*/
static void free_sub_slot(struct subscription *subp)
{
/* Clean up a previously successfully) allocated subscription slot. */
assert(subp->flags & DSF_IN_USE);
regfree(&subp->regex);
memset(&subp->regex, 0, sizeof(subp->regex));
subp->flags = 0;
}
/*===========================================================================* /*===========================================================================*
* lookup_entry * * lookup_entry *
*===========================================================================*/ *===========================================================================*/
@ -454,15 +468,18 @@ int do_subscribe(message *m_ptr)
return ESRCH; return ESRCH;
/* See if the owner already has an existing subscription. */ /* See if the owner already has an existing subscription. */
if((subp = lookup_sub(owner)) == NULL) { if ((subp = lookup_sub(owner)) != NULL) {
/* The subscription doesn't exist, allocate a new one. */ /* If a subscription exists but we can't overwrite, return error. */
if((subp = alloc_sub_slot()) == NULL) if (!(m_ptr->m_ds_req.flags & DSF_OVERWRITE))
return EAGAIN; return EEXIST;
} else if(!(m_ptr->m_ds_req.flags & DSF_OVERWRITE)) { /* Otherwise just free the old one. */
/* The subscription exists but we can't overwrite, return error. */ free_sub_slot(subp);
return EEXIST;
} }
/* Find a free subscription slot. */
if ((subp = alloc_sub_slot()) == NULL)
return EAGAIN;
/* Copy key name from the caller. Anchor the subscription with "^regexp$" so /* Copy key name from the caller. Anchor the subscription with "^regexp$" so
* substrings don't match. The caller will probably not expect this, * substrings don't match. The caller will probably not expect this,
* and the usual case is for a complete match. * and the usual case is for a complete match.
@ -476,6 +493,7 @@ int do_subscribe(message *m_ptr)
if((e=regcomp(&subp->regex, regex, REG_EXTENDED)) != 0) { if((e=regcomp(&subp->regex, regex, REG_EXTENDED)) != 0) {
regerror(e, &subp->regex, errbuf, sizeof(errbuf)); regerror(e, &subp->regex, errbuf, sizeof(errbuf));
printf("DS: subscribe: regerror: %s\n", errbuf); printf("DS: subscribe: regerror: %s\n", errbuf);
memset(&subp->regex, 0, sizeof(subp->regex));
return EINVAL; return EINVAL;
} }
@ -598,7 +616,7 @@ int do_delete(message *m_ptr)
for (i = 0; i < NR_DS_SUBS; i++) { for (i = 0; i < NR_DS_SUBS; i++) {
if ((ds_subs[i].flags & DSF_IN_USE) if ((ds_subs[i].flags & DSF_IN_USE)
&& !strcmp(ds_subs[i].owner, label)) { && !strcmp(ds_subs[i].owner, label)) {
ds_subs[i].flags = 0; free_sub_slot(&ds_subs[i]);
} }
} }