From mboxrd@z Thu Jan 1 00:00:00 1970 From: Changli Gao Subject: Re: [patch] isdn: fix information leak Date: Thu, 5 Aug 2010 21:18:00 +0800 Message-ID: References: <20100805093806.GF9031@bicker> <20100805101938.GH9031@bicker> <20100805113721.GI9031@bicker> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: Karsten Keil , netdev@vger.kernel.org, kernel-janitors@vger.kernel.org To: Dan Carpenter Return-path: Received: from mail-wy0-f174.google.com ([74.125.82.174]:63922 "EHLO mail-wy0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1760331Ab0HENSW convert rfc822-to-8bit (ORCPT ); Thu, 5 Aug 2010 09:18:22 -0400 In-Reply-To: <20100805113721.GI9031@bicker> Sender: netdev-owner@vger.kernel.org List-ID: On Thu, Aug 5, 2010 at 7:37 PM, Dan Carpenter wrote= : > > Both strncpy() and strlcpy() take a limitter. =A0The difference is th= at > strlcpy() always takes on a terminator and strncpy() only adds a > terminator if there is space. > > strlcpy() is a BSD function that never caught on in Linux. =A0The gli= bc > maintainers think that if you accidentally chop off the last part of = a > word that makes you an idiot. =A0They think you should known the leng= th of > your data at all times and use memcpy() or a proper string library. > > I prefer strlcpy() to strncpy(). =A0Some people do stuff like: > =A0 =A0 =A0 =A0strncpy(bar, foo, n); > =A0 =A0 =A0 =A0bar[n] =3D '\0'; > You have to read through the code to find if n is "sizeof(bar)" or > "sizeof(bar) - 1". =A0Which is a pain in the arse. =A0strlcpy() is ex= plicit > and it's just one line of code instead of two. > > The other tricky thing you should remember about strncpy() is that th= e > posix version writes NUL chars from the end of the string to the > limitter but the kernel version only copies one NUL character. > You should spend some time on reading the source code of strlcpy() and strncpy(). the example use of them is: char dst[24]; char *src =3D "test"; strncpy(dst, src, sizeof(dst) - 1); strlcpy(dst, src, sizeof(dst)); both of them don't need to zero dst, and they don't need to pad zero at then end of the dst. --=20 Regards, Changli Gao(xiaosuo@gmail.com)