Mailing List Archive

Support open source code!


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

Re: tlug: gcc question



On Wed, 12 Jan 2000, Fredric Fredricson wrote:

> I would say this question is more appropriate on a c mailing
> list or news group but I will try to answer it anyway:
> 
> Your code is broken in a way that will show up differently on
> different OSes.
> 
> "Drew C. Poulin" wrote:
> > 
> > #include<stdio.h>
> > 
> > main(){
> > 
> >   char *fname, *lname;
> Defines two pointers to char arrays. Usually the pointers are
> initialized to 0x0000, but you can never be sure.... 

I don't think that gcc/egcs will make programs this way -- every time I've
debugged a program, I've never seen them initialized to NULL - doesn't
mean it doesn't happen sometimes, I've just never seen it... always
initialize your own pointers :)

> It is also important to remember that there is no
> built in bounds checking in c arrays. None. At all.
> C is difficult for a beginner since it is essentially
> a more or less portable assembler on steroids.

therefore, never use gets() - if I do:

char *foo;
foo=(char *)malloc(80); /* (or do char foo[80];) */
memset(foo,0,80);  /* zeroes out the string */
gets(foo);


and i type in 100 characters -- then what?  SIGSEGV, that's right.  called
a buffer overrun.  

HOWEVER:

fgets(foo,79,stdin);

will do the bounds checking for you.

--------------------------
Scott M. Stone, CCNA <sstone@example.com>
UNIX Systems and Network Engineer
Taos - The SysAdmin Company 

-------------------------------------------------------------------
Next Technical Meeting: January 14 (Fri) 19:00
* Topic: "glibc - current status and future developments"
* Guest Speaker: Ulrich Drepper (Cygnus Solutions)
* Place: Oracle Japan HQ 12F Seminar Room (New Otani Garden Court)
-------------------------------------------------------------------
more info: http://www.tlug.gr.jp        Sponsor: Global Online Japan


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links