From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with archive (Exim 4.43) id 1KCcIO-0004Wn-BK for mharc-grub-devel@gnu.org; Sat, 28 Jun 2008 11:25:00 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1KCcIM-0004TJ-Mt for grub-devel@gnu.org; Sat, 28 Jun 2008 11:24:58 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1KCcIL-0004Qa-AK for grub-devel@gnu.org; Sat, 28 Jun 2008 11:24:58 -0400 Received: from [199.232.76.173] (port=54295 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KCcIL-0004QB-0U for grub-devel@gnu.org; Sat, 28 Jun 2008 11:24:57 -0400 Received: from mta-out.inet.fi ([195.156.147.13]:41090 helo=kirsi2.rokki.sonera.fi) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1KCcIK-0007Js-KS for grub-devel@gnu.org; Sat, 28 Jun 2008 11:24:56 -0400 Received: from [127.0.0.1] (88.193.32.97) by kirsi2.rokki.sonera.fi (8.5.014) id 4858893A006B975F for grub-devel@gnu.org; Sat, 28 Jun 2008 18:24:54 +0300 Message-ID: <486657CD.9000506@nic.fi> Date: Sat, 28 Jun 2008 18:25:01 +0300 From: =?ISO-8859-1?Q?Vesa_J=E4=E4skel=E4inen?= User-Agent: Thunderbird 2.0.0.14 (Windows/20080421) MIME-Version: 1.0 To: The development of GRUB 2 References: <20080628080344.76c766f8@gibibit.com> In-Reply-To: <20080628080344.76c766f8@gibibit.com> X-Enigmail-Version: 0.95.6 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-detected-kernel: by monty-python.gnu.org: Linux 2.6 (newer, 3) Subject: Re: grub_strdup() error; or: grub_malloc() zeroes memory? X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: The development of GRUB 2 List-Id: The development of GRUB 2 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 28 Jun 2008 15:24:58 -0000 Colin D Bennett wrote: > It looks like grub_strdup() does not terminate the returned string with > a 0 byte. The only way I could see it working is if grub_malloc() > filled the returned memory with zeroes. Does it? > > From kern/misc.c: (circa line 476) > > char * > grub_strdup (const char *s) > { > grub_size_t len; > char *p; > > len = grub_strlen (s) + 1; > p = (char *) grub_malloc (len); > if (! p) > return 0; > > return grub_memcpy (p, s, len); > } Zero is copied from source string... notice strlen() + 1. > But right after that, we have > > char * > grub_strndup (const char *s, grub_size_t n) > { > grub_size_t len; > char *p; > > len = grub_strlen (s); > if (len > n) > len = n; > p = (char *) grub_malloc (len + 1); > if (! p) > return 0; > > grub_memcpy (p, s, len); > p[len] = '\0'; > return p; > } > > which explicitly stores a terminating null byte. If grub_malloc() did > initialize the memory to zero, then this explicity store would be > unnecessary. Here if string is not fully copied there needs to be NUL terminator.