From owner-freebsd-hackers@FreeBSD.ORG Wed Apr 23 16:06:38 2008 Return-Path: Delivered-To: hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 7C4331065672 for ; Wed, 23 Apr 2008 16:06:38 +0000 (UTC) (envelope-from mwm@mired.org) Received: from mired.org (student.mired.org [66.92.153.77]) by mx1.freebsd.org (Postfix) with ESMTP id 0D22A8FC13 for ; Wed, 23 Apr 2008 16:06:37 +0000 (UTC) (envelope-from mwm@mired.org) Received: (qmail 53403 invoked from network); 23 Apr 2008 11:38:36 -0400 Received: from unknown (HELO mbook-fbsd) (192.168.195.251) by 0 with SMTP; 23 Apr 2008 11:38:36 -0400 Date: Wed, 23 Apr 2008 11:39:51 -0400 From: Mike Meyer To: Bernard van Gastel Message-ID: <20080423113951.020f0130@mbook-fbsd> In-Reply-To: <5F412E73-29FC-4876-A6F0-9BC269876192@bitpowder.com> References: <7d6fde3d0804222240j6b42b77yd86d8accb5a959fa@mail.gmail.com> <20080423025048.6b51a580@bhuda.mired.org> <5F412E73-29FC-4876-A6F0-9BC269876192@bitpowder.com> Organization: Meyer Consulting X-Mailer: Claws Mail 3.0.2 (GTK+ 2.12.5; amd64-portbld-freebsd7.0) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Mailman-Approved-At: Wed, 23 Apr 2008 16:11:11 +0000 Cc: hackers@freebsd.org, Garrett Cooper , Mike Meyer Subject: Re: strdup(NULL) supposed to create SIGSEGV? X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 23 Apr 2008 16:06:38 -0000 On Wed, 23 Apr 2008 10:30:39 +0200 Bernard van Gastel wrote: > Op 23 apr 2008, om 08:50 heeft Mike Meyer het volgende geschreven: > > On Tue, 22 Apr 2008 22:40:21 -0700 > > "Garrett Cooper" wrote: > > > >> Hi all, > >> I made an oops in a program, which uncovered "feature" in > >> strdup(2) > >> that I wasn't aware of before. So I was wondering, is > >> strdup(pointer = NULL) > >> supposed to segfault should this just return NULL and set errno? > > > > Yes, it's supposed to segfault. Check out what, say, strcpy does if > > you ask it to copy a NULL pointer. And this is an improvement from the > > bad old days, when they would happily walk through memory starting at > > 0..... > > I don't like it this way. I would like: > > strdup(NULL) = NULL > strdup(string) = copy of string > > strcpy(NULL, NULL) = NULL > strcpy(s1, NULL) = ERROR > strcpy(NULL, s2) = NULL (with s2 unchanged) > strcpy(s1, s2) = normal > > But I am not sure of the implications. Maybe in some situation it is > bad... Anyone? I think someone gave the reason I'm about to: trying to copy a NULL pointer means I have a bug somewhere earlier in my code that will eventually produce visibly wrong results - a segfault being such. The sooner that happens after the bug, the less code I have to search to find it, the better for me. So quietly propagating the error is bad in general. Actually, I'd like to reverse the question: under what conditions would you be trying to copy a string where not having a string isn't a sign that something is broken? > > Besides, errno is used to signal errors from system calls. strdup > > isn't a system call, it's a library function (says so at the top of > > the man page). > > But strdup uses malloc, which is a system call (from the strdup > manual: If insufficient memory is available, NULL is returned and > errno is set to ENOMEM.) As others have pointed out, malloc isn't a system call. However, what strdup (and malloc) are doing in this case is passing the system error from what is eventually an internal system call out to their caller. Basically, errno being set means some system call went wrong, even if it was made deep in the heart of a library somewhere. http://www.mired.org/consulting.html Independent Network/Unix/Perforce consultant, email for more information.