B
bbaale
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!='\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 -= seed_1;
else
s += seed_2;
if(s == '&' || s== '\0' || s == '\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 += seed_1;
else
s -= 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;
}
#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!='\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 -= seed_1;
else
s += seed_2;
if(s == '&' || s== '\0' || s == '\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 += seed_1;
else
s -= 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;
}