memset +免费 [英] memset + free

查看:139
本文介绍了memset +免费的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下计划:


#include< stdlib.h>

#include< string.h>


int main(无效)

{

void * p = malloc(4);

if(p)

{

strcpy(p," SEC");

免费(memset(p,0,4));

}

返回0;

}


内存块中的字符是否由p指向(假设内存

分配成功)保证设置为0,就在内存被释放之前; b $ b被取消分配;或者是一个编译器允许优化掉对

memset的调用?

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?

推荐答案

dykeinthebox写道:
dykeinthebox wrote:

考虑以下程序:


#include< stdlib.h>

#include < string.h>


int main(无效)

{

void * p = malloc(4);

if(p)

{

strcpy(p," SEC");

free(memset( p,0,4));

}

返回0;

}


是p指向的内存块中的字符(假设内存

分配成功)保证设置为0,就在内存被取消分配之前。或者是一个编译器允许优化掉对

memset的调用?
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?



由于符合规定的程序无法区分,

似乎规则保持并允许编译器

删除memset()调用。就此而言,允许

删除strcpy(),if和malloc():没有

它们对程序有任何影响''可观察的行为,

因此所有这些都可以被优化掉。


你想做什么?确保密码或

类似的秘密项目没有出现在核心转储中?


-

Eric索斯曼
es*****@acm-dot-org.inva lid

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


在文章< f4 ********** @ news6.zwoll1.ov.home.nl>,

dykeinthebox< dy**********@hotrmail.com写道:
In article <f4**********@news6.zwoll1.ov.home.nl>,
dykeinthebox <dy**********@hotrmail.comwrote:

>考虑以下程序:

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

int main(void)
$

void * p = malloc(4 );

if(p)

{

strcpy(p," SEC");

免费(memset(p,0,4));

}

返回0;
}

是内存中的字符p指向的块(假设内存分配成功)保证设置为0,就在内存被释放之前;或者是一个编译器允许优化掉对
memset的调用?
>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?



我不相信任何严格符合要求的程序可以说明b
告诉区别,所以as-如果规则允许编译器优化

它。


你真的想做什么? (如果它是我认为的那样,操作系统

应该为你做。)

dave


-

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


惊喜你的编译器。编写比它要求更好的代码。

- 在comp.lang.c中的Keith Thompson

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


dykeinthebox写道,在10/06 / 07 21:39:
dykeinthebox wrote, On 10/06/07 21:39:

考虑以下程序:


#include< stdlib.h>

#include< string.h>


int main(无效)

{

void * p = malloc(4);

if(p)

{

strcpy(p," SEC");

免费(memset(p,0,4));

}

返回0;

}


内存块中的字符是否由p指向(假设内存

分配成功)保证设置为0,就在内存为
$ b $之前b被解除分配;或者是一个编译器允许优化掉对

memset的调用?
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?



由于合格程序无法通过

区分as-if规则允许优化memset。


如果你从安全角度看这个(即你想要
保证有人可以'检查内存以获取密码)他们我建议在一个安全组和/或专门用于你的
平台的小组中询问,因为C对你没什么帮助。

-

Flash Gordon

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


这篇关于memset +免费的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