LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-24-2011, 01:01 AM   #1
akshay_satish
Member
 
Registered: May 2011
Posts: 63

Rep: Reputation: Disabled
crash cuz of different pltfrom


This is a small piece of code snippet I am pasting here. Any help appreciated. I have a hunch the memcpy is corrupting it. The platform is X86_64.
Should I be doing a malloc on those variables inside memcpy and then use them in there?

Appreciate your time for any help. Thank you!

static int
write_cmpOMCSequenceTag(
u_char *var_val,
size_t var_val_len,
)
{
unsigned int snmp_val = 0;
memcpy( (void *)&snmp_val, (void *)var_val, var_val_len);
}

Last edited by akshay_satish; 06-05-2011 at 08:00 AM.
 
Old 05-24-2011, 06:34 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,533

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
There's not enough information in the code you posted to give you a definitive answer as to why you are getting a segfault. Perhaps it may have something to do with the function parameters 'var_val' and 'var_val_len'; please show me how you have these declared (prior to calling the function).

As for enhancing your problem solving skills, in the future, try to augment your knowledge by developing simple test programs in which you can practice your theories. For example, in the code below, I test out the memcpy() function using ideas from your code, and there is not any issue whatsoever. Thus it is my belief that memcpy() is not causing your app's segfault, but something else.

Code:
#include <string.h>
#include <sys/types.h>

void function(u_char* var_val,
              size_t  var_val_len)
{
   unsigned int snmp_val = 0;

   memcpy(&snmp_val, var_val, var_val_len);
}

int main(void)
{
   u_char var_val = 1;
   size_t var_val_len = sizeof(var_val);

   function(&var_val, var_val_len);

   return 0;
}

Last edited by dwhitney67; 05-24-2011 at 06:50 AM.
 
Old 05-24-2011, 06:45 AM   #3
akshay_satish
Member
 
Registered: May 2011
Posts: 63

Original Poster
Rep: Reputation: Disabled
I will try and get the values of that.

Last edited by akshay_satish; 06-05-2011 at 08:01 AM.
 
Old 05-24-2011, 08:35 AM   #4
segmentation_fault
Member
 
Registered: Sep 2008
Location: Ioannina, Greece
Distribution: Gentoo
Posts: 332

Rep: Reputation: 55
If your printf statements near the SEGV point don't print, try adding right after the printf
fflush(stdout);
or fflush(stderr); (if you use fprintf(stderr,"...") )
 
Old 05-24-2011, 08:44 AM   #5
akshay_satish
Member
 
Registered: May 2011
Posts: 63

Original Poster
Rep: Reputation: Disabled
Yeah I just did that and now I read your post, thank you

Here is the o/p-->
var_val_len value is 8

I have a feeling it is because of the x84_64 architecture that is causing the SEGV here.
Here the size of a pointer and an integer are not the same right and as a result SEGV?
Pls let me know your thoughts

Thanks

Last edited by akshay_satish; 06-04-2011 at 01:31 AM.
 
Old 05-24-2011, 08:56 AM   #6
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,533

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by akshay_satish View Post
Yeah I just did that and now I read your post, thank you

Here is the o/p-->
var_val_len value is 8
var_val value is u$ (some junk I guess)

I have a feeling it is because of the x84_64 architecture that is causing the SEGV here.
Here the size of a pointer and an integer are not the same right and as a result SEGV?
Pls let me know your thoughts

Thanks
You are correct; on a 64-bit arch, a pointer is 8-bytes long. On a 32-bit arch, it is 4-bytes long, which coincides with the size of an (unsigned) int.

From your function declaration, it would seem that you are treating 'var_val' as an unsigned char; yet you state that it's length is 8 bytes. Shouldn't the size be 1 byte?
 
Old 05-24-2011, 09:05 AM   #7
segmentation_fault
Member
 
Registered: Sep 2008
Location: Ioannina, Greece
Distribution: Gentoo
Posts: 332

