473,378 Members | 1,370 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

memset + free

Consider the following program:

#include <stdlib.h>
#include <string.h>

int main( void )
{
void *p = malloc( 4 );
if ( p )
{
strcpy( p, "SEC" );
free( memset( p, 0, 4 ) );
}
return 0;
}

Are the characters in the memory block pointed to by p (assuming the memory
allocation succeeded) guaranteed to be set to 0, just before the memory is
being deallocated; or is a compiler allowed to 'optimize away' the call to
memset?
Jun 10 '07 #1
18 11943
dykeinthebox wrote:
Consider the following program:

#include <stdlib.h>
#include <string.h>

int main( void )
{
void *p = malloc( 4 );
if ( p )
{
strcpy( p, "SEC" );
free( memset( p, 0, 4 ) );
}
return 0;
}

Are the characters in the memory block pointed to by p (assuming the memory
allocation succeeded) guaranteed to be set to 0, just before the memory is
being deallocated; or is a compiler allowed to 'optimize away' the call to
memset?
Since a conforming program cannot tell the difference,
the "as if rule" holds and the compiler is permitted to
delete the memset() call. For that matter, it is allowed
to delete the strcpy(), the if, and the malloc(): none of
them have any influence on the program's observable behavior,
so all of them can be optimized away.

What are you trying to do? Ensure that a password or
similar secret item doesn't show up in a core dump?

--
Eric Sosman
es*****@acm-dot-org.invalid
Jun 10 '07 #2
In article <f4**********@news6.zwoll1.ov.home.nl>,
dykeinthebox <dy**********@hotrmail.comwrote:
>Consider the following program:

#include <stdlib.h>
#include <string.h>

int main( void )
{
void *p = malloc( 4 );
if ( p )
{
strcpy( p, "SEC" );
free( memset( p, 0, 4 ) );
}
return 0;
}

Are the characters in the memory block pointed to by p (assuming the memory
allocation succeeded) guaranteed to be set to 0, just before the memory is
being deallocated; or is a compiler allowed to 'optimize away' the call to
memset?
I don't believe there's any way that a strictly conforming program can
tell the difference, so the as-if rule allows the compiler to optimize
it away.

What are you Really Trying To Do? (If it's what I think it is, the OS
should be doing it for you.)
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca

Surprise your compiler. Write better code than it asks you to.
--Keith Thompson in comp.lang.c
Jun 10 '07 #3
dykeinthebox wrote, On 10/06/07 21:39:
Consider the following program:

#include <stdlib.h>
#include <string.h>

int main( void )
{
void *p = malloc( 4 );
if ( p )
{
strcpy( p, "SEC" );
free( memset( p, 0, 4 ) );
}
return 0;
}

Are the characters in the memory block pointed to by p (assuming the memory
allocation succeeded) guaranteed to be set to 0, just before the memory is
being deallocated; or is a compiler allowed to 'optimize away' the call to
memset?
Since there is no way for a conforming program to tell the difference by
the "as-if" rule it is allowed to optimise out the memset.

If you are looking at this from a security perspective (i.e. you want to
guarantee that someone can't examine memory to get passwords) they I
suggest asking in a security group and/or a group dedicated to your
platform since C does not help you very much.
--
Flash Gordon
Jun 10 '07 #4
On Jun 10, 5:10 pm, dj3va...@csclub.uwaterloo.ca (Dave Vandervies)
wrote:
What are you Really Trying To Do? (If it's what I think it is, the OS
should be doing it for you.)
Why? AFAIK the libc should simply put the memory block back on the
free chain(s).

Regards,
Frodo B

Jun 10 '07 #5
On Jun 10, 4:39 pm, "dykeinthebox" <dykeinthe...@hotrmail.comwrote:
allocation succeeded) guaranteed to be set to 0, just before the memory is
being deallocated; or is a compiler allowed to 'optimize away' the call to
memset?
If it is important for memset() to be called, would it improve matters
if the pointer was volatile?
Of course, optimization flags are OT here.

Regards,
Frodo B

Jun 11 '07 #6
Frodo Baggins <fr*********@gmail.comwrites:
On Jun 10, 4:39 pm, "dykeinthebox" <dykeinthe...@hotrmail.comwrote:
>allocation succeeded) guaranteed to be set to 0, just before the memory is
being deallocated; or is a compiler allowed to 'optimize away' the call to
memset?

