Re: how to avoid a memset() optimization

"Alex Colvin" <alexc@world.std.com>
8 Nov 2002 11:02:08 -0500

          From comp.compilers

Related articles
how to avoid a memset() optimization fwai@rsasecurity.com (Francis Wai) (2002-11-07)
Re: how to avoid a memset() optimization lars@bearnip.com (Lars Duening) (2002-11-08)
Re: how to avoid a memset() optimization alexc@world.std.com (Alex Colvin) (2002-11-08)
Re: how to avoid a memset() optimization fjh@cs.mu.OZ.AU (Fergus Henderson) (2002-11-12)
Re: how to avoid a memset() optimization christian.bau@freeserve.co.uk (Christian Bau) (2002-11-12)
Re: how to avoid a memset() optimization lars@bearnip.com (Lars Duening) (2002-11-12)
Re: how to avoid a memset() optimization cgweav@aol.com (Clayton Weaver) (2002-11-12)
Re: how to avoid a memset() optimization n2102139816.ch@chch.demon.co.uk (Charles Bryant) (2002-11-13)
Re: how to avoid a memset() optimization dobes@dobesland.com (Dobes Vandermeer) (2002-11-13)
[9 later articles]
| List of all articles for this month |

From: "Alex Colvin" <alexc@world.std.com>
Newsgroups: comp.compilers
Date: 8 Nov 2002 11:02:08 -0500
Organization: The World Public Access UNIX, Brookline, MA
References: 02-11-030
Keywords: C
Posted-Date: 08 Nov 2002 11:02:07 EST



"Francis Wai" <fwai@rsasecurity.com> writes:


>{
> char key[16];
> strcpy(key, "whatever");
> encrpts(key);
> memset(key, 0, 16);
>}


>Various suggestions have been made, such as declaring the variable
>volatile and having a scrub memory function in a file of its own. I'm
>wondering if there are better ways such as telling the compiler not to
>optimize away a function call.


>[Declaring the array volatile is the right way to do it. The reason
>volatile exists is to tell the compiler not to do otherwise valid
>optimizations. -John]


I hesitate to contradict the master, but I vote against 'volatile" for
key[]. If you declare key[] volatile, then you have to cast away the
volatility when passing it to strcpy(), encrpts(), and memset(), which
do not deal with volatile strings. In this example, there's no reason
why they should.


You want the compiler to assume a reference to key[] after memset(),
which is what you're assuming when you worry about someone seeing
it. Try declaring key[] static or external instead. That warns the
compiler that you're assuming a lifetime beyond main().


If you absolutely need key[] to be auto, then you've got a problem.
Consider writing your own memset() that accepts a volatile.
--
mac the naïf
[I don't see any reason that casting away the volatile wouldn't work. -John]



Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.