* [patch]: Fix memory leak of c-exp.y
@ 2008-06-21 17:21 teawater
0 siblings, 0 replies; 6+ messages in thread
From: teawater @ 2008-06-21 17:21 UTC (permalink / raw)
To: gdb-patches
[-- Attachment #1: Type: text/plain, Size: 262 bytes --]
c-exp.y has a memory leak in function parse_number. char *s is malloc
at line 1211.
There are returns at lines 1137, 1147, and 1157 without calling free.
This patch is for the GDB CVS version.
ChangeLog:
* gdb/c-exp.y: Fix memory leak of function parse_number
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: c-exp_leak.patch --]
[-- Type: text/x-diff; name=c-exp_leak.patch, Size: 805 bytes --]
--- a/gdb/c-exp.y
+++ b/gdb/c-exp.y
@@ -1134,6 +1134,7 @@ parse_number (p, len, parsed_float, puti
= builtin_type (current_gdbarch)->builtin_decfloat;
decimal_from_string (putithere->typed_val_decfloat.val, 4, p);
p[len] = saved_char;
+ free (s);
return (DECFLOAT);
}
@@ -1144,6 +1145,7 @@ parse_number (p, len, parsed_float, puti
= builtin_type (current_gdbarch)->builtin_decdouble;
decimal_from_string (putithere->typed_val_decfloat.val, 8, p);
p[len] = saved_char;
+ free (s);
return (DECFLOAT);
}
@@ -1154,6 +1156,7 @@ parse_number (p, len, parsed_float, puti
= builtin_type (current_gdbarch)->builtin_declong;
decimal_from_string (putithere->typed_val_decfloat.val, 16, p);
p[len] = saved_char;
+ free (s);
return (DECFLOAT);
}
^ permalink raw reply [flat|nested] 6+ messages in thread
* [patch]: Fix memory leak of c-exp.y
@ 2008-06-24 12:32 teawater
2008-06-24 14:04 ` Jan Kratochvil
2008-06-25 13:11 ` Joel Brobecker
0 siblings, 2 replies; 6+ messages in thread
From: teawater @ 2008-06-24 12:32 UTC (permalink / raw)
To: gdb-patches
Sorry that the format of this patch is not very well. So I send it again.
c-exp.y has a memory leak in function parse_number. char *s is malloc
at line 1211.
There are returns at lines 1137, 1147, and 1157 without calling free.
This patch is for the GDB CVS version.
ChangeLog:
2008-06-21 Hui Zhu <teawater@gmail.com>
* gdb/c-exp.y: Fix memory leak of function parse_number
--- a/gdb/c-exp.y
+++ b/gdb/c-exp.y
@@ -1134,6 +1134,7 @@ parse_number (p, len, parsed_float, puti
= builtin_type (current_gdbarch)->builtin_decfloat;
decimal_from_string (putithere->typed_val_decfloat.val, 4, p);
p[len] = saved_char;
+ free (s);
return (DECFLOAT);
}
@@ -1144,6 +1145,7 @@ parse_number (p, len, parsed_float, puti
= builtin_type (current_gdbarch)->builtin_decdouble;
decimal_from_string (putithere->typed_val_decfloat.val, 8, p);
p[len] = saved_char;
+ free (s);
return (DECFLOAT);
}
@@ -1154,6 +1156,7 @@ parse_number (p, len, parsed_float, puti
= builtin_type (current_gdbarch)->builtin_declong;
decimal_from_string (putithere->typed_val_decfloat.val, 16, p);
p[len] = saved_char;
+ free (s);
return (DECFLOAT);
}
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [patch]: Fix memory leak of c-exp.y
2008-06-24 12:32 teawater
@ 2008-06-24 14:04 ` Jan Kratochvil
2008-06-25 12:32 ` teawater
2008-06-25 13:11 ` Joel Brobecker
1 sibling, 1 reply; 6+ messages in thread
From: Jan Kratochvil @ 2008-06-24 14:04 UTC (permalink / raw)
To: Hui Zhu; +Cc: Thiago Jung Bauermann, gdb-patches
[-- Attachment #1: Type: text/plain, Size: 556 bytes --]
On Tue, 24 Jun 2008 08:32:44 +0200, teawater wrote:
> ChangeLog:
> 2008-06-21 Hui Zhu <teawater@gmail.com>
> * gdb/c-exp.y: Fix memory leak of function parse_number
Agreed with the fix just IMO the block of code needs more cleanups. [attached]
The leak was brought in by:
http://sourceware.org/ml/gdb-patches/2007-10/msg00395.html
Just please check the GNU Coding Standards document for the ChangeLog style, it
should have been more like:
2008-06-21 Hui Zhu <teawater@gmail.com>
* c-exp.y (parse_number): Fix a memory leak.
Thanks,
Jan
[-- Attachment #2: gdb-c-exp-dfp-leaketc.patch --]
[-- Type: text/plain, Size: 2525 bytes --]
2008-06-24 Jan Kratochvil <jan.kratochvil@redhat.com>
Fix a memory leak found by Hui Zhu <teawater@gmail.com>.
* c-exp.y (parse_number): Move the S and SAVED_CHAR initialization
after the DECFLOAT detection to fix a memory leak. Remove the
redundant NUM initialization. Protect the DECFLOAT detection memory
access before the P block. Restore the P memory content for the
DECFLOAT detection.
--- ./gdb/c-exp.y 9 Jun 2008 19:25:14 -0000 1.45
+++ ./gdb/c-exp.y 24 Jun 2008 13:03:26 -0000
@@ -1118,45 +1118,46 @@ parse_number (p, len, parsed_float, puti
if (parsed_float)
{
/* It's a float since it contains a point or an exponent. */
- char *s = malloc (len);
- int num = 0; /* number of tokens scanned by scanf */
- char saved_char = p[len];
-
- p[len] = 0; /* null-terminate the token */
+ char *s;
+ int num; /* number of tokens scanned by scanf */
+ char saved_char;
/* If it ends at "df", "dd" or "dl", take it as type of decimal floating
point. Return DECFLOAT. */
- if (p[len - 2] == 'd' && p[len - 1] == 'f')
+ if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f')
{
p[len - 2] = '\0';
putithere->typed_val_decfloat.type
= builtin_type (current_gdbarch)->builtin_decfloat;
decimal_from_string (putithere->typed_val_decfloat.val, 4, p);
- p[len] = saved_char;
- return (DECFLOAT);
+ p[len - 2] = 'd';
+ return DECFLOAT;
}
- if (p[len - 2] == 'd' && p[len - 1] == 'd')
+ if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd')
{
p[len - 2] = '\0';
putithere->typed_val_decfloat.type
= builtin_type (current_gdbarch)->builtin_decdouble;
decimal_from_string (putithere->typed_val_decfloat.val, 8, p);
- p[len] = saved_char;
- return (DECFLOAT);
+ p[len - 2] = 'd';
+ return DECFLOAT;
}
- if (p[len - 2] == 'd' && p[len - 1] == 'l')
+ if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l')
{
p[len - 2] = '\0';
putithere->typed_val_decfloat.type
= builtin_type (current_gdbarch)->builtin_declong;
decimal_from_string (putithere->typed_val_decfloat.val, 16, p);
- p[len] = saved_char;
- return (DECFLOAT);
+ p[len - 2] = 'd';
+ return DECFLOAT;
}
+ s = malloc (len);
+ saved_char = p[len];
+ p[len] = 0; /* null-terminate the token */
num = sscanf (p, "%" DOUBLEST_SCAN_FORMAT "%s",
&putithere->typed_val_float.dval, s);
p[len] = saved_char; /* restore the input stream */
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [patch]: Fix memory leak of c-exp.y
2008-06-24 14:04 ` Jan Kratochvil
@ 2008-06-25 12:32 ` teawater
0 siblings, 0 replies; 6+ messages in thread
From: teawater @ 2008-06-25 12:32 UTC (permalink / raw)
To: Jan Kratochvil; +Cc: Thiago Jung Bauermann, gdb-patches
On Tue, Jun 24, 2008 at 21:35, Jan Kratochvil <jan.kratochvil@redhat.com> wrote:
> On Tue, 24 Jun 2008 08:32:44 +0200, teawater wrote:
>> ChangeLog:
>> 2008-06-21 Hui Zhu <teawater@gmail.com>
>> * gdb/c-exp.y: Fix memory leak of function parse_number
>
> Agreed with the fix just IMO the block of code needs more cleanups. [attached]
> The leak was brought in by:
> http://sourceware.org/ml/gdb-patches/2007-10/msg00395.html
>
> Just please check the GNU Coding Standards document for the ChangeLog style, it
> should have been more like:
>
> 2008-06-21 Hui Zhu <teawater@gmail.com>
>
> * c-exp.y (parse_number): Fix a memory leak.
>
OK. Thank you Jan.
Hui
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [patch]: Fix memory leak of c-exp.y
2008-06-24 12:32 teawater
2008-06-24 14:04 ` Jan Kratochvil
@ 2008-06-25 13:11 ` Joel Brobecker
2008-06-25 13:40 ` Daniel Jacobowitz
1 sibling, 1 reply; 6+ messages in thread
From: Joel Brobecker @ 2008-06-25 13:11 UTC (permalink / raw)
To: teawater; +Cc: gdb-patches
Hi teawater,
> 2008-06-21 Hui Zhu <teawater@gmail.com>
> * gdb/c-exp.y: Fix memory leak of function parse_number
Instead of adding calls to free, I looks like this string is actually
completely local, so how about replacing the call to malloc with a
call to alloca, and then get rid of all calls to free instead?
--
Joel
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [patch]: Fix memory leak of c-exp.y
2008-06-25 13:11 ` Joel Brobecker
@ 2008-06-25 13:40 ` Daniel Jacobowitz
0 siblings, 0 replies; 6+ messages in thread
From: Daniel Jacobowitz @ 2008-06-25 13:40 UTC (permalink / raw)
To: Joel Brobecker; +Cc: teawater, gdb-patches, Jan Kratochvil
On Wed, Jun 25, 2008 at 08:43:03AM -0400, Joel Brobecker wrote:
> Hi teawater,
>
> > 2008-06-21 Hui Zhu <teawater@gmail.com>
> > * gdb/c-exp.y: Fix memory leak of function parse_number
>
> Instead of adding calls to free, I looks like this string is actually
> completely local, so how about replacing the call to malloc with a
> call to alloca, and then get rid of all calls to free instead?
Let's go with Jan's version for now - he's fixed a few related
problems in the same function. Jan, that patch is OK.
--
Daniel Jacobowitz
CodeSourcery
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2008-06-25 13:32 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-06-21 17:21 [patch]: Fix memory leak of c-exp.y teawater
2008-06-24 12:32 teawater
2008-06-24 14:04 ` Jan Kratochvil
2008-06-25 12:32 ` teawater
2008-06-25 13:11 ` Joel Brobecker
2008-06-25 13:40 ` Daniel Jacobowitz
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox