Re: C question



rlau@csc.com wrote:
> 
> You should consider declaring your structure like that
> 
> typedef struct a_item{
> g_char b[ MAX_G_CHAR_SIZE ];
> } A_ITEM;
> 
> Use strcpy or iits equivalent to copy it. I think that's a good way to
> do that because g_malloc will that allocate the right size for you.
> 
> Your code did not allocate space for the text string.

There's another bullet waiting for a foot there. You have to be certain
your string is no longer than MAX_G_CHAR_SIZE or you could end up with a
hard to chase down segfault. This is the stuff buffer overflows are made
of.

You can insure the size either by putting a NULL at MAX_G_CHAR_SIZE-1 in
your string before you do your strcpy, or you could use one of the
non-standard strncpy functions. If strncpy is not included on your
version of UNIX, you might consider implementing it for personal use.

--
Bruce Ide                                           
bruce.ide@echostar.com
"C has all the power of assembly language combined with all the ease of
programming of assembly language."




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]