mirror of
https://github.com/espressif/openthread.git
synced 2026-08-04 09:57:47 +00:00
[utils] remove strlcpy (#4483)
This commit is contained in:
@@ -227,7 +227,6 @@ LOCAL_SRC_FILES := \
|
||||
src/core/utils/child_supervision.cpp \
|
||||
src/core/utils/heap.cpp \
|
||||
src/core/utils/jam_detector.cpp \
|
||||
src/core/utils/missing_strlcpy.c \
|
||||
src/core/utils/missing_strnlen.c \
|
||||
src/core/utils/parse_cmdline.cpp \
|
||||
src/core/utils/slaac_address.cpp \
|
||||
|
||||
@@ -159,7 +159,6 @@ static_library("lib-ot-core") {
|
||||
"src/core/utils/child_supervision.cpp",
|
||||
"src/core/utils/heap.cpp",
|
||||
"src/core/utils/jam_detector.cpp",
|
||||
"src/core/utils/missing_strlcpy.c",
|
||||
"src/core/utils/missing_strnlen.c",
|
||||
"src/core/utils/parse_cmdline.cpp",
|
||||
"src/core/utils/slaac_address.cpp",
|
||||
|
||||
@@ -173,7 +173,6 @@ then
|
||||
# Here we guess conservative values for tests that require link checks
|
||||
# to test for these features. This will prevent these checks from
|
||||
# being performed later in the configuration process.
|
||||
ac_cv_func_strlcpy=${ac_cv_func_strlcpy-no}
|
||||
ac_cv_func_strnlen=${ac_cv_func_strnlen-no}
|
||||
else
|
||||
AC_MSG_RESULT([no])
|
||||
@@ -950,7 +949,6 @@ AC_CHECK_HEADERS([string.h])
|
||||
#
|
||||
# Missing Functions
|
||||
#
|
||||
AC_CHECK_FUNC([strlcpy], [AC_DEFINE([HAVE_STRLCPY], [1], [Define if strlcpy exists.])])
|
||||
AC_CHECK_FUNC([strnlen], [AC_DEFINE([HAVE_STRNLEN], [1], [Define if strnlen exists.])])
|
||||
|
||||
#
|
||||
|
||||
@@ -28,7 +28,6 @@
|
||||
|
||||
include(CheckFunctionExists)
|
||||
|
||||
check_function_exists("strlcpy" HAVE_STRLCPY)
|
||||
check_function_exists("strnlen" HAVE_STRNLEN)
|
||||
|
||||
configure_file(${PROJECT_SOURCE_DIR}/etc/cmake/openthread-config-generic.h.in ${PROJECT_BINARY_DIR}/etc/cmake/openthread-config-generic.h)
|
||||
|
||||
@@ -26,8 +26,5 @@
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/* Define if strlcpy exists. */
|
||||
#cmakedefine01 HAVE_STRLCPY
|
||||
|
||||
/* Define if strnlen exists. */
|
||||
#cmakedefine01 HAVE_STRNLEN
|
||||
|
||||
@@ -107,7 +107,7 @@ otError Coap::ProcessResource(int argc, char *argv[])
|
||||
mResource.mContext = this;
|
||||
mResource.mHandler = &Coap::HandleRequest;
|
||||
|
||||
strlcpy(mUriPath, argv[1], kMaxUriLength);
|
||||
strncpy(mUriPath, argv[1], sizeof(mUriPath) - 1);
|
||||
SuccessOrExit(error = otCoapAddResource(mInterpreter.mInstance, &mResource));
|
||||
}
|
||||
else
|
||||
@@ -188,7 +188,7 @@ otError Coap::ProcessRequest(int argc, char *argv[])
|
||||
if (argc > 2)
|
||||
{
|
||||
VerifyOrExit(strlen(argv[2]) < kMaxUriLength, error = OT_ERROR_INVALID_ARGS);
|
||||
strlcpy(coapUri, argv[2], kMaxUriLength);
|
||||
strncpy(coapUri, argv[2], sizeof(coapUri) - 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -123,7 +123,7 @@ otError CoapSecure::ProcessResource(int argc, char *argv[])
|
||||
mResource.mContext = this;
|
||||
mResource.mHandler = &CoapSecure::HandleRequest;
|
||||
|
||||
strlcpy(mUriPath, argv[1], kMaxUriLength);
|
||||
strncpy(mUriPath, argv[1], sizeof(mUriPath) - 1);
|
||||
SuccessOrExit(error = otCoapSecureAddResource(mInterpreter.mInstance, &mResource));
|
||||
}
|
||||
else
|
||||
@@ -247,7 +247,7 @@ otError CoapSecure::ProcessRequest(int argc, char *argv[])
|
||||
// CoAP-URI
|
||||
if (argc > (2 - indexShifter))
|
||||
{
|
||||
strlcpy(coapUri, argv[2 - indexShifter], kMaxUriLength);
|
||||
strncpy(coapUri, argv[2 - indexShifter], sizeof(coapUri) - 1);
|
||||
}
|
||||
|
||||
// CoAP-Type
|
||||
|
||||
@@ -178,7 +178,6 @@ set(COMMON_SOURCES
|
||||
utils/child_supervision.cpp
|
||||
utils/heap.cpp
|
||||
utils/jam_detector.cpp
|
||||
utils/missing_strlcpy.c
|
||||
utils/missing_strnlen.c
|
||||
utils/parse_cmdline.cpp
|
||||
utils/slaac_address.cpp
|
||||
|
||||
@@ -235,7 +235,6 @@ SOURCES_COMMON = \
|
||||
utils/child_supervision.cpp \
|
||||
utils/heap.cpp \
|
||||
utils/jam_detector.cpp \
|
||||
utils/missing_strlcpy.c \
|
||||
utils/missing_strnlen.c \
|
||||
utils/parse_cmdline.cpp \
|
||||
utils/slaac_address.cpp \
|
||||
@@ -268,7 +267,6 @@ libopenthread_radio_a_SOURCES = \
|
||||
radio/radio_callbacks.cpp \
|
||||
radio/radio_platform.cpp \
|
||||
thread/link_quality.cpp \
|
||||
utils/missing_strlcpy.c \
|
||||
utils/missing_strnlen.c \
|
||||
utils/parse_cmdline.cpp \
|
||||
$(NULL)
|
||||
|
||||
@@ -275,7 +275,7 @@ otError Commissioner::AddJoiner(const Mac::ExtAddress *aEui64, const char *aPskd
|
||||
joiner->mAny = true;
|
||||
}
|
||||
|
||||
(void)strlcpy(joiner->mPsk, aPskd, sizeof(joiner->mPsk));
|
||||
strncpy(joiner->mPsk, aPskd, sizeof(joiner->mPsk) - 1);
|
||||
joiner->mValid = true;
|
||||
joiner->mExpirationTime = TimerMilli::GetNow() + Time::SecToMsec(aTimeout);
|
||||
|
||||
@@ -308,7 +308,7 @@ otError Commissioner::GetNextJoinerInfo(uint16_t &aIterator, otJoinerInfo &aJoin
|
||||
|
||||
aJoiner.mAny = mJoiners[index].mAny;
|
||||
aJoiner.mEui64 = mJoiners[index].mEui64;
|
||||
strlcpy(aJoiner.mPsk, mJoiners[index].mPsk, sizeof(aJoiner.mPsk));
|
||||
strncpy(aJoiner.mPsk, mJoiners[index].mPsk, sizeof(aJoiner.mPsk) - 1);
|
||||
aJoiner.mExpirationTime = mJoiners[index].mExpirationTime - TimerMilli::GetNow();
|
||||
aIterator = static_cast<uint16_t>(index) + 1;
|
||||
ExitNow();
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2016, The OpenThread Authors.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the copyright holder nor the
|
||||
* names of its contributors may be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "utils/wrap_string.h"
|
||||
|
||||
size_t missing_strlcpy(char *dst, const char *src, size_t dstsize)
|
||||
{
|
||||
const size_t slen = strlen(src);
|
||||
|
||||
if (dstsize != 0)
|
||||
{
|
||||
dstsize--;
|
||||
|
||||
if (slen < dstsize)
|
||||
{
|
||||
dstsize = slen;
|
||||
}
|
||||
|
||||
if (dstsize != 0)
|
||||
{
|
||||
memcpy(dst, src, dstsize);
|
||||
}
|
||||
|
||||
dst[dstsize] = 0;
|
||||
}
|
||||
|
||||
return slen;
|
||||
}
|
||||
@@ -49,8 +49,6 @@
|
||||
|
||||
/* Prototypes for our missing function replacements */
|
||||
|
||||
/* See: https://www.freebsd.org/cgi/man.cgi?query=strlcpy */
|
||||
WRAP_EXTERN_C size_t missing_strlcpy(char *dst, const char *src, size_t dstsize);
|
||||
/* See: https://www.freebsd.org/cgi/man.cgi?query=strnlen */
|
||||
WRAP_EXTERN_C size_t missing_strnlen(const char *s, size_t maxlen);
|
||||
|
||||
@@ -60,16 +58,8 @@ WRAP_EXTERN_C size_t missing_strnlen(const char *s, size_t maxlen);
|
||||
#define HAVE_STRNLEN 0
|
||||
#endif
|
||||
|
||||
#ifndef HAVE_STRLCPY
|
||||
#define HAVE_STRLCPY 0
|
||||
#endif
|
||||
|
||||
#if (!HAVE_STRNLEN)
|
||||
#define strnlen(S, N) missing_strnlen(S, N)
|
||||
#endif
|
||||
|
||||
#if (!HAVE_STRLCPY)
|
||||
#define strlcpy(D, S, N) missing_strlcpy(D, S, N)
|
||||
#endif
|
||||
|
||||
#endif // WRAP_STRING_H
|
||||
|
||||
@@ -122,7 +122,6 @@ check_PROGRAMS += \
|
||||
test-priority-queue \
|
||||
test-pskc \
|
||||
test-string \
|
||||
test-strlcpy \
|
||||
test-strnlen \
|
||||
test-timer \
|
||||
$(NULL)
|
||||
@@ -222,9 +221,6 @@ test_pskc_SOURCES = $(COMMON_SOURCES) test_pskc.cpp
|
||||
test_string_LDADD = $(COMMON_LDADD)
|
||||
test_string_SOURCES = $(COMMON_SOURCES) test_string.cpp
|
||||
|
||||
test_strlcpy_LDADD = $(COMMON_LDADD)
|
||||
test_strlcpy_SOURCES = test_strlcpy.c
|
||||
|
||||
test_strnlen_LDADD = $(COMMON_LDADD)
|
||||
test_strnlen_SOURCES = test_strnlen.c
|
||||
|
||||
@@ -263,7 +259,6 @@ PRETTY_FILES = \
|
||||
$(test_spinel_decoder_SOURCES) \
|
||||
$(test_spinel_encoder_SOURCES) \
|
||||
$(test_string_SOURCES) \
|
||||
$(test_strlcpy_SOURCES) \
|
||||
$(test_strnlen_SOURCES) \
|
||||
$(test_timer_SOURCES) \
|
||||
$(test_toolchain_SOURCES) \
|
||||
|
||||
@@ -1,65 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2016-2017, The OpenThread Authors.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the copyright holder nor the
|
||||
* names of its contributors may be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <openthread/config.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "utils/wrap_string.h"
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
char string_a[8] = "foo";
|
||||
char string_b[] = "barbarbar";
|
||||
size_t ret = 0;
|
||||
int errors = 0;
|
||||
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
ret = strlcpy(string_a, string_b, sizeof(string_a));
|
||||
|
||||
if (0 != strcmp(string_a, "barbarb"))
|
||||
{
|
||||
printf("strcmp failed\n");
|
||||
errors++;
|
||||
}
|
||||
|
||||
if (ret != 9)
|
||||
{
|
||||
printf("strlcpy return value is wrong (%d)\n", (int)ret);
|
||||
errors++;
|
||||
}
|
||||
|
||||
if (errors != 0)
|
||||
{
|
||||
printf("FAIL\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
printf("OK\n");
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
Reference in New Issue
Block a user