473,396 Members | 1,982 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,396 software developers and data experts.

struct member overlap?

Hi you,
I'm facing a strange problem, and I don't know what to do. Maybe
someone could help me understand this?

I'm storing IP and Netmask on the struct below.
1st I store IP an print it, its OK, like "127.0.0.1".
Then I store Netmask and it somehow messes the IP's last octet, like
"127.0.0." ...
Check out line comments below, please...

Thanks

---------------begin code--------------------------------------------

#define inaddrr(x) (*(struct in_addr *) &ifr->x[sizeof sa.sin_port])
#define IFRSIZE ((int)(size * sizeof (struct ifreq)))

//This is the struct used to store IP and Netmask
struct ifinfo {
char * name;
char * ip;
char * netmask;
u32 ip32;
u32 netmask32;
};

....
....
....

//Stores IP on the struct
char *ip = inet_ntoa(inaddrr(ifr_addr.sa_data));
if_info->ip = (char*) malloc(sizeof(*ip)+2);
strcpy( if_info->ip, ip );
if_info->ip32 = inaddrr(ifr_addr.sa_data).s_addr;

// *** Prints IP OK, like "127.0.0.1"
printf("\nIP=%s", if_info->ip);

//Stores Netmask on the struct
if (0 == ioctl(sockfd, SIOCGIFNETMASK, ifr) &&
strcmp("255.255.255.255", inet_ntoa(inaddrr(ifr_addr.sa_data)))) {
char * netmask = inet_ntoa(inaddrr(ifr_addr.sa_data));
if_info->netmask = (char*) malloc(sizeof(*netmask));
strcpy( if_info->netmask, netmask );
if_info->netmask32 = inaddrr(ifr_addr.sa_data).s_addr;
}

// *** Prints IP, without last octet, like "127.0.0."
printf("\nteste: %s=%s", ip, if_info->ip);

---------------end code--------------------------------------------

Nov 15 '05 #1
2 4600
hi******@gmail.com writes:
Hi you,
I'm facing a strange problem, and I don't know what to do. Maybe
someone could help me understand this?

I'm storing IP and Netmask on the struct below.
1st I store IP an print it, its OK, like "127.0.0.1".
Then I store Netmask and it somehow messes the IP's last octet, like
"127.0.0." ...
Check out line comments below, please...

Thanks

---------------begin code--------------------------------------------

#define inaddrr(x) (*(struct in_addr *) &ifr->x[sizeof sa.sin_port])
#define IFRSIZE ((int)(size * sizeof (struct ifreq)))

//This is the struct used to store IP and Netmask
struct ifinfo {
char * name;
char * ip;
char * netmask;
u32 ip32;
u32 netmask32;
};

...
...
...

//Stores IP on the struct
char *ip = inet_ntoa(inaddrr(ifr_addr.sa_data));
if_info->ip = (char*) malloc(sizeof(*ip)+2);
strcpy( if_info->ip, ip );
if_info->ip32 = inaddrr(ifr_addr.sa_data).s_addr;

// *** Prints IP OK, like "127.0.0.1"
printf("\nIP=%s", if_info->ip);

//Stores Netmask on the struct
if (0 == ioctl(sockfd, SIOCGIFNETMASK, ifr) &&
strcmp("255.255.255.255", inet_ntoa(inaddrr(ifr_addr.sa_data)))) {
char * netmask = inet_ntoa(inaddrr(ifr_addr.sa_data));
if_info->netmask = (char*) malloc(sizeof(*netmask));
strcpy( if_info->netmask, netmask );
if_info->netmask32 = inaddrr(ifr_addr.sa_data).s_addr;
}

// *** Prints IP, without last octet, like "127.0.0."
printf("\nteste: %s=%s", ip, if_info->ip);

---------------end code--------------------------------------------


Twice in the above code there are several statements that
look like they were intended to copy a string, but what they
do is unsafe (that is, wrong).

In particular, 'sizeof' is a compile-time operator, and does
not return the length of a string; rather it returns the
size of its operand. So

malloc(sizeof(*netmask))

allocates something that is the size of what 'netmask'
points to, which is to say 'sizeof(char)', which is to say 1
byte. That's not what you want.

The code can be simplified if it assumes the existence of a
function 'copy_string()' that returns a newly-allocated copy
of its string argument. See how the code is simplified:

//Stores IP in the struct
if_info->ip = copy_string( inet_ntoa(inaddrr(ifr_addr.sa_data)) );
if_info->ip32 = inaddrr(ifr_addr.sa_data).s_addr;

