From: Charlie Somerville Date: 2013-10-18T12:34:11-04:00 Subject: [ruby-core:57933] Re: strlen and strnlen in Ruby --52616303_71c91298_8bb0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline On =46riday, 18 October 2013 at 12:03 PM, Edward Ocampo-Gooding wrote: > Is there a reason why strcpy is used in some places but strncpy in othe= rs=3F > =20 > =20 I don't think there's any real convention in the CRuby codebase. =20 > (I=E2=80=99ve been dogmatically following the advice of my elders to fa= vour strncpy whenever possible since it=E2=80=99s easy to run into securi= ty issues or accidentally feeding a non-null-byte-terminated string in an= d having the program crash or worse yet, use the result without checking.= ) > =20 > =20 =20 Keep in mind strncpy has its own flaws - if the source string is exactly = the length of the destination buffer, it won't null terminate. =20 Personally I've never really been a fan of strncpy. strcpy is perfectly s= afe if you know what you're doing. If you don't, you'll probably run into= problems with strncpy too. --52616303_71c91298_8bb0 Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline
On =46= riday, 18 October 2013 at 12:03 PM, Edward Ocampo-Gooding wrote:
Is there a reason = why strcpy is used in some places but strncpy in others=3F

I don't think there's any real= convention in the CRuby codebase.
 
(I=E2=80=99ve been do= gmatically following the advice of my elders to favour strncpy whenever p= ossible since it=E2=80=99s easy to run into security issues or accidental= ly feeding a non-null-byte-terminated string in and having the program cr= ash or worse yet, use the result without checking.)
 
Keep in mind strncpy has its own flaws - if the sourc= e string is exactly the length of the destination buffer, it won't null t= erminate.

Personally I've never really be= en a fan of strncpy. strcpy is perfectly safe if you know what you're doi= ng. If you don't, you'll probably run into problems with strncpy too. --52616303_71c91298_8bb0--