If it is important for memset() to be called, would it improve matters
if the pointer was volatile?
Of course, optimization flags are OT here.
Probably not, but it might help if the data the pointer points to is
volatile.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 11 '07 #7
In article <f4**********@news6.zwoll1.ov.home.nl>,
dykeinthebox <dy**********@hotrmail.comwrote:
strcpy( p, "SEC" );
free( memset( p, 0, 4 ) );
If the "SEC" here indicates that you're worried about security, you
need to describe just what you're trying to achieve. On a
general-purpose operating system, no other program should be able to
see the data: it will be zeroed before it's allocated to another
program. A user with normal privileges should not be able to
look at it, unless they're running the program.

In theory using volatile might force the write to occur, but in
practice I doubt any compiler does the optimisation anyway - it's hard
to imagine a situation where it would be worthwhile.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Jun 11 '07 #8
dykeinthebox wrote:
>
#include <stdlib.h>
#include <string.h>

int main( void ) {
void *p = malloc( 4 );
if ( p ) {
strcpy( p, "SEC" );
free( memset( p, 0, 4 ) );
}
return 0;
}

Are the characters in the memory block pointed to by p (assuming
the memory allocation succeeded) guaranteed to be set to 0, just
before the memory is being deallocated; or is a compiler allowed
to 'optimize away' the call to memset?
Since, after the free call, nothing can tell the difference, it can
optimize it out.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 11 '07 #9
On Sun, 10 Jun 2007 23:57:37 -0000, Frodo Baggins
<fr*********@gmail.comwrote in comp.lang.c:
On Jun 10, 4:39 pm, "dykeinthebox" <dykeinthe...@hotrmail.comwrote:
allocation succeeded) guaranteed to be set to 0, just before the memory is
being deallocated; or is a compiler allowed to 'optimize away' the call to
memset?

If it is important for memset() to be called, would it improve matters
if the pointer was volatile?
Of course, optimization flags are OT here.
You can't pass a volatile pointer to memset(). memset() only accepts
a non-cv qualified void pointer. To pass a volatile pointer, you must
use a cast to remove the volatile qualifier, and the result is
undefined behavior.

So there is still no reason that the implementation can't optimize
away the call.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Jun 11 '07 #10
Jack Klein <ja*******@spamcop.netwrites:
On Sun, 10 Jun 2007 23:57:37 -0000, Frodo Baggins
<fr*********@gmail.comwrote in comp.lang.c:
>On Jun 10, 4:39 pm, "dykeinthebox" <dykeinthe...@hotrmail.comwrote:
allocation succeeded) guaranteed to be set to 0, just before the memory is
being deallocated; or is a compiler allowed to 'optimize away' the call to
memset?

If it is important for memset() to be called, would it improve matters
if the pointer was volatile?
Of course, optimization flags are OT here.

You can't pass a volatile pointer to memset(). memset() only accepts
a non-cv qualified void pointer. To pass a volatile pointer, you must
use a cast to remove the volatile qualifier, and the result is
undefined behavior.

So there is still no reason that the implementation can't optimize
away the call.
If necessary (and I'm not convinced that it is), you could write your
own memset equivalent that takes a pointer to volatile whatever
argument.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 11 '07 #11
On Sun, 10 Jun 2007 20:11:33 -0700, Keith Thompson <ks***@mib.org>
wrote in comp.lang.c:
Jack Klein <ja*******@spamcop.netwrites:
On Sun, 10 Jun 2007 23:57:37 -0000, Frodo Baggins
<fr*********@gmail.comwrote in comp.lang.c:
On Jun 10, 4:39 pm, "dykeinthebox" <dykeinthe...@hotrmail.comwrote:
allocation succeeded) guaranteed to be set to 0, just before the memory is
being deallocated; or is a compiler allowed to 'optimize away' the call to
memset?

If it is important for memset() to be called, would it improve matters
if the pointer was volatile?
Of course, optimization flags are OT here.
You can't pass a volatile pointer to memset(). memset() only accepts
a non-cv qualified void pointer. To pass a volatile pointer, you must
use a cast to remove the volatile qualifier, and the result is
undefined behavior.

So there is still no reason that the implementation can't optimize
away the call.

If necessary (and I'm not convinced that it is), you could write your
own memset equivalent that takes a pointer to volatile whatever
argument.
The standard sayeth (6.7.3 P5):

"If an attempt is made to modify an object defined with a
const-qualified type through use of an lvalue with non-const-qualified
type, the behavior is undefined. If an attempt is made to refer to an
object defined with a volatile-qualified type through use of an lvalue
with non-volatile-qualified type, the behavior is undefined."

....but I think you are right and I am incorrect, in this particular
case.

Since the memory was obtained from malloc(), the region of storage
itself is not truly volatile, even though the address returned by
malloc might be assigned to a volatile qualified pointer.

So in this case, casting away the volatile qualification to pass the
pointer to memset(), or any other function that requires a pointer to
non-volatile, would not produce undefined behavior.

The following example would:

#include <stdlib.h>
#include <string.h>

int main(void)
{
volatile int y;
memset((void *)&y, 0, sizeof y);
return 0;
}

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Jun 11 '07 #12
On Sun, 10 Jun 2007 23:55:03 -0000, Frodo Baggins
<fr*********@gmail.comwrote:
>On Jun 10, 5:10 pm, dj3va...@csclub.uwaterloo.ca (Dave Vandervies)
wrote:
>What are you Really Trying To Do? (If it's what I think it is, the OS
should be doing it for you.)

Why? AFAIK the libc should simply put the memory block back on the
free chain(s).

Regards,
Frodo B
OT
Because let's say on your OS another process was able to allocate that
freed-up memory. Then, if that memory was never zeroed, that other
process would be able to see what was in that memory, which could be a
security issue. In this case, something in the chain should clear
that memory, either when it's freed or when it's allocated. That
could be libc or the OS or your app. It's suspected that this is what
the OP is trying to achieve.

Jim
Jun 11 '07 #13
On Jun 11, 2:16 am, Eric Sosman <esos...@acm-dot-org.invalidwrote:
dykeinthebox wrote:
Consider the following program:
#include <stdlib.h>
#include <string.h>
int main( void )
{
void *p = malloc( 4 );
if ( p )
{
strcpy( p, "SEC" );
free( memset( p, 0, 4 ) );
}
return 0;
}
Are the characters in the memory block pointed to by p (assuming the memory
allocation succeeded) guaranteed to be set to 0, just before the memory is
being deallocated; or is a compiler allowed to 'optimize away' the call to
memset?

Since a conforming program cannot tell the difference,
the "as if rule" holds and the compiler is permitted to
delete the memset() call. For that matter, it is allowed
to delete the strcpy(), the if, and the malloc(): none of
them have any influence on the program's observable behavior,
so all of them can be optimized away.

What are you trying to do? Ensure that a password or
similar secret item doesn't show up in a core dump?
just curious to know how passwd/secret stuff can not be in core ? how
to do that ?

thanks,
-raxit sheth
>
--
Eric Sosman
esos...@acm-dot-org.invalid- Hide quoted text -

- Show quoted text -

Jun 12 '07 #14
dykeinthebox wrote:
strcpy( p, "SEC" );
free( memset( p, 0, 4 ) );
You could do something like:

strcpy( p,"SEC");
/*do some stuff with p - I guess you want that anyway*/
memset(p,0,4);
if (*p)
errorFlag=1;
free(p);

You do of course have to declare errorFlag to be something reasonable...
I can't see any way this can be optimized away, you can even declare
errorFlag to be volatile if that helps.

However, as others have suggested - on most modern operating systems
should this not normally be a problem.
Jun 12 '07 #15
Johan Bengtsson wrote, On 12/06/07 17:55:
dykeinthebox wrote:
> strcpy( p, "SEC" );
free( memset( p, 0, 4 ) );
You could do something like:

strcpy( p,"SEC");
/*do some stuff with p - I guess you want that anyway*/
memset(p,0,4);
if (*p)
errorFlag=1;
free(p);

You do of course have to declare errorFlag to be something reasonable...
I can't see any way this can be optimized away, you can even declare
errorFlag to be volatile if that helps.
Easy, it can unconditionally not set errorFlag because it knows what the
result of the if should be. Then it can drop the memset.
However, as others have suggested - on most modern operating systems
should this not normally be a problem.
If you are seriously concerned about security there are various
problems. Even after being freed the memory could still be in a core
dump because it might not have been returned to the OS. However, the
best ways of handling this will require OS specific techniques,
including how to avoid it being paged to disk!
--
Flash Gordon
Jun 12 '07 #16
"Johan Bengtsson" <qw*******@hotmail.comwrote in message
news:Ud***************@newsb.telia.net...
You could do something like:

strcpy( p,"SEC");
/*do some stuff with p - I guess you want that anyway*/
memset(p,0,4);
if (*p)
errorFlag=1;
free(p);

You do of course have to declare errorFlag to be something
reasonable... I can't see any way this can be optimized away, you
can even declare errorFlag to be volatile if that helps.
It's entirely possible to optimize it away. There's no guarantee the
compiler won't inline memset() or that it can't do interprocedural
optimization to determine the "if" statement's condition will always be
false, which would then cause "errorFlag=1" to be optimized away as dead
code (volatile or not).

Most current compilers probably don't go that far, but I can't see any
reason they couldn't. The "as if" clause is a wonderful thing.

S

--
Stephen Sprunk "Those people who think they know everything
CCIE #3723 are a great annoyance to those of us who do."
K5SSS --Isaac Asimov
--
Posted via a free Usenet account from http://www.teranews.com

Jun 12 '07 #17
ra************@gmail.com wrote:
What are you trying to do? Ensure that a password or
similar secret item doesn't show up in a core dump?

just curious to know how passwd/secret stuff can not be in core ? how
to do that ?
In this way the C standard takes an approach very much _unlike_ Java or C#.
The standard intends (in as much as history has borne the intention) to
provide a language with the minimal amount of library interfaces necessary
to provide portablility guarantees for the userbase (in knowledge and
environment). An interesting observation, I believe, is that the _broader_
the userbase the more minimal the language, since the relative merit and
value of any additional interface diminishes. I think this holds for other
languages as well, and serendipitously gives creedance to my prejudice that
monolithic language "packages"--Java/JDK, C#/.Net--just as well limit the
developer more than empower him.

Of course, with C you have to figure historical baggage (strncpy(), etc),
and also some committee tracking of longer term trends in usage which,
overtime, try to appreciate some common and widespread practices (i.e. the
standardization of fixed-width types a la uint32_t).

Others might say the totality of C99 blows my theory out of the water. Even
so, answer yourself this: does your toaster keep secrets?

Jun 12 '07 #18

"dykeinthebox" <dy**********@hotrmail.comha scritto nel messaggio
news:f4**********@news6.zwoll1.ov.home.nl...
Consider the following program:

#include <stdlib.h>
#include <string.h>

int main( void )
{
void *p = malloc( 4 );
if ( p )
{
strcpy( p, "SEC" );
free( memset( p, 0, 4 ) );
}
return 0;
}

Are the characters in the memory block pointed to by p (assuming the
memory
allocation succeeded) guaranteed to be set to 0, just before the memory is
being deallocated; or is a compiler allowed to 'optimize away' the call to
memset?
Try changing the declaration to void * volatile p. That should work.
Jun 15 '07 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: bob_jenkins | last post by:
{ const void *p; (void)memset((void *)p, ' ', (size_t)10); } Should this call to memset() be legal? Memset is of type void *memset(void *, unsigned char, size_t) Also, (void *) is the...
17
by: Frederick Ding | last post by:
Hi, guys,I met a problem, Please look at the problem below: int* bit = (int*)malloc(10000*sizeof(int)); memset(bit, 1, 10000*sizeof(int)); printf("%d %d %d\n", bit,bit, bit); Output: ...
14
by: Patrick Kowalzick | last post by:
Dear all, I have an existing piece of code with a struct with some PODs. struct A { int x; int y; };
16
by: hack_tick | last post by:
hi Guys! I am having a class, having around 300+ pointers ONLY, I need to set them all NULL, when the object is created. The best way I think is memset(this, NULL, sizeof(classname)); ...
76
by: dbansal | last post by:
Hi group, I have a question to ask you all. I have allocated some chunk of memory using ptr=(int*)malloc(). now I am trying to free that memory using free((void*)ptr). My question is does free()...
23
by: AndersWang | last post by:
Hi, dose anybody here explain to me why memset would be faster than a simple loop. I doubt about it! In an int array scenario: int array; for(int i=0;i<10;i++) //ten loops
12
by: Martin Wells | last post by:
I'm trying to come up with a fully-portable macro for supplying memset with an unsigned char rather than an int. I'm going to think out loud as I go along. . . I'll take a sample system before I...
20
by: sirsnorklingtayo | last post by:
hi guys please help about Linked List, I'm having trouble freeing the allocated memory of a single linked list node with a dynamic char* fields, it doesn't freed up if I use the FREE()...
38
by: Bill Cunningham | last post by:
When I want to clear memory space this is what I typically do myself, char a; int i; for (i=0;i != 100;++i) a='\0'; Now with the function memset I could do the same thing and it would be...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.