From owner-freebsd-hackers@FreeBSD.ORG Wed Apr 23 09:03:08 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 DAC94106564A for ; Wed, 23 Apr 2008 09:03:08 +0000 (UTC) (envelope-from bvgastel@bitpowder.com) Received: from smtpout1.ru.nl (smtpout1.ru.nl [131.174.66.174]) by mx1.freebsd.org (Postfix) with ESMTP id 774DA8FC16 for ; Wed, 23 Apr 2008 09:03:08 +0000 (UTC) (envelope-from bvgastel@bitpowder.com) X-Virus-Scanned: by AMaViS & ClamAV Received: from smaug.bitpowder.com (vhe-410001.sshn.net [195.169.215.64]) by smtp.ru.nl (Postfix) with ESMTP id E9DE480298; Wed, 23 Apr 2008 10:30:41 +0200 (CEST) Received: from shadowfax.bitpowder.com (localhost [127.0.0.1]) by smaug.bitpowder.com (8.14.2/8.13.6) with ESMTP id m3N8UZgn013881; Wed, 23 Apr 2008 08:30:36 GMT (envelope-from bvgastel@bitpowder.com) Message-Id: <5F412E73-29FC-4876-A6F0-9BC269876192@bitpowder.com> From: Bernard van Gastel To: Mike Meyer In-Reply-To: <20080423025048.6b51a580@bhuda.mired.org> Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes Content-Transfer-Encoding: 7bit Mime-Version: 1.0 (Apple Message framework v919.2) Date: Wed, 23 Apr 2008 10:30:39 +0200 References: <7d6fde3d0804222240j6b42b77yd86d8accb5a959fa@mail.gmail.com> <20080423025048.6b51a580@bhuda.mired.org> X-Mailer: Apple Mail (2.919.2) Cc: Garrett Cooper , hackers@freebsd.org 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 09:03:08 -0000 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? > > 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.) Regards, Bernard