home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / c / 12835 < prev    next >
Encoding:
Text File  |  1992-08-26  |  3.8 KB  |  90 lines

  1. Path: sparky!uunet!munnari.oz.au!goanna!ok
  2. From: ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: strcpy implementation question
  5. Message-ID: <14213@goanna.cs.rmit.oz.au>
  6. Date: 27 Aug 92 06:56:37 GMT
  7. References: <PINKAS.92Aug21114508@caraway.intel.com> <PINKAS.92Aug25163006@caraway.intel.com>
  8. Organization: Comp Sci, RMIT, Melbourne, Australia
  9. Lines: 79
  10.  
  11. In article <PINKAS.92Aug25163006@caraway.intel.com>, pinkas@caraway.intel.com (Israel Pinkas) writes:
  12. > In article <14173@goanna.cs.rmit.oz.au> ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) writes:
  13. > > There is a very basic problem which explains why strcpy() should touch
  14. > > only strlen(source)+1 chars:
  15. > >     characters following the intended end of the source MAY NOT EXIST
  16. > >     characters following the intended end of the destination MAY NOT EXIST
  17. > >     characters following the intended end of the source may exist
  18. > >     AND YET NOT BE INITIALISED.
  19. > I wish people would read my posting.  I specifically detailed the fact that
  20. > the strcpy routine in question only was used when the source AND
  21. > destination had been allocated by the compiler and were of a known size AT
  22. > COMPILE TIME.
  23.  
  24. I wish people would read charitably and not assume that the answer is
  25. irrelevant to their question without thinking.
  26.  
  27. The point is _uniformity_.  Since strcpy() *can't* go too far in the
  28. great majority of cases, it will only be confusing if it does something
  29. different in this case.
  30.  
  31. When is the size of the source known at compile time?  Two cases:
  32. (a) the source is a string literal (or is a pointer whose value is known
  33.     by constant propagation to be a specific string literal)
  34. (b) the code looks something like
  35.     if (strlen(x) == 27) { /* don't change x */; strcpy(d, x); ...}
  36.  
  37. > The fact that the memory has not been initialized is irrelevant.  If the
  38. > source didn't care about it, neither will the destination..
  39.  
  40. This is wrong.  It isn' the source or the destination that cares, it is
  41. strcpy() that cares.  strcpy() MUST NOT READ any uninitialised memory
  42. locations.
  43.  
  44. > > The traditional documentation for strcpy() said:
  45. > >     strcpy copies string s2 to s1,
  46. > >     -->    stopping after the null character has been moved
  47. > > X3J11 weren't in the business of breaking anything they didn't _have_ to
  48. > > break, not if they could help it.
  49. > Man pages do not a spec make.
  50.  
  51. On the contrary, until X3J11 published the final standard, that *WAS*
  52. the spec.  The spec for strcpy() _was_ (until the ANSI standard was
  53. published) whatever the SVID said it was, and the SVID says
  54.     char *strcpy(s, s2) char *s1, *s2;
  55.     The function strcpy() copies string s2 to s1,
  56.     stopping after the null character has been copied.
  57.     The functions ... strcpy ... all alter s1.
  58.     These functions do not check for overflow of the array pointed to by s1
  59.     ... Each function returns s1.
  60.  
  61. The spec _NOW_ is whatever X3J11 say it is (our copy of the standard _still_
  62. hasn't arrived or I'd quote it).  But you can bet your ar-- that X3J11 didn't
  63. _intend_ to change this.
  64.  
  65. Plauger & Brodie isn't the ANSI standard either (I'll start quoting that just
  66. as soon as our copy arrives).  But they say:
  67.     char *strcpy(char *s1, const char *s2)
  68.     This function copies the string s2, including its terminating null
  69.     character, to successive elements of the array of char whose first
  70.     element has the address s1.  It returns s1.
  71. You can be pretty sure that a whole lot of C programmers are going to take
  72. that as gospel.
  73.  
  74. > I specifically asked people for a spec that states that the optimization is
  75. > illegal or for pointers to code that is widely used that will be broken by
  76. > this code.
  77.  
  78. Several people have posted examples like
  79.     char buffer[3+1+3+1];
  80.     strcpy(buffer+3+1, "bar");
  81.     strcpy(buffer, "foo);
  82.     buffer[3] = '.';
  83. which ought to set buffer to |foo.bar\0|.
  84.  
  85. -- 
  86. You can lie with statistics ... but not to a statistician.
  87.