home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.lang.c:12932 comp.std.c:2534
- Newsgroups: comp.lang.c,comp.std.c
- Path: sparky!uunet!haven.umd.edu!darwin.sura.net!convex!seas.smu.edu!utacfd.uta.edu!rwsys!sneaky!gordon
- From: gordon@sneaky.lonestar.org (Gordon Burditt)
- Subject: Re: strcpy implementation question
- Message-ID: <1992Aug26.224904.7671@sneaky.lonestar.org>
- Organization: Gordon Burditt
- References: <1992Aug23.003930.9918@saaf.se> <1992Aug23.194919.22007@iecc.cambridge.ma.us> <PINKAS.92Aug24183511@caraway.intel.com>
- Date: Wed, 26 Aug 1992 22:49:04 GMT
- Lines: 42
-
- >That's the problem. I claim that copying past the end of the string is
- >wrong, as the user may be building a string up backwards. The compiler
- >writers claim that there is nothing in the ANSI spec that claims that the
- >memory past the destination's terminator is inviolate.
-
- You won't find an explicit statement that the source string is inviolate,
- either. (On destructive-read machines using, say, actual core memory, you
- might save time not re-writing the data you read. This would be a very
- wierd "optimization".) Nor is there a statement that argc or any other
- random variable in the program is inviolate. And there isn't any statement
- that evaluating "1+2" doesn't launch missiles. But there's not much point
- in having a standard if doing anything can cause random side effects - the
- standard uses terms like "undefined behavior" or "implementation-defined
- behavior", and states limits on when these can happen. Unless the source
- and destination overlap, the behavior is supposed to be defined.
-
- >2. The user may be relying on a side effect. By only copying until the end
- > of src, rather than until the end of the string, the string may not be
- > completely copied. For example:
-
- A better example would be the infamous "variable-length struct".
- You declare something like:
-
- struct foo {
- int type;
- ... bunch of other stuff here ...
- char name[1];
- } *fp;
- char x[100];
-
- and then use it like:
-
- fp = (struct foo *) malloc(sizeof(struct foo) + strlen(x));
- strcpy(fp->name, x);
-
- "name" has supposedly known size. x has supposedly known size.
- The compiler probably aligned both of them. But if the copying stops
- at 1 char or 1 word when x contains a 70-char string, I'm going to be
- annoyed. And a lot of programs will break.
-
- Gordon L. Burditt
- sneaky.lonestar.org!gordon
-