From patchwork Sat Sep 25 13:03:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 1532694 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org 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=ewGKTsX3; dkim-atps=neutral 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=) 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) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4HGpyb2hq8z9t4b for ; Sat, 25 Sep 2021 23:04:39 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 75C2883521; Sat, 25 Sep 2021 15:04: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="ewGKTsX3"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id EF42E834FE; Sat, 25 Sep 2021 15:03:50 +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=-3.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-oi1-x235.google.com (mail-oi1-x235.google.com [IPv6:2607:f8b0:4864:20::235]) (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 0CA10834FE for ; Sat, 25 Sep 2021 15:03:41 +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-x235.google.com with SMTP id u22so18597064oie.5 for ; Sat, 25 Sep 2021 06:03:40 -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=IxX0qAXsfbBImBejZgLAqxOVV+QY+lpYFr3YTkcYDRo=; b=ewGKTsX36+eykIfgX9v7ql1+WLmkNg7WYqxws0UrXB/uwtC4amEtW/G9HJeWMq2V9b vk1L8FRZJa7dPX5HTBODwOlJL8y4valhWU5CFBpb8N4h8ZsDpWOby556gXgpgfwsSn2p FpHqqMSStbUJJbn6Bx3QmPu89BtoQkgrA4cNY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=IxX0qAXsfbBImBejZgLAqxOVV+QY+lpYFr3YTkcYDRo=; b=qd3KqQY/0zGKTaCSZ7WdLFEATvStU3hz51RmpU/WrQhhL55R+9zI/9Ra7ZXhKgHsGS CKfuWY7lysH9mLCKGErg9z8g4apmBHjHtHIw3jNyzMKMGNtDTmfemr8ROuCwoZR3+Chc SOJgHCC7Gc8i10CO7uL4klC2p6w6J43+ovOeHu4+3hahA4Qy68uaNfnYy8bIgcOjtyG9 qEhSor6kSzkczksmnrdGHxz+uV7YnG2z/iZs5rGoZN3fLj3GcTcXoSiixjcYdX6eLXKw YECTMCvK9ALiKL/9PsMnUAF8hTM4lhJdivxt403SamqowvFiP58MzBWOR2y5zvFYQKzs OmvQ== X-Gm-Message-State: AOAM5334RVbq2ZxU65AX+3DYubDTLvJ9ZCBzgv1vI+OuLDhIcRrNqlx+ 2gjtEsL6g8sS2MX6PwVgpKeeiDXQFGLNFw== X-Google-Smtp-Source: ABdhPJzH7NHfZIkXW4KcqJHk7fRu2ODp3Zax1U6SrvO4mWfxDr9ALoe/QNlu0b3ZsCHhogsRgN7i7g== X-Received: by 2002:aca:f257:: with SMTP id q84mr5234841oih.52.1632575019225; Sat, 25 Sep 2021 06:03:39 -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 f61sm2770018otf.73.2021.09.25.06.03.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Sep 2021 06:03:38 -0700 (PDT) From: Simon Glass To: U-Boot Mailing List Cc: Heinrich Schuchardt , Alexandru Gagniuc , Bin Meng , Tom Rini , Simon Glass Subject: [PATCH v4 01/15] lib: Add memdup() Date: Sat, 25 Sep 2021 07:03:06 -0600 Message-Id: <20210925070255.v4.1.I1d417387eb1e7273b536017f4a8920fc4e2369a9@changeid> X-Mailer: git-send-email 2.33.0.685.g46640cef36-goog In-Reply-To: <20210925130320.7824-1-sjg@chromium.org> References: <20210925130320.7824-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.103.2 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 --- (no changes since v2) 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 ba176fb08f7..78bd65c4136 100644 --- a/lib/string.c +++ b/lib/string.c @@ -659,6 +659,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);