home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / os / linux / 22311 < prev    next >
Encoding:
Text File  |  1992-12-31  |  2.4 KB  |  66 lines

  1. Newsgroups: comp.os.linux
  2. Path: sparky!uunet!think.com!rpi!batcomputer!eos.acm.rpi.edu!kutcha
  3. From: kutcha@eos.acm.rpi.edu (Phillip Rzewski)
  4. Subject: Re: "the `gets' function is unreliable and should not be used"??!!!
  5. Message-ID: <1992Dec31.222242.16868@tc.cornell.edu>
  6. Sender: news@tc.cornell.edu
  7. Nntp-Posting-Host: eos.acm.rpi.edu
  8. Organization: The Voice of Fate
  9. References: <C058sJ.Fuu@news.cso.uiuc.edu>
  10. Date: Thu, 31 Dec 1992 22:22:42 GMT
  11. Lines: 53
  12.  
  13. In article <C058sJ.Fuu@news.cso.uiuc.edu> dld54032@uxa.cso.uiuc.edu (Dave Dribin) writes:
  14. >I was trying to compile some programs that I had written for class on
  15. >a Sun Sparc and compile using gcc, and I kept getting this error that
  16. >the `gets' function is unreliable.  How can it be unreliable!  It won't
  17. >even work with this simple program:
  18. >#include <stdio.h>
  19. >
  20. >main() {
  21. >    char str[256];
  22. >
  23. >    gets(str);
  24. >    puts(str);
  25. >
  26. >    return(0);
  27. >}
  28. >
  29. >I am using SLS Linux v99.0.  Did I install anything wrong, or is the function
  30. >really unreliable.  I would think that all the standard ASNI functions would
  31. >be reliable!  Thanx in andvance, and happy new year...
  32.  
  33.     Well, it's reliable in the sense that it does what you want it to (get a
  34. string) but it's unreliable in the sense that it can cause trouble. Basically
  35. the gcc message stems from the fact that gets() just reads the string into
  36. a memory location without any regard to how much can be fit there without
  37. causing memory violations. On the other hand, fgets() has a handy argument
  38. that lets you limit how many characters can be read. So as for your program,
  39. you probably would rather:
  40.  
  41. #include <stdio.h>
  42.  
  43. main() {
  44.        char str[256];
  45.  
  46.        fgets(str, 256, stdin);
  47.        puts(str);
  48.  
  49.        return(0);
  50. }
  51.  
  52.     With your original program, if I typed 1000 characters, it would probably
  53. casue a SEGV and dump core. With the version I just typed up here, it would
  54. just read the first 255 characters (it acutally reads n - 1, so there is room
  55. for a '\0') and continue happily.
  56.  
  57.     I happen to think the gcc error message is a tad annoying, but it's
  58. only trying to be helpful. I get the impression that once people saw the
  59. problems gets() could cause they'd try to stamp out its use forever by
  60. just reminding people over and over again why it is a bad thing. :)
  61.  
  62. --
  63.   Phillip Andrew Rzewski                      Internet: kutcha@acm.rpi.edu
  64.              "Don't wait; while you're waiting, you're dying."
  65.                           --- Peter Hammill
  66.