From: "Jan Beulich" Subject: Re: [PATCH] hvmloader/smbios: Change strncpy to memcpy for anchor strings. Date: Tue, 24 Sep 2013 10:39:46 +0100 Message-ID: <52417A0202000078000F5CD4@nat28.tlf.novell.com> References: <1379960529-4978-1-git-send-email-andrew.cooper3@citrix.com> <9AAE0902D5BC7E449B7C8E4E778ABCD0111442@AMSPEX01CL01.citrite.net> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: Received: from mail6.bemta4.messagelabs.com ([85.158.143.247]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1VOP5a-00032W-9O for xen-devel@lists.xenproject.org; Tue, 24 Sep 2013 09:39:26 +0000 In-Reply-To: <9AAE0902D5BC7E449B7C8E4E778ABCD0111442@AMSPEX01CL01.citrite.net> Content-Disposition: inline List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: Paul Durrant Cc: Andrew Cooper , "Keir (Xen.org)" , xen-devel List-Id: xen-devel@lists.xenproject.org >>> On 24.09.13 at 11:14, Paul Durrant wrote: >> -----Original Message----- >> From: xen-devel-bounces@lists.xen.org [mailto:xen-devel- >> bounces@lists.xen.org] On Behalf Of Andrew Cooper >> Sent: 23 September 2013 19:22 >> To: Xen-devel >> Cc: Andrew Cooper; Keir (Xen.org); Jan Beulich >> Subject: [Xen-devel] [PATCH] hvmloader/smbios: Change strncpy to >> memcpy for anchor strings. >> >> Coverity complains about the use of strncpy() to completely fill the anchor >> strings, resulting in an unterminated string. >> >> Although the strncpy result is correct, the anchor strings are not strings > in >> the C sense, and use of memcpy is the prevaling style elsewhere in >> hvmloader >> anyway. >> >> While tidying up the style in this function, also remove some trailing >> whitespace and gratuitous cast. >> >> Signed-off-by: Andrew Cooper >> CC: Keir Fraser >> CC: Jan Beulich >> --- >> tools/firmware/hvmloader/smbios.c | 9 +++++---- >> 1 file changed, 5 insertions(+), 4 deletions(-) >> >> diff --git a/tools/firmware/hvmloader/smbios.c >> b/tools/firmware/hvmloader/smbios.c >> index 9f292cc..900f4e7 100644 >> --- a/tools/firmware/hvmloader/smbios.c >> +++ b/tools/firmware/hvmloader/smbios.c >> @@ -347,18 +347,19 @@ smbios_entry_point_init(void *start, >> { >> uint8_t sum; >> int i; >> - struct smbios_entry_point *ep = (struct smbios_entry_point *)start; >> + struct smbios_entry_point *ep = start; >> >> memset(ep, 0, sizeof(*ep)); >> >> - strncpy(ep->anchor_string, "_SM_", 4); >> + memcpy(ep->anchor_string, "_SM_", sizeof(ep->anchor_string)); > > Why the change from 4 to sizeof(ep->anchor_string) here (and similar below)? > Setting the copy length based on the size of the destination rather than the > source seems like the wrong thing to do. One can argue either way here: - passing the destination's size guarantees no memory corruption - passing the source's size guarantees no uninitialized memory Since the structure fields involved here aren't going to change, either way is fine imo. Jan