From patchwork Thu May 6 14:23:49 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 1475000 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.a=rsa-sha256 header.s=google header.b=YLEV1/2I; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4FbbTH0dFtz9sW7 for ; Fri, 7 May 2021 00:25:23 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 2131082D2E; Thu, 6 May 2021 16:25:13 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="YLEV1/2I"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 7040282D0F; Thu, 6 May 2021 16:25:06 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,SPF_HELO_NONE autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-oi1-x22d.google.com (mail-oi1-x22d.google.com [IPv6:2607:f8b0:4864:20::22d]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 6D74882D0F for ; Thu, 6 May 2021 16:24:58 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@chromium.org Received: by mail-oi1-x22d.google.com with SMTP id o16so503371oiw.3 for ; Thu, 06 May 2021 07:24:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=7jCg6dF+wtzsjkbPgMr/E1sY6tJe18PFRQla7EzSOyE=; b=YLEV1/2Is03CR8R9z/ZOmTtgxJZ0wvfPYzeCXucLo7N49+mgJqvXNT+XPMd5MyDFSz NFSuJeuZf8SzH6np6zslFS29oFz7nsW20GLNZbtZX1hbcxsEGCwA51YDC6lYNxK12SfL DfB9N9eQP3oJMwkVpAArAUe9O1wEVcRsU3lec= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=7jCg6dF+wtzsjkbPgMr/E1sY6tJe18PFRQla7EzSOyE=; b=lWu6ihH3KHI0MWJbU3mWl87mweGjCfCRSj26FuWzpCmo3tXtFX19NPEZrZZv/fcWHk EZnYa2MCmYsKl37mjrXhdaFRJ2savbaLF+pOKSOLvT+ArF+Lx2aZGowQpKAYNBGGXYKl GGmBRE6IREB/MQ8KkJBGqxxUgsUWWH1lWn6ZBypP4IkGxKjdbuCzTvP43WAN+OvzzupU vAWfOb23XNwjrxXXoUf12s+0QiBnCyWWvj/b0xzCVv81hP0Jt/B9rtCP1CAGDneXEvLF iybSbcdqjydxaIoZ4lsnKuSNcfN6iSbUsRAC3sRl5VjmyWAf0UK8KQr8LQeT3L5cNcsK 6+oA== X-Gm-Message-State: AOAM531LxAVYW3jGAArjB+wQH5FquMkuqu9fK/vFA3BjyT5Y6P9iHoeQ nsLK2uM1VKN9taG6pfh0MULkPQ5uj9UagNe0 X-Google-Smtp-Source: ABdhPJzI15BWID9NjzE3ztyvJ23anrlo2veWYwW72p5MpXFhZ3AW4eKNc8zexZ2bYC7xdGueoaCYHg== X-Received: by 2002:aca:f587:: with SMTP id t129mr3339897oih.84.1620311096501; Thu, 06 May 2021 07:24:56 -0700 (PDT) Received: from kiwi.bld.corp.google.com (c-67-190-101-114.hsd1.co.comcast.net. [67.190.101.114]) by smtp.gmail.com with ESMTPSA id z204sm445490oia.2.2021.05.06.07.24.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 May 2021 07:24:56 -0700 (PDT) From: Simon Glass To: U-Boot Mailing List Cc: Tom Rini , Heinrich Schuchardt , Andre Przywara , Masahiro Yamada , Rasmus Villemoes , Robert Marko , Bin Meng , Simon Glass Subject: [PATCH v2 01/50] lib: Add memdup() Date: Thu, 6 May 2021 08:23:49 -0600 Message-Id: <20210506082420.v2.1.I1d417387eb1e7273b536017f4a8920fc4e2369a9@changeid> X-Mailer: git-send-email 2.31.1.607.g51e8a6a459-goog In-Reply-To: <20210506142438.1310977-1-sjg@chromium.org> References: <20210506142438.1310977-1-sjg@chromium.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.102.4 at phobos.denx.de X-Virus-Status: Clean Add a function to duplicate a memory region, a little like strdup(). Signed-off-by: Simon Glass --- Changes in v2: - Add a patch to introduce a memdup() function include/linux/string.h | 13 +++++++++++++ lib/string.c | 13 +++++++++++++ test/lib/string.c | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) diff --git a/include/linux/string.h b/include/linux/string.h index dd255f21633..3169c93796e 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -129,6 +129,19 @@ extern void * memchr(const void *,int,__kernel_size_t); void *memchr_inv(const void *, int, size_t); #endif +/** + * memdup() - allocate a buffer and copy in the contents + * + * Note that this returns a valid pointer even if @len is 0 + * + * @src: data to copy in + * @len: number of bytes to copy + * @return allocated buffer with the copied contents, or NULL if not enough + * memory is available + * + */ +char *memdup(const void *src, size_t len); + unsigned long ustrtoul(const char *cp, char **endp, unsigned int base); unsigned long long ustrtoull(const char *cp, char **endp, unsigned int base); diff --git a/lib/string.c b/lib/string.c index a0cff8fe88e..1be61ee0499 100644 --- a/lib/string.c +++ b/lib/string.c @@ -658,6 +658,19 @@ void * memscan(void * addr, int c, size_t size) } #endif +char *memdup(const void *src, size_t len) +{ + char *p; + + p = malloc(len); + if (!p) + return NULL; + + memcpy(p, src, len); + + return p; +} + #ifndef __HAVE_ARCH_STRSTR /** * strstr - Find the first substring in a %NUL terminated string diff --git a/test/lib/string.c b/test/lib/string.c index 64234bef36c..5dcf4d6db00 100644 --- a/test/lib/string.c +++ b/test/lib/string.c @@ -23,6 +23,8 @@ /* Allow for copying up to 32 bytes */ #define BUFLEN (SWEEP + 33) +#define TEST_STR "hello" + /** * init_buffer() - initialize buffer * @@ -193,3 +195,33 @@ static int lib_memmove(struct unit_test_state *uts) } LIB_TEST(lib_memmove, 0); + +/** lib_memdup() - unit test for memdup() */ +static int lib_memdup(struct unit_test_state *uts) +{ + char buf[BUFLEN]; + size_t len; + char *p, *q; + + /* Zero size should do nothing */ + p = memdup(NULL, 0); + ut_assertnonnull(p); + free(p); + + p = memdup(buf, 0); + ut_assertnonnull(p); + free(p); + + strcpy(buf, TEST_STR); + len = sizeof(TEST_STR); + p = memdup(buf, len); + ut_asserteq_mem(p, buf, len); + + q = memdup(p, len); + ut_asserteq_mem(q, buf, len); + free(q); + free(p); + + return 0; +} +LIB_TEST(lib_memdup, 0);