473,405 Members | 2,344 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,405 software developers and data experts.

Unpredictable segmentation fault

I wrote this code:

void * xmalloc (size_t size){
register void *value = OPENSSL_malloc(size);
if (value == 0)
printf("virtual memory exhausted");
return value;
}

int _chooseTSK(char* message,int seed){
_hash hash,hash2;
char *strp,*prova,*res;
int result;
BIGNUM *mess,*cmp,*rem,*k;
BN_CTX *ctx;
mess=BN_new();rem=BN_new();k=BN_new();cmp=BN_new() ;
ctx = BN_CTX_new();
_evp_hash(&hash,message);
prova=xmalloc(2*strlen(hash.digest)+1);
strcpy(prova,hex2str(hash.digest,hash.digest_size) );
BN_hex2bn(&mess,(const char*) prova);
BN_set_word(k,K);
BN_mod(rem,mess,k,ctx);
strp=xmalloc(BN_num_bytes(mess));
strp = BN_bn2dec(mess);
res = BN_bn2dec(rem);
result=atoi((const char *) res);
_evp_hash(&hash2,message);
BN_dec2bn(&cmp,(const char*) hash2.digest);
OPENSSL_free(strp);
OPENSSL_free(mess);
OPENSSL_free(rem);
OPENSSL_free(k);
OPENSSL_free(cmp);
BN_CTX_end(ctx);
return result;
}
int main(int argc,char** argv){
int i,j;
char* prova;
char* speriamo="spero";
prova=xmalloc(strlen(argv[1])+1+5);
for(i=0;i<10;i++){
printf("\n%d",_chooseTSK(argv[1],i));
}
printf("\n");
for(j=0;j<1;j++){
for(i=0;i<1000;i++){
sprintf( prova, "%s|%d",argv[1],i);
printf("\n%d: %d",i,_chooseTSK(prova,i));
*prova="\0";
}
}
free(prova);
return 0;
}
During the execution of this program i always obtain unpredictable
segmentation fault. After running it trough gdb debugger it tolds me
this:
Program received signal SIGSEGV, Segmentation fault.
0x08048b84 in main (argc=1, argv=0xbff8ac34) at parm_utility.c:48
48 prova=xmalloc(strlen(argv[1])+1+5);

But i receive seg fault some time after 5 iteration sometime after 100
iteration,
any suggestion?

Jun 3 '07 #1
8 4182
On Jun 3, 10:43 pm, Andrea <aciru...@gmail.comwrote:
I wrote this code:

void * xmalloc (size_t size){
register void *value = OPENSSL_malloc(size);
if (value == 0)
printf("virtual memory exhausted");
return value;

}

int _chooseTSK(char* message,int seed){
_hash hash,hash2;
char *strp,*prova,*res;
int result;
BIGNUM *mess,*cmp,*rem,*k;
BN_CTX *ctx;
mess=BN_new();rem=BN_new();k=BN_new();cmp=BN_new() ;
ctx = BN_CTX_new();
_evp_hash(&hash,message);
prova=xmalloc(2*strlen(hash.digest)+1);
strcpy(prova,hex2str(hash.digest,hash.digest_size) );
BN_hex2bn(&mess,(const char*) prova);
BN_set_word(k,K);
BN_mod(rem,mess,k,ctx);
strp=xmalloc(BN_num_bytes(mess));
strp = BN_bn2dec(mess);
res = BN_bn2dec(rem);
result=atoi((const char *) res);
_evp_hash(&hash2,message);
BN_dec2bn(&cmp,(const char*) hash2.digest);
OPENSSL_free(strp);
OPENSSL_free(mess);
OPENSSL_free(rem);
OPENSSL_free(k);
OPENSSL_free(cmp);
BN_CTX_end(ctx);
return result;

}

int main(int argc,char** argv){
int i,j;
char* prova;
char* speriamo="spero";
prova=xmalloc(strlen(argv[1])+1+5);
for(i=0;i<10;i++){
printf("\n%d",_chooseTSK(argv[1],i));
}
printf("\n");
for(j=0;j<1;j++){
for(i=0;i<1000;i++){
sprintf( prova, "%s|%d",argv[1],i);
printf("\n%d: %d",i,_chooseTSK(prova,i));
*prova="\0";
}
}
free(prova);
return 0;

}