Rep: Reputation: 55
Could a workaround be
var_val_len=sizeof(void *);
?
(I haven't use memcpy so I may say nonsences...)
 
Old 05-24-2011, 09:10 AM   #8
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,533

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by segmentation_fault View Post
Could a workaround be
var_val_len=sizeof(void *);
?
(I haven't use memcpy so I may say nonsences...)
IMHO, it would be better to use either the variable name or the type within the sizeof(). From what the OP posted, var_val is not a void pointer, but a u_char (unsigned char).
Code:
var_val_len = sizeof(var_val);   /* this is my preferred style */

/* or */

var_val_len = sizeof(u_char);
 
Old 05-24-2011, 10:54 AM   #9
akshay_satish
Member
 
Registered: May 2011
Posts: 63

Original Poster
Rep: Reputation: Disabled
Thanks a lot guys for your time and valuable suggestions.
I think var_val_len = sizeof(var_val); wont work, as it caused a SEGV again. Guess, even thats declared more than 4 bytes.
So, will look for some alternatives.
Thanks once again .. appreciate the help..
 
Old 05-24-2011, 12:22 PM   #10
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,533

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by akshay_satish View Post
Thanks a lot guys for your time and valuable suggestions.
I think var_val_len = sizeof(var_val); wont work, as it caused a SEGV again. Guess, even thats declared more than 4 bytes.
So, will look for some alternatives.
Thanks once again .. appreciate the help..
You are chasing your tail; there's nothing wrong with the memcpy() statement you have, and there's nothing wrong with the sizeof() statement above. How is var_val originally declared?

I have shown in a previous post a sample program that mimics yours. However, I declared 'var_val' (in the main function) as an u_char (unsigned char). Thus when I pass it, and its size, to the function(), all works as expected.

You have yet to tell, or show, me the declaration of the variable that you pass to the cmpOMCSequenceTag() function, much less how it is passed to the function.
 
Old 05-25-2011, 04:59 PM   #11
Heraton
Member
 
Registered: Apr 2011
Location: Germany
Distribution: Mint 10, openSuSE
Posts: 58

Rep: Reputation: 3
About what i did to fix my code...

Hi!

In my old thread you asked:
Quote:
Originally Posted by akshay_satish View Post
Hi, could you pls share what was the fix you made to memcpy to get rid of the SEGV. Currently I am experiencing the same SEGV on my X84_64 platform.
In my case, i simply forgot to include the <netinet/ether.h>, which made the compiler truncate my "64 bit pointer" into a regular integer. Thus all my returned "pointers" where too short and therefore causing the seg fault.

The fix was to include the header file in which ether_aton() was declared. This error could have been found easily by compiling my code with all warnings enabled. Look for "implicit declaration"-warnings. They are really bad.

In my code there was nothing wrong with memcpy, except that i was giving it some invalid pointers to operate with. My fault.

As dwhitney67 pointed out, it is necessary to see the declaration of your variables to check, whether something is messed up with your parameters or not.

regards, Heraton
 
Old 05-26-2011, 03:19 AM   #12
akshay_satish
Member
 
Registered: May 2011
Posts: 63

Original Poster
Rep: Reputation: Disabled
yeah I just check the declaration and it seems to be a u_char* type. because of this the size will be 8 on X86_64 and 4 on X84, in memcpy we are copying that to an unsigned int which is 4. So the dump in X86_64.
 
Old 05-26-2011, 06:09 AM   #13
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,533

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by akshay_satish View Post
yeah I just check the declaration and it seems to be a u_char* type. because of this the size will be 8 on X86_64 and 4 on X84, in memcpy we are copying that to an unsigned int which is 4. So the dump in X86_64.
Ok, that's good to know. memcpy() takes a destination pointer, a source pointer, and a size. Presumably you have allocated space for that u_char* variable. Thus whatever size you used for that should be a size between 1 and 4.

Code:
size_   var_val_len = 4;
u_char* var_val     = malloc(sizeof(u_char) * var_val_len);

...

unsigned int snmp_val;

assert(var_val_len <= sizeof(snmp_val));

memcpy(&snmp_val, var_val, var_val_len);

...
Based on the fact that var_val is such a small entity, it would have been better to declare it as an array, thus obviating any need to allocate memory for it. For example:
Code:
u_char var_val[4];
size_t var_val_len = sizeof(var_val);
 
Old 05-27-2011, 12:48 AM   #14
akshay_satish
Member
 
Registered: May 2011
Posts: 63

Original Poster
Rep: Reputation: Disabled
Thanks dwhitney67. You were of great help.
One more question to you aside this.
Do you think with the problem code I had, I would hit the SEGV on an ia64 platform as well? I am sure the pointer size is 8 on an ia64 and 4 for an integer.
So I will be seeing the SEGV in there too right?
 
Old 05-27-2011, 05:12 AM   #15
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,533

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by akshay_satish View Post
Thanks dwhitney67. You were of great help.
One more question to you aside this.
Do you think with the problem code I had, I would hit the SEGV on an ia64 platform as well? I am sure the pointer size is 8 on an ia64 and 4 for an integer.
So I will be seeing the SEGV in there too right?
I am going out on a limb to say "yes". The size of an int is 4-bytes, both on a 32- and 64-bit platform. If you attempt to copy 8 bytes (the size of a pointer on a 64-bit platform) of data into it, something will go wrong.

The error you had was taking the size of a pointer; what you required was the size of the data pointed to by that pointer.

In summary, you can copy 1-byte, 2-bytes, ..., up to 4-bytes into an int; not 8-bytes.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
segv debugging prakash_m80 Linux - Software 0 03-20-2009 05:10 AM
pls convert this windows code into linux code nagendrar Programming 6 06-03-2008 08:00 AM
g_thread_init SEGV The_Nerd Programming 1 06-20-2004 06:00 PM
pls pls pls help me ! i'm tired with httpd config on fedora apache 2.0.48 AngelOfTheDamn Fedora 0 01-24-2004 06:12 PM


All times are GMT -5. The time now is 06:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration