473,788 Members | 2,800 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,*r es;
int result;
BIGNUM *mess,*cmp,*rem ,*k;
BN_CTX *ctx;
mess=BN_new();r em=BN_new();k=B N_new();cmp=BN_ new();
ctx = BN_CTX_new();
_evp_hash(&hash ,message);
prova=xmalloc(2 *strlen(hash.di gest)+1);
strcpy(prova,he x2str(hash.dige st,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((co nst char *) res);
_evp_hash(&hash 2,message);
BN_dec2bn(&cmp, (const char*) hash2.digest);
OPENSSL_free(st rp);
OPENSSL_free(me ss);
OPENSSL_free(re m);
OPENSSL_free(k) ;
OPENSSL_free(cm p);
BN_CTX_end(ctx) ;
return result;
}
int main(int argc,char** argv){
int i,j;
char* prova;
char* speriamo="spero ";
prova=xmalloc(s trlen(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",arg v[1],i);
printf("\n%d: %d",i,_chooseTS K(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(s trlen(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 4215
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,*r es;
int result;
BIGNUM *mess,*cmp,*rem ,*k;
BN_CTX *ctx;
mess=BN_new();r em=BN_new();k=B N_new();cmp=BN_ new();
ctx = BN_CTX_new();
_evp_hash(&hash ,message);
prova=xmalloc(2 *strlen(hash.di gest)+1);
strcpy(prova,he x2str(hash.dige st,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((co nst char *) res);
_evp_hash(&hash 2,message);
BN_dec2bn(&cmp, (const char*) hash2.digest);
OPENSSL_free(st rp);
OPENSSL_free(me ss);
OPENSSL_free(re m);
OPENSSL_free(k) ;
OPENSSL_free(cm p);
BN_CTX_end(ctx) ;
return result;

}

int main(int argc,char** argv){
int i,j;
char* prova;
char* speriamo="spero ";
prova=xmalloc(s trlen(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",arg v[1],i);
printf("\n%d: %d",i,_chooseTS K(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(s trlen(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_digestb yname(HASH);
if(!md) {
printf("Unknown message digest %s\n",HASH);
exit(1);
}
if(!(ret=(unsig ned char*) xmalloc2(EVP_MA X_MD_SIZE)))
return NULL;
EVP_DigestInit( &mdctx, md);
EVP_DigestUpdat e(&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(unsig ned char *bs, unsigned int n){
int i;
for (i=0;i<n;i++){
printf("%02x",b s[i]);
}
}
char* hex2str(unsigne d char *hex,unsigned int len){
int i;
char app[3];
char *buf;
//app=malloc(size of(char));
buf=xmalloc2(2* len+1);
strcpy(buf,"");
for (i=0;i<len;i++) {
sprintf(app,"%0 2x",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_vsysca ll ()

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(s trlen(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.individua l.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
--
"Considerat ion 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,*r es;
int result;
BIGNUM *mess,*cmp,*rem ,*k;
BN_CTX *ctx;
mess=BN_new();r em=BN_new();k=B N_new();cmp=BN_ new();
ctx = BN_CTX_new();
_evp_hash(&hash ,message);
prova=xmalloc(2 *strlen(hash.di gest)+1);
strcpy(prova,he x2str(hash.dige st,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((co nst char *) res);
_evp_hash(&hash 2,message);
BN_dec2bn(&cmp, (const char*) hash2.digest);
OPENSSL_free(st rp);
OPENSSL_free(me ss);
OPENSSL_free(re m);
OPENSSL_free(k) ;
OPENSSL_free(cm p);
BN_CTX_end(ctx) ;
return result;
}
int main(int argc,char** argv){
int i,j;
char* prova;
char* speriamo="spero ";
prova=xmalloc(s trlen(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",arg v[1],i);
printf("\n%d: %d",i,_chooseTS K(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(s trlen(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.securityfoc us.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.goo glegroups.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,*r es;
int result;
BIGNUM *mess,*cmp,*rem ,*k;
BN_CTX *ctx;
mess=BN_new();r em=BN_new();k=B N_new();cmp=BN_ new();
ctx = BN_CTX_new();
_evp_hash(&hash ,message);
prova=xmalloc(2 *strlen(hash.di gest)+1);
Sure prova isn't NULL?
strcpy(prova,he x2str(hash.dige st,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((co nst char *) res);
_evp_hash(&hash 2,message);
BN_dec2bn(&cmp, (const char*) hash2.digest);
OPENSSL_free(st rp);
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(me ss);
OPENSSL_free(re m);
OPENSSL_free(k) ;
OPENSSL_free(cm p);
BN_CTX_end(ctx) ;
return result;
}
int main(int argc,char** argv){
int i,j;
char* prova;
char* speriamo="spero ";
prova=xmalloc(s trlen(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",arg v[1],i);
printf("\n%d: %d",i,_chooseTS K(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(s trlen(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_FAILU RE/*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
6812
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 works fine and do all what I need. But at the end of the execution, I can read "Segmentation Fault". The segmentation fault appear at the end of my script execution,
3
1939
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: /var/www/cgi-bin/script.cgi when i debug the program i get Segmentation fault gdb ./script.cgi
3
11445
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 () from /lib/tls/libc.so.6 It's really strange; I just call malloc() like "tmp=malloc(size);" the system gives me Segmentation fault I want to write a code to do like a dynamic array, and the code is as
5
2998
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 understood that a segmentation fault can occur whenever I declare a pointer and I leave it un-initialized. So I thought the problem here is with the (const char *)s in the stuct flightData (please note that I get the same fault declaring as char * the...
20
520
by: ramu | last post by:
Hi, int main(void) { char ch='a'; printf("%s",ch); return 0; } Why does this give segmentation fault?
27
3372
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! #include <stdlib.h> #include <stdio.h> typedef struct _node_t {
7
5881
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 testcases we experiance Segmentation fault from the python libraries. If i know how to handle the exception for Segmentation fault , it will help me complete the run on any testcase , even if i experiance Seg Fault due to any one or many functions in...
3
5187
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 from 10g release2 PHP is configured with the following parameters './configure' '--prefix=/opt/oracle/php' '--with-apxs=/opt/oracle/apache/bin/apxs' '--with-config-file-path=/opt/oracle/apache/conf' '--enable-safe-mode' '--enable-session'...
6
5045
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 gcc. The only difference is that in first I only call "main" and in second call
0
9656
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10364
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10172
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9967
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.