During the execution of this program i always obtain unpredictable
segmentation fault. After running it trough gdb debugger it tolds me
this:
Program received signal SIGSEGV, Segmentation fault.
0x08048b84 in main (argc=1, argv=0xbff8ac34) at parm_utility.c:48
48 prova=xmalloc(strlen(argv[1])+1+5);

But i receive seg fault some time after 5 iteration sometime after 100
iteration,
any suggestion?
Other part of my code is listed below:

void * xmalloc2 (size_t size){
register void *value = OPENSSL_malloc(size);
if (value == 0)
printf("virtual memory exhausted");
return value;
}
int _evp_hash(_hash *hash,char *message){
const EVP_MD *md;
EVP_MD_CTX mdctx;
unsigned char *ret;
unsigned int md_len,i;
OpenSSL_add_all_digests();
md = EVP_get_digestbyname(HASH);
if(!md) {
printf("Unknown message digest %s\n",HASH);
exit(1);
}
if(!(ret=(unsigned char*) xmalloc2(EVP_MAX_MD_SIZE)))
return NULL;
EVP_DigestInit(&mdctx, md);
EVP_DigestUpdate(&mdctx, message, strlen(message));
EVP_DigestFinal(&mdctx, hash->digest , &md_len);
hash->digest_size=md_len;
hash->digest_size=md_len;
free(ret);
return 0;
}
void print_hex(unsigned char *bs, unsigned int n){
int i;
for (i=0;i<n;i++){
printf("%02x",bs[i]);
}
}
char* hex2str(unsigned char *hex,unsigned int len){
int i;
char app[3];
char *buf;
//app=malloc(sizeof(char));
buf=xmalloc2(2*len+1);
strcpy(buf,"");
for (i=0;i<len;i++){
sprintf(app,"%02x",hex[i]);
strcat(buf,app);
}
//free(app);
return buf;
}

I runned again gdb and this is the problem:

*** glibc detected *** malloc(): memory corruption (fast): 0x0804cc38
***
9: 3
Program received signal SIGABRT, Aborted.
0xffffe410 in __kernel_vsyscall ()

Program received signal SIGSEGV, Segmentation fault.
0xb7e70be4 in lh_insert () from /usr/lib/i686/cmov/libcrypto.so.0.9.8
Jun 3 '07 #2
Andrea wrote:

During the execution of this program i always obtain unpredictable
segmentation fault. After running it trough gdb debugger it tolds me
this:
Program received signal SIGSEGV, Segmentation fault.
0x08048b84 in main (argc=1, argv=0xbff8ac34) at parm_utility.c:48
48 prova=xmalloc(strlen(argv[1])+1+5);

But i receive seg fault some time after 5 iteration sometime after 100
iteration,
any suggestion?
If it seg faults during a malloc() or related call, then you've likely
corrupted memory by overrunning an array boundary or the like.

Start cutting down the code to isolate the problem.


Brian
Jun 3 '07 #3
In article <5c*************@mid.individual.net>,
Default User <de***********@yahoo.comwrote:
>If it seg faults during a malloc() or related call, then you've likely
corrupted memory by overrunning an array boundary or the like.
True. Or you have double-free()ed memory or, or free()ed non-malloc()ed
memory, or something similar.
>Start cutting down the code to isolate the problem.
Most modern systems have tools to make solving this sort of problem
much more efficient. For example, if you are using a unix-like system
you may have a program called "valgrind" available, or your debugger
may have a built-in memory-checking mode. Don't waste time doing by
hand what a program can do for you.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Jun 3 '07 #4
On Sun, 03 Jun 2007 20:43:23 -0000, Andrea <ac******@gmail.comwrote:
>I wrote this code:

void * xmalloc (size_t size){
register void *value = OPENSSL_malloc(size);
if (value == 0)
printf("virtual memory exhausted");
return value;
}

