* Commit: Close memory leaks in chew
@ 2016-08-08 11:19 Nick Clifton
2016-08-08 11:53 ` Andreas Schwab
0 siblings, 1 reply; 3+ messages in thread
From: Nick Clifton @ 2016-08-08 11:19 UTC (permalink / raw)
To: binutils
Hi Guys,
I have been trying to compile the binutils sources with address
sanitization enabled and I have run into memory leaks in the chew
program. So I am checking in the patch below to fix them.
Cheers
Nick
bfd/ChangeLog
2016-08-08 Nick Clifton <nickc@redhat.com>
* doc/chew.c (delete_string): Only free the string buffer if it is
there. Mark the buffer as NULL after freeing.
(drop): Free the dropped string.
(free_words): New function: Frees the memory allocated to the
dictionary.
(add_instrinsic): Duplicate the name string, so that it can be
freed later on.
(compile): Free unused words.
(main): Free the dictionary and top level string buffers at the
end.
diff --git a/bfd/doc/chew.c b/bfd/doc/chew.c
index 92b62cd..71c7e2d 100644
--- a/bfd/doc/chew.c
+++ b/bfd/doc/chew.c
@@ -170,7 +170,9 @@ static void
delete_string (buffer)
string_type *buffer;
{
- free (buffer->ptr);
+ if (buffer->ptr)
+ free (buffer->ptr);
+ buffer->ptr = NULL;
}
static char *
@@ -1088,6 +1090,7 @@ drop ()
{
tos--;
check_range ();
+ delete_string (tos + 1);
pc++;
}
@@ -1244,6 +1247,35 @@ lookup_word (word)
}
static void
+free_words (void)
+{
+ dict_type *ptr = root;
+
+ while (ptr)
+ {
+ dict_type *next;
+
+ if (ptr->word)
+ free (ptr->word);
+ if (ptr->code)
+ {
+ int i;
+ for (i = 0; i < ptr->code_length; i ++)
+ if (ptr->code[i] == push_text
+ && ptr->code[i + 1])
+ {
+ free (ptr->code[i + 1] - 1);
+ ++ i;
+ }
+ free (ptr->code);
+ }
+ next = ptr->next;
+ free (ptr);
+ ptr = next;
+ }
+}
+
+static void
perform ()
{
tos = stack;
@@ -1313,7 +1345,7 @@ add_intrinsic (name, func)
char *name;
void (*func) ();
{
- dict_type *new_d = newentry (name);
+ dict_type *new_d = newentry (strdup (name));
add_to_definition (new_d, func);
add_to_definition (new_d, 0);
}
@@ -1334,24 +1366,27 @@ compile (string)
{
/* Add words to the dictionary. */
char *word;
+
string = nextword (string, &word);
while (string && *string && word[0])
{
if (strcmp (word, "var") == 0)
{
+ free (word);
string = nextword (string, &word);
-
add_var (word);
string = nextword (string, &word);
}
else if (word[0] == ':')
{
dict_type *ptr;
+
/* Compile a word and add to dictionary. */
+ free (word);
string = nextword (string, &word);
-
ptr = newentry (word);
string = nextword (string, &word);
+
while (word[0] != ';')
{
switch (word[0])
@@ -1376,15 +1411,19 @@ compile (string)
function */
add_to_definition (ptr, push_number);
add_to_definition (ptr, (stinst_type) atol (word));
+ free (word);
break;
default:
add_to_definition (ptr, call);
add_to_definition (ptr, (stinst_type) lookup_word (word));
+ free (word);
}
string = nextword (string, &word);
}
add_to_definition (ptr, 0);
+ free (word);
+ word = NULL;
string = nextword (string, &word);
}
else
@@ -1392,6 +1431,8 @@ compile (string)
fprintf (stderr, "syntax error at %s\n", string - 1);
}
}
+ if (word)
+ free (word);
}
static void
@@ -1575,6 +1616,9 @@ main (ac, av)
}
}
write_buffer (stack + 0, stdout);
+ free_words ();
+ delete_string (&pptr);
+ delete_string (&buffer);
if (tos != stack)
{
fprintf (stderr, "finishing with current stack level %ld\n",
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Commit: Close memory leaks in chew
2016-08-08 11:19 Commit: Close memory leaks in chew Nick Clifton
@ 2016-08-08 11:53 ` Andreas Schwab
2016-08-08 14:23 ` Nick Clifton
0 siblings, 1 reply; 3+ messages in thread
From: Andreas Schwab @ 2016-08-08 11:53 UTC (permalink / raw)
To: Nick Clifton; +Cc: binutils
On Mo, Aug 08 2016, Nick Clifton <nickc@redhat.com> wrote:
> diff --git a/bfd/doc/chew.c b/bfd/doc/chew.c
> index 92b62cd..71c7e2d 100644
> --- a/bfd/doc/chew.c
> +++ b/bfd/doc/chew.c
> @@ -170,7 +170,9 @@ static void
> delete_string (buffer)
> string_type *buffer;
> {
> - free (buffer->ptr);
> + if (buffer->ptr)
> + free (buffer->ptr);
free (0) is a no-op.
Andreas.
--
Andreas Schwab, SUSE Labs, schwab@suse.de
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE 1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Commit: Close memory leaks in chew
2016-08-08 11:53 ` Andreas Schwab
@ 2016-08-08 14:23 ` Nick Clifton
0 siblings, 0 replies; 3+ messages in thread
From: Nick Clifton @ 2016-08-08 14:23 UTC (permalink / raw)
To: Andreas Schwab; +Cc: binutils
Hi Andreas,
>> - free (buffer->ptr);
>> + if (buffer->ptr)
>> + free (buffer->ptr);
>
> free (0) is a no-op.
OK - I will remember that for the future. I doubt if it really matters
here, since the performance penalty of the test is trivial.
Cheers
Nick
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2016-08-08 14:23 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-08-08 11:19 Commit: Close memory leaks in chew Nick Clifton
2016-08-08 11:53 ` Andreas Schwab
2016-08-08 14:23 ` Nick Clifton
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).