Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp3410986pxu; Tue, 15 Dec 2020 06:31:43 -0800 (PST) X-Google-Smtp-Source: ABdhPJzjTbF5lV+OR3hVxDrKIbuxthUB4GsVR7r6F2MjvE1yFdOaj/6bVaGd3C4wacbzJwNnwyLN X-Received: by 2002:a17:906:339a:: with SMTP id v26mr26650556eja.107.1608042703505; Tue, 15 Dec 2020 06:31:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1608042703; cv=none; d=google.com; s=arc-20160816; b=YgCHtnH4ZvqBl2x4Bz2io45ccQLxWkJBBJEpEEcwLPyx/KNka+vmrS8OWi+YDjTIGt 8J9yL1/GHL5i/na5kK5hv1ywlKyvjh//r5vL2NNej3KTJK5sgvfHvfDeEzk+y6kxT+rQ MeHmUzg9bRnMWorhnyJ5pwPopEJ9oWISe3CKpH7Y2pXTtibWZeN6qSzC2Vh4p5n/Rqe/ Qm/IheXN4F1CpH8bhMjHS3eCQG84Ma+cWAbDklY09i0omkLQTwsy/5SgC8EJi+KfTShi jcW4nI2lOyK4Qf8ipiVi8n7XBmEsXjRQ4fA/rIWCjcRvIl9vITSR/plb8wQGDsPaL43+ 1nOA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:dkim-signature; bh=n346iSyEamQWA/gBzJXauy4oQqN9IxZ/zPmo49LvrpM=; b=c90SjQAGvAb2iYvyL2q9U7T7GVqCxryljsQmxPTmBgH6BGHd3B0OWlrQuzdFqiIP9b 30hILvxqvWDS6N/NcConxpEGmOqH7ZHDwuVmiCvQd9uMhRS7FLSpYK278CrfX9Tm9E8F Ci+nCkX/BvulXYl13+a4MRCke01j42k1jcxDRllTd0VKyiyVwf/M74/Kg8xowhKgoFAp 4wSTYSyFjYm0YCLl1vDM6Y94BE9ViQcfr655QdJhn70bYOhjxb9DfAL65cFxydO1Q+gV qiYf+DoOdnbp1Em0zroUk5Bu+xN5UtCEFxpqPir8G9rkMFSDL5gMGJp6F3gygXQoNvM7 +/cA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cirrus.com header.s=PODMain02222019 header.b=bDnjEP+C; 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=REJECT sp=REJECT dis=NONE) header.from=cirrus.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y24si974911ejc.613.2020.12.15.06.31.18; Tue, 15 Dec 2020 06:31:43 -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=@cirrus.com header.s=PODMain02222019 header.b=bDnjEP+C; 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=REJECT sp=REJECT dis=NONE) header.from=cirrus.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729413AbgLOO2g (ORCPT + 99 others); Tue, 15 Dec 2020 09:28:36 -0500 Received: from mx0b-001ae601.pphosted.com ([67.231.152.168]:11302 "EHLO mx0b-001ae601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729471AbgLOO2R (ORCPT ); Tue, 15 Dec 2020 09:28:17 -0500 Received: from pps.filterd (m0077474.ppops.net [127.0.0.1]) by mx0b-001ae601.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 0BFENqAT028531; Tue, 15 Dec 2020 08:26:55 -0600 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cirrus.com; h=subject : to : cc : references : from : message-id : date : mime-version : in-reply-to : content-type : content-transfer-encoding; s=PODMain02222019; bh=n346iSyEamQWA/gBzJXauy4oQqN9IxZ/zPmo49LvrpM=; b=bDnjEP+CTe7XIbUOOnAlXe82KY/+zUkVGcH+lD1sQsLhqHIo1IERgfZ6nUwztpTDf+V1 5low3CG/f5de+zJldwwZrZoIcCMVUD9lyRCO4vjwGB8Gwi2XX95+EkdwVwsbI4NP012/ NWVkC7yn7M5IoJFKg/QISwItuXduXP3bm7u2l0Q4PAfgnal0wbfjHuQk0faCTdYi0cEI kJNy9VfpnvzOJsgb8S2FpK+Wdb8Ruuy1lcxx4nrYs+RkSqwUWaBWCK/nGSQdbYzTipLp 7Grb40ZiS33xGBKLFNcBCQG41V76NOtDCqmu3DtqDv8ozcum3f7y6Ed44jLPfpg0ur+G +g== Received: from ediex01.ad.cirrus.com ([87.246.76.36]) by mx0b-001ae601.pphosted.com with ESMTP id 35cu5rusw6-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT); Tue, 15 Dec 2020 08:26:55 -0600 Received: from EDIEX01.ad.cirrus.com (198.61.84.80) by EDIEX01.ad.cirrus.com (198.61.84.80) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1913.5; Tue, 15 Dec 2020 14:26:54 +0000 Received: from ediswmail.ad.cirrus.com (198.61.86.93) by EDIEX01.ad.cirrus.com (198.61.84.80) with Microsoft SMTP Server id 15.1.1913.5 via Frontend Transport; Tue, 15 Dec 2020 14:26:54 +0000 Received: from [10.0.2.15] (AUSNPC0LSNW1.ad.cirrus.com [198.61.64.236]) by ediswmail.ad.cirrus.com (Postfix) with ESMTP id EF3352AB; Tue, 15 Dec 2020 14:26:53 +0000 (UTC) Subject: Re: [PATCH v2 2/4] lib: test_scanf: Add tests for sscanf number conversion To: Petr Mladek CC: , , , , , References: <20201130145800.19960-1-rf@opensource.cirrus.com> <20201130145800.19960-2-rf@opensource.cirrus.com> From: Richard Fitzgerald Message-ID: Date: Tue, 15 Dec 2020 14:26:53 +0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.5.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="utf-8"; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 adultscore=0 mlxscore=0 mlxlogscore=921 suspectscore=0 spamscore=0 clxscore=1015 malwarescore=0 impostorscore=0 priorityscore=1501 bulkscore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2012150103 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 09/12/2020 14:15, Petr Mladek wrote: > 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 That's a ~, not a - ~0 = 0xFFFFFFFF ~-1 = 0 > 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 >