int _chooseTSK(char* message,int seed){
_hash hash,hash2;
char *strp,*prova,*res;
int result;
BIGNUM *mess,*cmp,*rem,*k;
BN_CTX *ctx;
mess=BN_new();rem=BN_new();k=BN_new();cmp=BN_new() ;
ctx = BN_CTX_new();
_evp_hash(&hash,message);
prova=xmalloc(2*strlen(hash.digest)+1);
strcpy(prova,hex2str(hash.digest,hash.digest_size) );
BN_hex2bn(&mess,(const char*) prova);
BN_set_word(k,K);
BN_mod(rem,mess,k,ctx);
strp=xmalloc(BN_num_bytes(mess));
strp = BN_bn2dec(mess);
res = BN_bn2dec(rem);
result=atoi((const char *) res);
_evp_hash(&hash2,message);
BN_dec2bn(&cmp,(const char*) hash2.digest);
OPENSSL_free(strp);
OPENSSL_free(mess);
OPENSSL_free(rem);
OPENSSL_free(k);
OPENSSL_free(cmp);
BN_CTX_end(ctx);
return result;
}
int main(int argc,char** argv){
int i,j;
char* prova;
char* speriamo="spero";
prova=xmalloc(strlen(argv[1])+1+5);
Is argc at least 2? Are you sure argv[1] exists and is not NULL?
> for(i=0;i<10;i++){
printf("\n%d",_chooseTSK(argv[1],i));
}
printf("\n");
for(j=0;j<1;j++){
for(i=0;i<1000;i++){
sprintf( prova, "%s|%d",argv[1],i);
printf("\n%d: %d",i,_chooseTSK(prova,i));
*prova="\0";
}
}
free(prova);
return 0;
}
During the execution of this program i always obtain unpredictable
segmentation fault. After running it trough gdb debugger it tolds me
this:
Program received signal SIGSEGV, Segmentation fault.
0x08048b84 in main (argc=1, argv=0xbff8ac34) at parm_utility.c:48
48 prova=xmalloc(strlen(argv[1])+1+5);

But i receive seg fault some time after 5 iteration sometime after 100
iteration,
any suggestion?
Iterations of what? You do not call main recursively. You call
xmalloc with argv[1] as part of the argument only once in main and
that call is not inside a loop.

You call a dynamic allocation routine several times and make no effort
to determine if it succeeded before dereferencing the returned value.
Any allocation failure would result in undefined behavior at the point
of dereference.

Your code contains numerous non-standard functions and types and you
give us no information about any of them. In spite of appearances,
most of us are not clairvoyant.

If hash.digest is really an array of bytes containing hex data that
will be expanded 2 for 1 as your code suggests, then passing that
array to strlen will often not produce the correct value for the size
of the resulting string when passed to hex2str. This is the same
mistake you made in your other message (HEXADECIMAL to STRING) earlier
today. My response to that message explains the problem in detail.
Once your strcpy overflows the allocated space, you have invoked
undefined behavior. One of the common manifestations of this kind of
undefined behavior is for future calls to malloc or free to result in
a segfault because you have screwed up the internal data they use to
keep track of allocations.
Remove del for email
Jun 3 '07 #5
Andrea wrote:
>
I wrote this code:

void * xmalloc (size_t size){
register void *value = OPENSSL_malloc(size);
if (value == 0)
printf("virtual memory exhausted");
return value;
}
.... snip ...
>
During the execution of this program i always obtain unpredictable
segmentation fault. After running it trough gdb debugger it tolds
me this:
Possibly because you are using a non-existant function,
OPENSSL_malloc. I didn't bother to read further. Also, all
#includes are missing. This is not compilable.

--
<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 4 '07 #6
"Andrea" <ac******@gmail.comschrieb im Newsbeitrag
news:11**********************@m36g2000hse.googlegr oups.com...
>I wrote this code:

void * xmalloc (size_t size){
register void *value = OPENSSL_malloc(size);
if (value == 0)
printf("virtual memory exhausted");
return value;
}

