From: Andrew Cooper Subject: [PATCH 04/32] block-vvfat: fix fat_chksum() buffer overrun warning Date: Wed, 22 Apr 2015 16:08:17 +0100 Message-ID: <1429715325-5027-4-git-send-email-andrew.cooper3@citrix.com> References: <1429715235-4759-1-git-send-email-andrew.cooper3@citrix.com> <1429715325-5027-1-git-send-email-andrew.cooper3@citrix.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: <1429715325-5027-1-git-send-email-andrew.cooper3@citrix.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: Xen-devel List-Id: xen-devel@lists.xenproject.org Newer GCC versions raise an undefined behaviour warning in fat_chksum() because it overruns the name buffer. However, this is intentional behaviour because the extension array immediately follows. Refactor this function to avoid the warning and make it clear it's checksumming both parts. Signed-off-by: Andrew Cooper Coverity-ID: 1055738 --- block-vvfat.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/block-vvfat.c b/block-vvfat.c index 9eb676b..345d7be 100644 --- a/block-vvfat.c +++ b/block-vvfat.c @@ -504,14 +504,21 @@ static void set_begin_of_direntry(direntry_t* direntry, uint32_t begin) /* fat functions */ +static inline void fat_chksum_part(const char *name, size_t len, uint8_t *chksum) +{ + size_t i; + + for(i = 0; i < len; i++) + *chksum = (((*chksum&0xfe) >> 1) | ((*chksum & 0x01) ? 0x80 : 0)) + + (unsigned char)name[i]; +} + static inline uint8_t fat_chksum(const direntry_t* entry) { uint8_t chksum=0; - int i; - for(i=0;i<11;i++) - chksum=(((chksum&0xfe)>>1)|((chksum&0x01)?0x80:0)) - +(unsigned char)entry->name[i]; + fat_chksum_part(entry->name, ARRAY_SIZE(entry->name), &chksum); + fat_chksum_part(entry->extension, ARRAY_SIZE(entry->extension), &chksum); return chksum; } -- 1.7.10.4