From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751811AbbIOMTY (ORCPT ); Tue, 15 Sep 2015 08:19:24 -0400 Received: from mga02.intel.com ([134.134.136.20]:48112 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751389AbbIOMTW (ORCPT ); Tue, 15 Sep 2015 08:19:22 -0400 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.17,535,1437462000"; d="scan'208";a="804789907" Message-ID: <1442319558.8361.33.camel@linux.intel.com> Subject: Re: [PATCH v3 2/2] lib/test-string_helpers.c: add string_get_size() tests From: Andy Shevchenko To: Vitaly Kuznetsov Cc: Rasmus Villemoes , Andrew Morton , James Bottomley , linux-kernel@vger.kernel.org, "K. Y. Srinivasan" Date: Tue, 15 Sep 2015 15:19:18 +0300 In-Reply-To: <87wpvryjb0.fsf@vitty.brq.redhat.com> References: <1442249150-31650-1-git-send-email-vkuznets@redhat.com> <1442249150-31650-3-git-send-email-vkuznets@redhat.com> <87y4g8wtjo.fsf@rasmusvillemoes.dk> <1442299382.8361.25.camel@linux.intel.com> <87wpvryjb0.fsf@vitty.brq.redhat.com> Organization: Intel Finland Oy Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.16.3-1 Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 2015-09-15 at 14:10 +0200, Vitaly Kuznetsov wrote: > Andy Shevchenko writes: > > > I prefer to put '\0' at the position after we expected have an > > actual > > '\0'. In this case we always be NULL terminated. I did this for > > hexdump > > test cases. > > Just to check I got your suggestions right: > > ... > + if (!memcmp(buf, exp_result, strnlen(exp_result, sizeof(buf) > - 1) + 1)) > + return; > + > + /* NULL terminate buf right after the expected '\0' */ > + buf[strnlen(exp_result, sizeof(buf) - 2) + 1] = '\0'; > ... > > Alternatively, we could have avoided strnlen() by asserting > strlen(exp_result) < sizeof(buf) - 1 at the very beginning. > Just buf[sizeof(buf) - 1] = '\0'; should be enough after you called the string_get_size(). And minimize the buffer to something like 16 (whatever is the biggest possible length + '\0' + some space for the wrong algorithm aligned to let's say 4. -- Andy Shevchenko Intel Finland Oy