home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / c / 12648 < prev    next >
Encoding:
Internet Message Format  |  1992-08-21  |  2.1 KB

  1. Xref: sparky comp.lang.c:12648 comp.std.c:2489
  2. Path: sparky!uunet!mcsun!sun4nl!and!jos
  3. From: jos@and.nl (Jos Horsmeier)
  4. Newsgroups: comp.lang.c,comp.std.c
  5. Subject: Re: scrcpy implementation question
  6. Message-ID: <3275@dozo.and.nl>
  7. Date: 22 Aug 92 10:45:54 GMT
  8. References: <PINKAS.92Aug21114508@caraway.intel.com>
  9. Followup-To: comp.lang.c
  10. Distribution: comp
  11. Organization: AND Software BV Rotterdam
  12. Lines: 43
  13.  
  14. In article <PINKAS.92Aug21114508@caraway.intel.com> pinkas@caraway.intel.com (Israel Pinkas) writes:
  15. |
  16. |A compiler group working for us has told us that they plan to make the
  17. |following optimization for the strcpy library function:
  18. |
  19. |If the sizes of the src and dst are known at compile time (e.g. they are
  20. |both declared in the current file as static or auto arrays), and if the
  21. |addresses are aligned correctly, then strcpy will be inlined as a partially
  22. |unrolled loop of word moves.  In order to avoid checking every byte, they
  23. |plan to copy past the terminator ('\0'), and copy a number of bytes equal
  24. |to the smaller of the src or dst.
  25.  
  26. [ example why things would break, deleted ... ]
  27.  
  28. |Does anybody know if strcpy is allowed to copy past the end of the string?
  29. |We looked at K&R2 and at the ANSI spec, but were unable to find anything
  30. |definite.
  31.  
  32. I think it's just plain silly to implement strcpy like that. The
  33. standard indeed doesn't say much aobut this (simple) function:
  34.  
  35. 4.11.2.3 The strcpy function
  36.  
  37. The strcpy function copies the string pointed to by s2 (including the
  38. terminatin null character) into the array pointed to by s1. If copying 
  39. takes place between objects that overlap, the behavior is undefined.
  40.  
  41. [ end quote ]
  42.  
  43. I'm willing to stick out my neck, by rephrasing the last sentence:
  44.  
  45. The behavior _shall_ be defined if copying takes place between non-
  46. overlapping objects. (I know ice is thin here ... )
  47. As you said, it would certainly break lots of code (at least lots
  48. of my code would break.)
  49.  
  50. My conclusion is that strcpy is _not_ allowed to copy past the terminating
  51. '\0' character, but I can't actually prove this. I'd wish the standard
  52. would have said something like: `if and only if copying takes place ...'
  53.  
  54. kind regards,
  55.  
  56. Jos aka jos@and.nl
  57.