Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp4050836pxu; Wed, 9 Dec 2020 07:14:30 -0800 (PST) X-Google-Smtp-Source: ABdhPJzyJy3M8mvcxkTMvmkg84p/cepMi17BYypiTAI4h5/+NzXWF4VI47svioCvkCbbyTbxAyCA X-Received: by 2002:a17:906:dd3:: with SMTP id p19mr2407013eji.221.1607526870707; Wed, 09 Dec 2020 07:14:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607526870; cv=none; d=google.com; s=arc-20160816; b=ABEOW4SA2mJToM2AjZ33MnBOb5QBbAfFI8CYhZfHZBEctWaRMgRsxgBqCl6ghw8FeB UKyPIV9FCtfMEjAnSxaScEmFmmwmUM1OnGSis4k0R5CCQPj7MSAwZ3SD0sezV3IonCI0 A/vASHeiZs46w8W206rhmfXmU/k/aXuOXV0wIsGMP7QmRwQrqXer54/9fWpjx34w8mEz 6X9xzoyXfZ/NWDMh1rasVvsF+gw+JZtJqijeZa7RcJRgR4Y2+mTJ9nfsBjzf7Qf18Ser mTpiEQyTC2+hhzx2wvrs8a124tW7MdCF8TDCYRveL0ZzVhTYvFxVPNvxCtFrWkZumLZw qVEA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=8ZGK7g35Goq64Ci9MeapPBQgkMe+5KSgf3nOc5lvEFA=; b=Bu+PsiWZkGTsR8B4YF9e1GW/TF5whr+lsm31Rn+AqvyGJs++TCrb4mRsJiukN8SHDK GIiiQlFr4YOAXmPJHiEeIrBq7P+b+Jrpsf0iDQ9Fw4U109kqcBRiAJ9WG6fOIhlPViJ3 8+1eTBhlIN44unn+ckgLrI+NS28h3cFBf1GqX+Qk2QqPhbSaOpklxyzpMrHQUQdJVosC e69rZx7Qj4Gi4WKijfW6D+iqFmICoZLcA1V3hVMGLjgyacBAFkOo4mhGioeNIluwnahs 87n42ahfqfmTyHMp4hkwiAfx/qBI5ejGlkQs7eh+Cs0+Bqbl083PDYAmeCt2OeIbERvY HfTw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=R47mLGAr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=suse.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k29si887391edk.367.2020.12.09.07.14.05; Wed, 09 Dec 2020 07:14:30 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=R47mLGAr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=suse.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732866AbgLIOQd (ORCPT + 99 others); Wed, 9 Dec 2020 09:16:33 -0500 Received: from mx2.suse.de ([195.135.220.15]:41156 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729725AbgLIOQd (ORCPT ); Wed, 9 Dec 2020 09:16:33 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1607523345; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=8ZGK7g35Goq64Ci9MeapPBQgkMe+5KSgf3nOc5lvEFA=; b=R47mLGAr7aLiQsrHNJH+SRoAOoJ5WYQZd5B2ksYtQu9uFF57MTwsYvxkG2obJg051NemD3 clV59YMSpHdpqnN4q+j/bZAUTinYcY5tmjd9e0JQi3Xml0td8ftMSjVy5XgSNdAPjpzJqP 4GZ6VwuFWZyTsJz/xMVIlB5+ecMEc9I= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id F016AB2DC; Wed, 9 Dec 2020 14:15:44 +0000 (UTC) Date: Wed, 9 Dec 2020 15:15:44 +0100 From: Petr Mladek To: Richard Fitzgerald Cc: rostedt@goodmis.org, sergey.senozhatsky@gmail.com, shuah@kernel.org, patches@opensource.cirrus.com, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: Re: [PATCH v2 2/4] lib: test_scanf: Add tests for sscanf number conversion Message-ID: References: <20201130145800.19960-1-rf@opensource.cirrus.com> <20201130145800.19960-2-rf@opensource.cirrus.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20201130145800.19960-2-rf@opensource.cirrus.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon 2020-11-30 14:57:58, Richard Fitzgerald wrote: > Adds test_sscanf to test various number conversion cases, as > number conversion was previously broken. > > This also tests the simple_strtoxxx() functions exported from > vsprintf.c. It is impressive. Honestly, I do not feel to be expert on testing and mathematics. I am not sure how comprehensive the test is. Also I am not sure what experts would say about the tricks with random numbers. Anyway, this is much more than what I have expected. And it checks great number of variants and corner cases. I suggest only one small change, see below. > --- /dev/null > +++ b/lib/test_scanf.c > @@ -0,0 +1,747 @@ > +// SPDX-License-Identifier: GPL-2.0-only > +/* > + * Test cases for sscanf facility. > + */ > + > +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +#include "../tools/testing/selftests/kselftest_module.h" > + > +#define BUF_SIZE 1024 > + > +static unsigned total_tests __initdata; > +static unsigned failed_tests __initdata; > +static char *test_buffer __initdata; > +static char *fmt_buffer __initdata; > +static struct rnd_state rnd_state __initdata; > + > +typedef int (*check_fn)(const void *check_data, const char *string, > + const char *fmt, int n_args, va_list ap); > + > +static void __scanf(4, 6) __init > +_test(check_fn fn, const void *check_data, const char *string, const char *fmt, > + int n_args, ...) > +{ > + va_list ap, ap_copy; > + int ret; > + > + total_tests++; > + > + va_start(ap, n_args); > + va_copy(ap_copy, ap); > + ret = vsscanf(string, fmt, ap_copy); > + va_end(ap_copy); > + > + if (ret != n_args) { > + pr_warn("vsscanf(\"%s\", \"%s\", ...) returned %d expected %d\n", > + string, fmt, ret, n_args); > + goto fail; > + } > + > + ret = (*fn)(check_data, string, fmt, n_args, ap); > + if (ret) > + goto fail; > + > + va_end(ap); > + > + return; > + > +fail: > + failed_tests++; > + va_end(ap); > +} > + > +#define test_one_number(T, gen_fmt, scan_fmt, val, fn) \ > +do { \ > + const T expect_val = (T)(val); \ > + T result = ~expect_val; /* should be overwritten */ \ If I get it correctly, this is supposed to initialize the temporary variable with a value that is different from the expected value. It will cause test failure when it is not updated by vsscanf(). It does not work for zero value. A better solution might be to add a constant, for example: T result = expect_val + 3; /* do not match when not overwritten */ \ I did not use "+ 1" intentionally because it might hide some overflow issues. > + \ > + snprintf(test_buffer, BUF_SIZE, gen_fmt, expect_val); \ > + _test(fn, &expect_val, test_buffer, "%" scan_fmt, 1, &result); \ > +} while (0) Otherwise, it looks good to me. Best Regards, Petr