From patchwork Fri Feb 14 14:59:11 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Jones X-Patchwork-Id: 11382407 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id DA75B109A for ; Fri, 14 Feb 2020 14:59:38 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id B8F9224649 for ; Fri, 14 Feb 2020 14:59:38 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="fPm9cU1m" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387505AbgBNO7i (ORCPT ); Fri, 14 Feb 2020 09:59:38 -0500 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:36753 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S2387499AbgBNO7h (ORCPT ); Fri, 14 Feb 2020 09:59:37 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1581692376; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Vi+NQYoOmU1ZDKZO+mIyuQFx3Qhtr4pAc4uPdqMXHLA=; b=fPm9cU1mTqZVmY8/5MTiIIzxaIt4n9Mn1ChJRecAH8WVPJoSzBz3zhMrgP8pVkh1PoKN/R sVd1mdddW0vIVKRSNYpebZY0qDgcpXNzEvZtgh4Tb1Kico2qvVWf8dNyqNhJMl9RPy/9bs l7WdoGm2I029TfpyzwqRbDnCOI5Tvq0= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-273-8Li7mBloMIWLyQInalQMkw-1; Fri, 14 Feb 2020 09:59:35 -0500 X-MC-Unique: 8Li7mBloMIWLyQInalQMkw-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 1F119800D4E; Fri, 14 Feb 2020 14:59:34 +0000 (UTC) Received: from kamzik.brq.redhat.com (unknown [10.43.2.160]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6ECF98AC43; Fri, 14 Feb 2020 14:59:32 +0000 (UTC) From: Andrew Jones To: kvm@vger.kernel.org Cc: pbonzini@redhat.com, bgardon@google.com, borntraeger@de.ibm.com, frankja@linux.ibm.com, thuth@redhat.com, peterx@redhat.com Subject: [PATCH 04/13] fixup! KVM: selftests: Add memory size parameter to the demand paging test Date: Fri, 14 Feb 2020 15:59:11 +0100 Message-Id: <20200214145920.30792-5-drjones@redhat.com> In-Reply-To: <20200214145920.30792-1-drjones@redhat.com> References: <20200214145920.30792-1-drjones@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org [Rewrote parse_size() to simplify and provide user more flexibility as to how sizes are input. Also fixed size overflow assert.] Signed-off-by: Andrew Jones Reviewed-by: Ben Gardon --- tools/testing/selftests/kvm/lib/test_util.c | 76 +++++++++------------ 1 file changed, 33 insertions(+), 43 deletions(-) diff --git a/tools/testing/selftests/kvm/lib/test_util.c b/tools/testing/selftests/kvm/lib/test_util.c index 706e0f963a44..cbd7f51b07a1 100644 --- a/tools/testing/selftests/kvm/lib/test_util.c +++ b/tools/testing/selftests/kvm/lib/test_util.c @@ -4,58 +4,48 @@ * * Copyright (C) 2020, Google LLC. */ - -#include "test_util.h" - +#include #include +#include +#include "test_util.h" /* * Parses "[0-9]+[kmgt]?". */ size_t parse_size(const char *size) { - size_t len = strlen(size); - size_t i; - size_t scale_shift = 0; size_t base; - - TEST_ASSERT(len > 0, "Need at least 1 digit in '%s'", size); - - /* Find the first letter in the string, indicating scale. */ - for (i = 0; i < len; i++) { - if (!isdigit(size[i])) { - TEST_ASSERT(i > 0, "Need at least 1 digit in '%s'", - size); - TEST_ASSERT(i == len - 1, - "Expected letter at the end in '%s'.", - size); - switch (tolower(size[i])) { - case 't': - scale_shift = 40; - break; - case 'g': - scale_shift = 30; - break; - case 'm': - scale_shift = 20; - break; - case 'k': - scale_shift = 10; - break; - default: - TEST_ASSERT(false, "Unknown size letter %c", - size[i]); - } - } + char *scale; + int shift = 0; + + TEST_ASSERT(size && isdigit(size[0]), "Need at least one digit in '%s'", size); + + base = strtoull(size, &scale, 0); + + TEST_ASSERT(base != ULLONG_MAX, "Overflow parsing size!"); + + switch (tolower(*scale)) { + case 't': + shift = 40; + break; + case 'g': + shift = 30; + break; + case 'm': + shift = 20; + break; + case 'k': + shift = 10; + break; + case 'b': + case '\0': + shift = 0; + break; + default: + TEST_ASSERT(false, "Unknown size letter %c", *scale); } - TEST_ASSERT(scale_shift < 8 * sizeof(size_t), - "Overflow parsing scale!"); - - base = atoi(size); - - TEST_ASSERT(!(base & ~((1 << (sizeof(size_t) - scale_shift)) - 1)), - "Overflow parsing size!"); + TEST_ASSERT((base << shift) >> shift == base, "Overflow scaling size!"); - return base << scale_shift; + return base << shift; }