//Stores Netmask in the struct
if(...condition...){
if_info->netmask = copy_string( inet_ntoa(inaddrr(ifr_addr.sa_data)) );
if_info->netmask32 = inaddrr(ifr_addr.sa_data).s_addr;
}

Now all we need to do is write copy_string():

char *
copy_string( const char *s ){
char *r = malloc( strlen(s) + 1 );

/* test malloc return value */
if( r == NULL ) exit(EXIT_FAILURE); /* or whatever... */

return strcpy( r, s );
}

Notice that the function 'strlen()' is used to determine
the length of the string to copy. One extra character
is needed to store the 0 byte that's used to terminate
strings in C.
P.S. Please use spaces rather than tabs in news postings.
Nov 15 '05 #2
Groovy hepcat hi******@gmail.com was jivin' on 17 Sep 2005 10:50:22
-0700 in comp.lang.c.
struct member overlap?'s a cool scene! Dig it!
I'm storing IP and Netmask on the struct below.
1st I store IP an print it, its OK, like "127.0.0.1".
Then I store Netmask and it somehow messes the IP's last octet, like
"127.0.0." ...

#define inaddrr(x) (*(struct in_addr *) &ifr->x[sizeof sa.sin_port])
#define IFRSIZE ((int)(size * sizeof (struct ifreq)))

//This is the struct used to store IP and Netmask
struct ifinfo {
char * name;
char * ip;
char * netmask;
u32 ip32;
u32 netmask32;
};

...
...
...

//Stores IP on the struct
char *ip = inet_ntoa(inaddrr(ifr_addr.sa_data));
if_info->ip = (char*) malloc(sizeof(*ip)+2);


Don't cast the return from malloc(). The cast could hide a bug if
you fail to include stdlib.h. But that's probably unrelated to the
behaviour you describe.
You should have lurked in the newsgroup for some time before
posting. It is rude to post to a group without lurking first. I know
you didn't lurk because you are casting malloc()'s return. We are
forever telling people not to do that. You should also have read the
FAQ before posting.
ip is a pointer to char. sizeof *ip, therefore, is the size of a
char. That's one measly byte. What you're allocating here (if malloc()
succeeds, which you didn't check for - BAD!!!) is a grand total of
three bytes. How long is your string? Then how many bytes do you need
to allocate?
In future when you ask for help, please post a complete program, not
a mish-mosh of code snippets. If your program is longer than, say,
about 100 lines, cut it down to the smallest *complete* program that
still displays the problematic behaviour. By "complete" I mean
something you expect to compile and run. (And do try to compile and
run it to make sure it does still contain the error.)

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Nov 15 '05 #3

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

Similar topics

4
by: Angus Comber | last post by:
Hello I have received a lot of help on my little project here. Many thanks. I have a struct with a string and a long member. I have worked out how to qsort the struct on both members. I can...
2
by: Michael B Allen | last post by:
My understanding is that struct members will remain in order such that the following can be used to support variable sized members: struct indexed_values { int *values; unsigned char bitset;...
60
by: Mohd Hanafiah Abdullah | last post by:
Is the following code conformat to ANSI C? typedef struct { int a; int b; } doomdata; int main(void) { int x;
14
by: indigodfw | last post by:
Greetings from India I would like to know the rationale on allowing structs to be assigned (using = operator) and not allowing equality operator ( == operator) on them. The compiler when it...
3
by: Michael B Allen | last post by:
Can offsetof be used to determine the offset of a member within an embedded struct member? For example, let 'struct foo' be a structure with an embedded structure 'struct bar' which has a member...
11
by: Alfonso Morra | last post by:
Hi, I am at the end of my tether now - after spending several days trying to figure how to do this. I have finally written a simple "proof of concept" program to test serializing a structure...
8
by: Mike | last post by:
The following struct, DataStruct, is only part of a larger one that contains additional fields and arrays. I need the explicit layout because this struct is really a union, where some of the...
3
by: Hallvard B Furuseth | last post by:
to find the required alignment of a struct, I've used #include <stddef.h> struct Align_helper { char dummy; struct S align; }; enum { S_alignment = offsetof(struct Align_helper, align) };
8
by: anon.asdf | last post by:
Hi! OK, lets try "array-copy": { char arrayA; arrayA = (char){1, 2, 3}; } it does *not* work since we're trying to make a fixed array-pointer arrayA, point to another location/address...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.