Skip to content

Commit

Permalink
Fix some memleaks
Browse files Browse the repository at this point in the history
  • Loading branch information
ppomes committed Jul 11, 2024
1 parent d9ea8bb commit 31fe5fa
Showing 1 changed file with 34 additions and 13 deletions.
47 changes: 34 additions & 13 deletions main/myanon.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,25 @@ void remove_quote(char *dst, char *src, size_t size)
const char *psrc = src;
char *pdst = dst;
size_t src_len = strlen(src);

memset(dst, 0, size);

/* Check for leading quote */
if (src_len > 0 && psrc[0] == '\'') {
if (src_len > 0 && psrc[0] == '\'')
{
psrc++;
src_len--;
}

/* Check for trailing quote */
if (src_len > 0 && psrc[src_len - 1] == '\'') {
if (src_len > 0 && psrc[src_len - 1] == '\'')
{
src_len--;
}

/* Copy the string without the edge quotes */
while (src_len > 0 && (pdst - dst < size - 1)) {
while (src_len > 0 && (pdst - dst < size - 1))
{
*pdst++ = *psrc++;
src_len--;
}
Expand Down Expand Up @@ -149,19 +152,24 @@ anonymized_res_st anonymize_token(bool quoted, anon_base_st *config, char *token
unsigned long ts_beg, ts_end;
char *worktoken;
int worktokenlen;
bool needfree = false;

if (stats)
{
ts_beg = get_ts_in_ms();
}

if (quoted) {
worktokenlen=tokenlen-2;
worktoken=mymalloc(worktokenlen+1);
remove_quote(worktoken,token,worktokenlen+1);
} else {
worktoken=token;
worktokenlen=tokenlen;
if (quoted)
{
worktokenlen = tokenlen - 2;
worktoken = mymalloc(worktokenlen + 1);
remove_quote(worktoken, token, worktokenlen + 1);
needfree = true;
}
else
{
worktoken = token;
worktokenlen = tokenlen;
}

switch (config->type)
Expand Down Expand Up @@ -238,6 +246,11 @@ anonymized_res_st anonymize_token(bool quoted, anon_base_st *config, char *token
anon_time += (ts_end - ts_beg);
}

if (needfree)
{
free(worktoken);
}

return res_st;
}

Expand All @@ -263,6 +276,7 @@ int main(int argc, char **argv)
int c;
char *fvalue = NULL;
anon_st *cur, *tmp = NULL;
truncate_st *trcur, *trtmp = NULL;
#ifdef HAVE_JQ
anon_json_st *jscur, *jstmp = NULL;
#endif
Expand Down Expand Up @@ -365,9 +379,9 @@ int main(int argc, char **argv)
HASH_ITER(hh, infos, cur, tmp)
{
#ifdef HAVE_JQ
HASH_ITER(hh,infos->json,jscur,jstmp)
HASH_ITER(hh, infos->json, jscur, jstmp)
{
jq_teardown(&(jscur->jq_state));
jq_teardown(&(jscur->jq_state));
HASH_DEL(infos->json, jscur);
free(jscur);
}
Expand All @@ -376,6 +390,13 @@ int main(int argc, char **argv)
free(cur);
}

HASH_ITER(hh, truncate_infos, trcur, trtmp)
{
HASH_DEL(truncate_infos, trcur);
free(trcur);
}


exit(EXIT_SUCCESS);

failure:
Expand Down

0 comments on commit 31fe5fa

Please sign in to comment.