未定义的行为 [英] Undefined Behavior

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

问题描述

为什么这段代码会导致未定义的行为?


#include< stdio.h>

#include< stdlib.h>

#include< string.h>


#define TRUE 1

#define FALSE 0

# define MAX_PASSWORD 11

int loop;

unsigned char rx;

int _strlen(s)

unsigned char s [];

{

int i;


i = 0;

while(s [我]!=''\''')

++我;

返回i;

}


int encrypt(s,x,len)

unsigned char * s;

unsigned char x;

int len ;

{


int i,ok;

unsigned char seed_1,seed_2;


seed_1 = rx;

seed_2 = rx + 31;

ok = TRUE;

for(i = 0; i< len; i ++)

{

seed_1 - = i;

seed_2 + = i;

if(i %2!= 0)

s [i] - = seed_1;

else

s [i] + = seed_2;

if(s [i] ==''&''|| s [i] ==''\ 0''|| s [i] ==''\ n'')

ok = FALSE;

}

if(x =='' &'')

ok = FALSE;

返回ok;

}


int decrypt(s,len)

unsigned char * s;

int len;


{

unsigned char seed_1,seed_2;

int i;


seed_1 = rx;

seed_2 = rx + 31;

for(i = 0; i< len; i ++)

{

seed_1 - = i;

seed_2 + = i;

if(i%2!= 0)

s [i] + = seed_1;

else

s [i] - = seed_2;

}

返回TRUE;

}


int encrypt_string(s)

unsigned char * s;

{


unsigned char x;

int c,len;


x = 0;

c = FALSE;

len = _strlen(s);


while(c!= FALSE || loop> 0)

{

x ++;

c =加密(s,x,len);

循环 - ;

}

s [len ++] = x;

s [len ++] = rx;

s [len] =''\ 0'';


返回TRUE;

}

int decrypt_string(s)

unsigned char * s;

{


unsigned char x,y;

int len;


len = _strlen(s)-1 ;


rx = s [len];

len--;

x = s [len];

for(y = 0; y< X ; y ++)

解密(s,len);

s [len] =''\ 0'';


返回是的;

}

int main(){


unsigned char * s;

unsigned char endchar;

unsigned int y;

int len;

s =(unsigned char *)calloc(MAX_PASSWORD,sizeof(unsigned char)) ;

if(s == NULL){

printf(无法分配内存);

返回0;

}


fgets(s,MAX_PASSWORD,stdin);

len = _strlen(s);


endchar = s [len-1];


if(endchar ==''\ r''|| endchar ==''\ n'')

s [len-1] =''\ 0'';


rx = strlen(s);

循环= 1;


y = encrypt_string(s);


if(y == TRUE)

printf( " \\\
\\\
Encrypted String:%s ######%d \ n \ nn",s,_strlen(s));


y = decrypt_string (s);

if(y == TRUE)

printf(" De crypted String:%s%d \ n",s,_strlen(s));


if(* s =!NULL){

免费(s);

s = NULL;

}

else

printf("无法解除分配内存\\ n");

返回0;


}

解决方案

bb****@gmail.com 说:


为什么这段代码会导致未定义的行为?


#include< stdio.h>

#include< stdlib.h>

#include< string.h>


#define TRUE 1

#define FALSE 0

# define MAX_PASSWORD 11

int loop;

unsigned char rx;


int _strlen(s)


入侵实现名称空间。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。


在文章< 11 ******* ***************@r35g2000prh.googlegroups .com> ;,

< bb **** @ gmail.comwrote:


> #define MAX_PASSWORD 11


s =(unsigned char *)calloc(MAX_PASSWORD,sizeof(unsigned char) );

if(s == NULL){

printf(无法分配内存);

返回0;

}


fgets(s,MAX_PASSWORD,stdin);

len = _strlen(s);



这是什么_strlen()?除了其他任何东西,这是一个保留的

名称。


>

endchar = s [ len-1];


if(endchar ==''\ r''|| endchar ==''\ n'')

s [LEN-1] = '' \0 '';



所以此时s指向缓冲区中长达11个字节的字符串(包括结尾处的n
)字节长。


y = encrypt_string(s);


> int encrypt_string(s)



....


s [len ++] = x;

s [len ++] = rx;

s [len] =''\ 0 '';



Woops!你正在添加东西到s的末尾,并且溢出

缓冲区。


你应该能够自己找到这样的错误,使用一个

内存检查工具,如valgrind。


- Richard

-

" ;应考虑在一些字母表中需要多达32个字符

- X3.4,1963。


bb **** @ gmail.com 写道:


>

为什么这段代码会导致未定义的行为?


#include< stdio.h>

#include< stdlib.h>

#include< string.h>


#define TRUE 1

#define FALSE 0

#define MAX_PASSWORD 11

int loop;

unsigned char rx;


int _strlen(s)

unsigned char s [];

{



因为在这里你使用了为

实现保留的标签。不要那样做。


-

< http://www.cs.auckland.ac.nz/~pgut001/pubs/ vista_cost.txt>

< http://www.securityfocus.com/columnists/423>

< http://www.aaxnet.com/editor/ edit043.html>

cbfalconer at maineline.net


-

通过 http://www.teranews.com

Why would this code cause undefined behavior?

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

#define TRUE 1
#define FALSE 0
#define MAX_PASSWORD 11
int loop;
unsigned char rx;
int _strlen(s)
unsigned char s[];
{
int i;

i=0;
while(s[i]!=''\0'')
++i;
return i;
}

int encrypt (s, x, len)
unsigned char *s ;
unsigned char x;
int len;
{

int i,ok;
unsigned char seed_1,seed_2;

seed_1 = rx;
seed_2 = rx + 31;
ok = TRUE;
for(i = 0 ; i < len ; i++)
{
seed_1 -= i;
seed_2 += i;
if(i%2 != 0)
s[i] -= seed_1;
else
s[i] += seed_2;
if(s[i] == ''&'' || s[i]== ''\0'' || s[i] == ''\n'')
ok = FALSE;
}
if (x == ''&'')
ok = FALSE;
return ok;
}

int decrypt (s, len)
unsigned char *s;
int len ;

{
unsigned char seed_1, seed_2;
int i;

seed_1 = rx;
seed_2 = rx+31;
for (i = 0; i < len ; i++)
{
seed_1 -= i;
seed_2 +=i;
if (i % 2 != 0)
s[i] += seed_1;
else
s[i] -= seed_2;
}
return TRUE;
}

int encrypt_string (s)
unsigned char *s ;
{

unsigned char x;
int c, len;

x=0;
c=FALSE;
len= _strlen(s);

while( c!= FALSE || loop>0)
{
x++;
c=encrypt(s, x, len);
loop--;
}
s[len++] = x;
s[len++] = rx;
s[len]= ''\0'';

return TRUE;
}
int decrypt_string(s)
unsigned char *s;
{

unsigned char x,y ;
int len;

len = _strlen(s)-1;

rx= s[len];
len--;
x=s[len];
for(y = 0; y < x ; y++)
decrypt(s, len);
s[len]= ''\0'';

return TRUE;
}
int main() {

unsigned char *s;
unsigned char endchar;
unsigned int y;
int len;
s=(unsigned char *) calloc( MAX_PASSWORD, sizeof(unsigned char) );
if(s==NULL){
printf("Could not allocate Memory");
return 0;
}

fgets(s,MAX_PASSWORD,stdin);
len=_strlen(s);

endchar =s[len-1];

if(endchar==''\r'' || endchar==''\n'')
s[len-1]=''\0'';

rx=strlen(s) ;
loop=1;

y=encrypt_string(s);

if(y==TRUE)
printf("\n\nEncrypted String :%s ######%d\n\n",s,_strlen(s));

y=decrypt_string(s);
if(y==TRUE)
printf("Decrypted String: %s %d\n",s,_strlen(s));

if(*s=!NULL){
free(s);
s=NULL;
}
else
printf("Could not deallocate memory\n");
return 0;

}

解决方案

bb****@gmail.com said:

Why would this code cause undefined behavior?

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

#define TRUE 1
#define FALSE 0
#define MAX_PASSWORD 11
int loop;
unsigned char rx;
int _strlen(s)

Invading implementation namespace.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


In article <11**********************@r35g2000prh.googlegroups .com>,
<bb****@gmail.comwrote:

>#define MAX_PASSWORD 11

s=(unsigned char *) calloc( MAX_PASSWORD, sizeof(unsigned char) );
if(s==NULL){
printf("Could not allocate Memory");
return 0;
}

fgets(s,MAX_PASSWORD,stdin);
len=_strlen(s);

What''s this _strlen()? Apart from anything else, that''s a reserved
name.

>
endchar =s[len-1];

if(endchar==''\r'' || endchar==''\n'')
s[len-1]=''\0'';

So at this point s points to a string up to 11 bytes long (including
the nul at the end) in a buffer 11 bytes long.

y=encrypt_string(s);

>int encrypt_string (s)

....

s[len++] = x;
s[len++] = rx;
s[len]= ''\0'';

Woops! You''re adding things on to the end of s, and overflowing the
buffer.

You should be able to find mistakes like this yourself, using a
memory checking tool such as valgrind.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.


bb****@gmail.com wrote:

>
Why would this code cause undefined behavior?

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

#define TRUE 1
#define FALSE 0
#define MAX_PASSWORD 11
int loop;
unsigned char rx;

int _strlen(s)
unsigned char s[];
{

Because right here you have used a label reserved for the
implementation. Don''t do that.

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

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


这篇关于未定义的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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