int _chooseTSK(char* message,int seed){
_hash hash,hash2;
char *strp,*prova,*res;
int result;
BIGNUM *mess,*cmp,*rem,*k;
BN_CTX *ctx;
mess=BN_new();rem=BN_new();k=BN_new();cmp=BN_new() ;
ctx = BN_CTX_new();
_evp_hash(&hash,message);
prova=xmalloc(2*strlen(hash.digest)+1);
Sure prova isn't NULL?
strcpy(prova,hex2str(hash.digest,hash.digest_size) );
BN_hex2bn(&mess,(const char*) prova);
BN_set_word(k,K);
BN_mod(rem,mess,k,ctx);
strp=xmalloc(BN_num_bytes(mess));
sure strp isn't NULL?
strp = BN_bn2dec(mess);
Writing to strp twice? Loosing the first value creates a memory leak!
res = BN_bn2dec(rem);
result=atoi((const char *) res);
_evp_hash(&hash2,message);
BN_dec2bn(&cmp,(const char*) hash2.digest);
OPENSSL_free(strp);
freeing something that hasn't been malloc-ed? (assuming OPENSSL_free() calls
free() and BN_bn2dec() did not use malloc() to obtain memory)
OPENSSL_free(mess);
OPENSSL_free(rem);
OPENSSL_free(k);
OPENSSL_free(cmp);
BN_CTX_end(ctx);
return result;
}
int main(int argc,char** argv){
int i,j;
char* prova;
char* speriamo="spero";
prova=xmalloc(strlen(argv[1])+1+5);
Sure argv[1] isn't NULL before the call? Sure prova isn't NULL after the
all? Better check
for(i=0;i<10;i++){
printf("\n%d",_chooseTSK(argv[1],i));
}
printf("\n");
for(j=0;j<1;j++){
for(i=0;i<1000;i++){
sprintf( prova, "%s|%d",argv[1],i);
printf("\n%d: %d",i,_chooseTSK(prova,i));
*prova="\0";
}
}
free(prova);
return 0;
}
During the execution of this program i always obtain unpredictable
segmentation fault. After running it trough gdb debugger it tolds me
this:
Program received signal SIGSEGV, Segmentation fault.
0x08048b84 in main (argc=1, argv=0xbff8ac34) at parm_utility.c:48
48 prova=xmalloc(strlen(argv[1])+1+5);

But i receive seg fault some time after 5 iteration sometime after 100
iteration,
any suggestion?

Bye, Jojo
Jun 4 '07 #7
Andrea wrote:

<snip>
But i receive seg fault some time after 5 iteration sometime after 100
iteration, any suggestion?
You have posted a couple of basic questions, now you pull in the OpenSSL
library, which isn't really supposed to be used by a newbie. Even if
multiple ppl here have used this library before, OpenSSL is off-topic.

To improve your chances of getting help, you should post a *minimal*
example, illustrating your C problem, if any. If your problem is OpenSSL
specific, sci.crypt is a better place to ask.

A good start, is to post code that actually compile..!

Your code was so ill-formated (hard to read), that it completely turned
me off.

$man indent

--
Tor <torust [at] online [dot] no>
Jun 4 '07 #8
Others have already mentioned several good suggestions, I have but one
thing left to point out immediately (Yes I know this is at the top)

Andrea wrote:
void * xmalloc (size_t size){
register void *value = OPENSSL_malloc(size);
if (value == 0)
printf("virtual memory exhausted");
return value;
}
The printf() statement contains a string with no newline, I am not
completely sure this will be printed on the screen if there is no other
output before the program crashes.

A better handling IMO would be something like this:

if (!value)
{
fprintf(stderr,"virtual memory exhausted\n");
exit(EXIT_FAILURE/*or some other suitable error code*/);
}

Making the output of something that is clearly an error message better
go to stderr so it is *always* visible even if the output is piped
somewhere.
Immediately terminate the program with a defined error code in a case
where it surely does not continue to work anyway.
Jun 4 '07 #9

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

Similar topics

2
by: sivignon | last post by:
Hi, I'm writing a php script which deals with 3 ORACLE databases. This script is launch by a script shell on an linux machine like this : /../php/bin/php ./MySript.php (PHP 4.3.3) My script...
3
by: diyanat | last post by:
i am writing a cgi script in C using the CGIC library, the script fails to run, i am using apache on linux error report from apache : internal server error Premature end of script headers:...
3
by: Zheng Da | last post by:
Program received signal SIGSEGV, Segmentation fault. 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 (gdb) bt #0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 #1 0x40094c54 in malloc...
5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
20
by: ramu | last post by:
Hi, int main(void) { char ch='a'; printf("%s",ch); return 0; } Why does this give segmentation fault?
27
by: Paminu | last post by:
I have a wierd problem. In my main function I print "test" as the first thing. But if I run the call to node_alloc AFTER the printf call I get a segmentation fault and test is not printed! ...
7
by: pycraze | last post by:
I would like to ask a question. How do one handle the exception due to Segmentation fault due to Python ? Our bit operations and arithmetic manipulations are written in C and to some of our...
3
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection...
6
by: DanielJohnson | last post by:
int main() { printf("\n Hello World"); main; return 0; } This program terminate just after one loop while the second program goes on infinitely untill segmentation fault (core dumped) on...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.