This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
Re: [PATCH] Fix tracepoint create_tsv_from_upload sprintf crash
On Wed, Dec 7, 2011 at 00:00, Hui Zhu <teawater@gmail.com> wrote:
> On Tue, Dec 6, 2011 at 23:43, Tom Tromey <tromey@redhat.com> wrote:
>>>>>>> ">" == Hui Zhu <teawater@gmail.com> writes:
>>
>>>> 2011-12-06 ?Hui Zhu ?<teawater@gmail.com>
>>>> ? ? ?* tracepoint.c (create_tsv_from_upload): Change sprintf to snprintf.
>>
>> How about using xstrprintf instead?
>> Then the name can be as long as you like.
>>
>> Tom
>
> Cool! I like it. ?I will make a patch according to your mail later.
>
> Thanks for your help.
>
> Best,
> Hui
Hi Tom,
I make a new patch according to your comments.
Please help me review it.
Thanks,
Hui
2011-12-08 Hui Zhu <teawater@gmail.com>
* tracepoint.c (create_tsv_from_upload): Change sprintf to xstrprintf.
---
tracepoint.c | 16 ++++++++++++----
1 file changed, 12 insertions(+), 4 deletions(-)
--- a/tracepoint.c
+++ b/tracepoint.c
@@ -3482,31 +3482,39 @@ struct trace_state_variable *
create_tsv_from_upload (struct uploaded_tsv *utsv)
{
const char *namebase;
- char buf[20];
+ char *buf;
int try_num = 0;
struct trace_state_variable *tsv;
+ struct cleanup *old_chain;
if (utsv->name)
{
namebase = utsv->name;
- sprintf (buf, "%s", namebase);
+ buf = xstrprintf ("%s", namebase);
}
else
{
namebase = "__tsv";
- sprintf (buf, "%s_%d", namebase, try_num++);
+ buf = xstrprintf ("%s_%d", namebase, try_num++);
}
/* Fish for a name that is not in use. */
/* (should check against all internal vars?) */
while (find_trace_state_variable (buf))
- sprintf (buf, "%s_%d", namebase, try_num++);
+ {
+ xfree (buf);
+ buf = xstrprintf ("%s_%d", namebase, try_num++);
+ }
+
+ old_chain = make_cleanup (xfree, buf);
/* We have an available name, create the variable. */
tsv = create_trace_state_variable (buf);
tsv->initial_value = utsv->initial_value;
tsv->builtin = utsv->builtin;
+ do_cleanups (old_chain);
+
return tsv;
}