Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!rex!samsung!spool.mu.edu!snorkelwacker.mit.edu!bloom-picayune.mit.edu!athena.mit.edu!jik From: jik@athena.mit.edu (Jonathan I. Kamens) Newsgroups: comp.sources.wanted Subject: Re: What is strdup() supposed to do? Does anyone have a copy? Message-ID: <1991Feb22.021231.25666@athena.mit.edu> Date: 22 Feb 91 02:12:31 GMT References: <1991Feb14.050716.9501@shibaya.lonestar.org> <1991Feb17.045913.17126@sbcs.sunysb.edu> <1991Feb17.164731.7564@onion.rain.com> Sender: news@athena.mit.edu (News system) Organization: Massachusetts Institute of Technology Lines: 34 In article , guido@cwi.nl (Guido van Rossum) writes: |> jeff@onion.rain.com (Jeff Beadles) writes: |> |> >Strcpy could also return an error, but I don't think that I've ever seen |> >someone check for it. :-) |> |> Actually, it is documented behaviour (at least on some systems) that |> it returns NULL when the malloc fails. I have written code that |> assumes this. It's usually a good idea to read what was written before you post a response to it. Strdup is documented as returning NULL when the malloc fails. But Jeff Beadles was talking about strcpy, not strdup, and that's what he said. Strcpy should never have anything to do with malloc. Strcpy is not documented as returning any sort of error value, and I doubt it ever could in most implementations (I'm staring at the BSD 4.3 (C version -- there is also an optomized assembler version for some architectures) version right now, and it certainly doesn't do any error checking. The only errors that strcpy can encountered are unterminated strings that will eventually cause it to go out of the valid address space, or bad pointers that will do the same thing; in both cases, the error that's going to occur is going to be a signal which, if it isn't caught, will lead to a coredump (or some other termination). Of course, on systems without memory protection (e.g. the PC, the Mac), it could just trounce all over the memory of the machine. -- Jonathan Kamens USnail: MIT Project Athena 11 Ashford Terrace jik@Athena.MIT.EDU Allston, MA 02134 Office: 617-253-8085 Home: 617-782-0710