This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [vta, trunk] don't leak uninitialized stack state into output
On Mar 1, 2009, Paul Richard Thomas <paul.richard.thomas@gmail.com> wrote:
> On Sun, Mar 1, 2009 at 11:42 AM, Richard Guenther
> <richard.guenther@gmail.com> wrote:
>> I think zeroing the buffer completely before using it would match
>> general programming style more appropriately
> That's verging on obvious. OK by me.
Unfortunately, the original patch wasn't quite right. It regressed
gfortran.dg/widechar_intrinsics_8.f90, but I didn't realize that before
it hit the branch :-(
I spent some time trying to figure out why, but then I decided to try
Richi's suggestion, and fixed it. I suppose gfc_target_encode_expr may
be returning something other than the number of chars used up in the
buffer.
I'm leaving it at that. Here's the patch I'm installing in the trunk,
followed by the patch for the branch (that combines reverting the broken
patch and applying the trunk patch). In case you'd like to investigate
the failure, applying the branch patch reversed would get you the broken
compiler.
for gcc/ChangeLog
from Alexandre Oliva <aoliva@redhat.com>
* fortran/simplify.c (gfc_simplify_transfer): Zero-initialize
the buffer.
Index: gcc/fortran/simplify.c
===================================================================
--- gcc/fortran/simplify.c.orig 2009-03-01 02:15:58.000000000 -0300
+++ gcc/fortran/simplify.c 2009-03-06 05:27:46.000000000 -0300
@@ -4541,6 +4541,7 @@ gfc_simplify_transfer (gfc_expr *source,
/* Allocate the buffer to store the binary version of the source. */
buffer_size = MAX (source_size, result_size);
buffer = (unsigned char*)alloca (buffer_size);
+ memset (buffer, 0, buffer_size);
/* Now write source to the buffer. */
gfc_target_encode_expr (source, buffer, buffer_size);
for gcc/ChangeLog.vta
from Alexandre Oliva <aoliva@redhat.com>
* fortran/simplify.c (gfc_simplify_transfer): Revert previous
patch. Zero out the entire buffer upfront.
Index: gcc/fortran/simplify.c
===================================================================
--- gcc/fortran/simplify.c.orig 2009-03-01 04:32:14.000000000 -0300
+++ gcc/fortran/simplify.c 2009-03-06 05:24:24.000000000 -0300
@@ -4465,7 +4465,6 @@ gfc_simplify_transfer (gfc_expr *source,
size_t result_size;
size_t result_elt_size;
size_t buffer_size;
- size_t used_size;
mpz_t tmp;
unsigned char *buffer;
@@ -4542,13 +4541,10 @@ gfc_simplify_transfer (gfc_expr *source,
/* Allocate the buffer to store the binary version of the source. */
buffer_size = MAX (source_size, result_size);
buffer = (unsigned char*)alloca (buffer_size);
+ memset (buffer, 0, buffer_size);
/* Now write source to the buffer. */
- used_size = gfc_target_encode_expr (source, buffer, buffer_size);
-
- /* Don't let random stack data leak into the output. */
- if (used_size < buffer_size)
- memset (buffer + used_size, 0, buffer_size - used_size);
+ gfc_target_encode_expr (source, buffer, buffer_size);
/* And read the buffer back into the new expression. */
gfc_target_interpret_expr (buffer, buffer_size, result);
--
Alexandre Oliva http://www.lsd.ic.unicamp.br/~oliva/
You must be the change you wish to see in the world. -- Gandhi
Be Free! -- http://FSFLA.org/ FSF Latin America board member
Free Software Evangelist Red Hat Brazil Compiler Engineer