[utils] remove strnlen (#4483)

This commit is contained in:
Jonathan Hui
2020-01-22 13:39:25 -08:00
parent 708c250fe5
commit 71a7e47c81
22 changed files with 78 additions and 195 deletions
-1
View File
@@ -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_strnlen.c \
src/core/utils/parse_cmdline.cpp \
src/core/utils/slaac_address.cpp \
src/ncp/hdlc.cpp \
-1
View File
@@ -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_strnlen.c",
"src/core/utils/parse_cmdline.cpp",
"src/core/utils/slaac_address.cpp",
"src/ncp/hdlc.cpp",
-9
View File
@@ -170,10 +170,6 @@ if test "${enable_no_executables_hack}" = "yes"
then
AC_MSG_RESULT([yes])
AC_NO_EXECUTABLES
# 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_strnlen=${ac_cv_func_strnlen-no}
else
AC_MSG_RESULT([no])
fi
@@ -946,11 +942,6 @@ AC_HEADER_STDC
AC_CHECK_HEADERS([stdint.h])
AC_CHECK_HEADERS([string.h])
#
# Missing Functions
#
AC_CHECK_FUNC([strnlen], [AC_DEFINE([HAVE_STRNLEN], [1], [Define if strnlen exists.])])
#
# Check for types and structures
#
-4
View File
@@ -26,8 +26,4 @@
# POSSIBILITY OF SUCH DAMAGE.
#
include(CheckFunctionExists)
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)
-3
View File
@@ -25,6 +25,3 @@
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/* Define if strnlen exists. */
#cmakedefine01 HAVE_STRNLEN
+1 -1
View File
@@ -3611,7 +3611,7 @@ void Interpreter::ProcessLine(char *aBuf, uint16_t aBufLength, Server &aServer)
mServer = &aServer;
VerifyOrExit(aBuf != NULL && strnlen(aBuf, aBufLength + 1) <= aBufLength);
VerifyOrExit(aBuf != NULL && StringLength(aBuf, aBufLength + 1) <= aBufLength);
VerifyOrExit(Utils::CmdLineParser::ParseCmd(aBuf, argc, argv, kMaxArgs) == OT_ERROR_NONE,
mServer->OutputFormat("Error: too many args (max %d)\r\n", kMaxArgs));
-1
View File
@@ -178,7 +178,6 @@ set(COMMON_SOURCES
utils/child_supervision.cpp
utils/heap.cpp
utils/jam_detector.cpp
utils/missing_strnlen.c
utils/parse_cmdline.cpp
utils/slaac_address.cpp
)
-2
View File
@@ -235,7 +235,6 @@ SOURCES_COMMON = \
utils/child_supervision.cpp \
utils/heap.cpp \
utils/jam_detector.cpp \
utils/missing_strnlen.c \
utils/parse_cmdline.cpp \
utils/slaac_address.cpp \
$(NULL)
@@ -267,7 +266,6 @@ libopenthread_radio_a_SOURCES = \
radio/radio_callbacks.cpp \
radio/radio_platform.cpp \
thread/link_quality.cpp \
utils/missing_strnlen.c \
utils/parse_cmdline.cpp \
$(NULL)
+12
View File
@@ -35,6 +35,18 @@
namespace ot {
uint16_t StringLength(const char *aString, uint16_t aMaxLength)
{
uint16_t ret;
for (ret = 0; (ret < aMaxLength) && (aString[ret] != 0); ret++)
{
// Empty loop.
}
return ret;
}
otError StringBase::Write(char *aBuffer, uint16_t aSize, uint16_t &aLength, const char *aFormat, va_list aArgs)
{
otError error = OT_ERROR_NONE;
+11
View File
@@ -57,6 +57,17 @@ namespace ot {
*
*/
/**
* This function returns the number of characters that precede the terminating NULL character.
*
* @param[in] aString A pointer to the string.
* @param[in] aMaxLength The maximum length in bytes.
*
* @returns The number of characters that precede the terminating NULL character or @p aMaxLength, whichever is smaller.
*
*/
uint16_t StringLength(const char *aString, uint16_t aMaxLength);
/**
* This class defines the base class for `String`.
*
+1 -1
View File
@@ -495,7 +495,7 @@ void Diags::ProcessLine(const char *aString, char *aOutput, size_t aOutputMaxLen
char * argVector[kMaxArgs];
uint8_t argCount = 0;
VerifyOrExit(strnlen(aString, kMaxCommandBuffer) < kMaxCommandBuffer, error = OT_ERROR_NO_BUFS);
VerifyOrExit(StringLength(aString, kMaxCommandBuffer) < kMaxCommandBuffer, error = OT_ERROR_NO_BUFS);
strcpy(buffer, aString);
error = ot::Utils::CmdLineParser::ParseCmd(buffer, argCount, argVector, kMaxArgs);
+2 -2
View File
@@ -123,7 +123,7 @@ uint8_t NetworkName::Data::CopyTo(char *aBuffer, uint8_t aMaxSize) const
NetworkName::Data NetworkName::GetAsData(void) const
{
uint8_t len = static_cast<uint8_t>(strnlen(m8, kMaxSize + 1));
uint8_t len = static_cast<uint8_t>(StringLength(m8, kMaxSize + 1));
return Data(m8, len);
}
@@ -131,7 +131,7 @@ NetworkName::Data NetworkName::GetAsData(void) const
otError NetworkName::Set(const Data &aNameData)
{
otError error = OT_ERROR_NONE;
uint8_t newLen = static_cast<uint8_t>(strnlen(aNameData.GetBuffer(), aNameData.GetLength()));
uint8_t newLen = static_cast<uint8_t>(StringLength(aNameData.GetBuffer(), aNameData.GetLength()));
VerifyOrExit(newLen <= kMaxSize, error = OT_ERROR_INVALID_ARGS);
+5 -5
View File
@@ -254,7 +254,7 @@ otError Commissioner::AddJoiner(const Mac::ExtAddress *aEui64, const char *aPskd
VerifyOrExit(mState == OT_COMMISSIONER_STATE_ACTIVE, error = OT_ERROR_INVALID_STATE);
VerifyOrExit(strnlen(aPskd, Dtls::kPskMaxLength + 1) <= Dtls::kPskMaxLength, error = OT_ERROR_INVALID_ARGS);
VerifyOrExit(StringLength(aPskd, Dtls::kPskMaxLength + 1) <= Dtls::kPskMaxLength, error = OT_ERROR_INVALID_ARGS);
RemoveJoiner(aEui64, 0); // remove immediately
@@ -387,7 +387,7 @@ otError Commissioner::SetProvisioningUrl(const char *aProvisioningUrl)
ExitNow();
}
len = static_cast<uint8_t>(strnlen(aProvisioningUrl, sizeof(mProvisioningUrl)));
len = static_cast<uint8_t>(StringLength(aProvisioningUrl, sizeof(mProvisioningUrl)));
VerifyOrExit(len < sizeof(mProvisioningUrl), error = OT_ERROR_INVALID_ARGS);
@@ -953,7 +953,7 @@ void Commissioner::HandleJoinerFinalize(Coap::Message &aMessage, const Ip6::Mess
if (Tlv::GetTlv(aMessage, Tlv::kProvisioningUrl, sizeof(provisioningUrl), provisioningUrl) == OT_ERROR_NONE)
{
uint8_t len = static_cast<uint8_t>(strnlen(mProvisioningUrl, sizeof(mProvisioningUrl)));
uint8_t len = static_cast<uint8_t>(StringLength(mProvisioningUrl, sizeof(mProvisioningUrl)));
if ((provisioningUrl.GetProvisioningUrlLength() != len) ||
!memcmp(provisioningUrl.GetProvisioningUrl(), mProvisioningUrl, len))
@@ -1110,8 +1110,8 @@ otError Commissioner::GeneratePskc(const char * aPassPhrase,
uint16_t passphraseLen;
uint8_t networkNameLen;
passphraseLen = static_cast<uint16_t>(strnlen(aPassPhrase, OT_COMMISSIONING_PASSPHRASE_MAX_SIZE + 1));
networkNameLen = static_cast<uint8_t>(strnlen(aNetworkName, OT_NETWORK_NAME_MAX_SIZE + 1));
passphraseLen = static_cast<uint16_t>(StringLength(aPassPhrase, OT_COMMISSIONING_PASSPHRASE_MAX_SIZE + 1));
networkNameLen = static_cast<uint8_t>(StringLength(aNetworkName, OT_NETWORK_NAME_MAX_SIZE + 1));
VerifyOrExit((passphraseLen >= OT_COMMISSIONING_PASSPHRASE_MIN_SIZE) &&
(passphraseLen <= OT_COMMISSIONING_PASSPHRASE_MAX_SIZE) &&
+6 -6
View File
@@ -850,7 +850,7 @@ public:
*/
void SetCommissionerId(const char *aCommissionerId)
{
size_t length = strnlen(aCommissionerId, sizeof(mCommissionerId));
uint16_t length = StringLength(aCommissionerId, sizeof(mCommissionerId));
memcpy(mCommissionerId, aCommissionerId, length);
SetLength(static_cast<uint8_t>(length));
}
@@ -1872,7 +1872,7 @@ public:
*/
void SetProvisioningUrl(const char *aProvisioningUrl)
{
size_t len = aProvisioningUrl ? strnlen(aProvisioningUrl, kMaxLength) : 0;
uint16_t len = aProvisioningUrl ? StringLength(aProvisioningUrl, kMaxLength) : 0;
SetLength(static_cast<uint8_t>(len));
@@ -1931,7 +1931,7 @@ public:
*/
void SetVendorName(const char *aVendorName)
{
size_t len = (aVendorName == NULL) ? 0 : strnlen(aVendorName, sizeof(mVendorName));
uint16_t len = (aVendorName == NULL) ? 0 : StringLength(aVendorName, sizeof(mVendorName));
SetLength(static_cast<uint8_t>(len));
@@ -1995,7 +1995,7 @@ public:
*/
void SetVendorModel(const char *aVendorModel)
{
size_t len = (aVendorModel == NULL) ? 0 : strnlen(aVendorModel, sizeof(mVendorModel));
uint16_t len = (aVendorModel == NULL) ? 0 : StringLength(aVendorModel, sizeof(mVendorModel));
SetLength(static_cast<uint8_t>(len));
@@ -2059,7 +2059,7 @@ public:
*/
void SetVendorSwVersion(const char *aVendorSwVersion)
{
size_t len = (aVendorSwVersion == NULL) ? 0 : strnlen(aVendorSwVersion, sizeof(mVendorSwVersion));
uint16_t len = (aVendorSwVersion == NULL) ? 0 : StringLength(aVendorSwVersion, sizeof(mVendorSwVersion));
SetLength(static_cast<uint8_t>(len));
@@ -2123,7 +2123,7 @@ public:
*/
void SetVendorData(const char *aVendorData)
{
size_t len = (aVendorData == NULL) ? 0 : strnlen(aVendorData, sizeof(mVendorData));
uint16_t len = (aVendorData == NULL) ? 0 : StringLength(aVendorData, sizeof(mVendorData));
SetLength(static_cast<uint8_t>(len));
-40
View File
@@ -1,40 +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_strnlen(const char *s, size_t maxlen)
{
size_t ret;
for (ret = 0; (ret < maxlen) && (s[ret] != 0); ret++)
{
// Empty loop.
}
return ret;
}
-11
View File
@@ -49,17 +49,6 @@
/* Prototypes for our missing function replacements */
/* See: https://www.freebsd.org/cgi/man.cgi?query=strnlen */
WRAP_EXTERN_C size_t missing_strnlen(const char *s, size_t maxlen);
#undef WRAP_EXTERN_C
#ifndef HAVE_STRNLEN
#define HAVE_STRNLEN 0
#endif
#if (!HAVE_STRNLEN)
#define strnlen(S, N) missing_strnlen(S, N)
#endif
#endif // WRAP_STRING_H
+14
View File
@@ -147,6 +147,20 @@ static int spinel_errno_workaround_;
#define require(c, l) require_action(c, l, {})
#endif
#ifndef strnlen
size_t strnlen(const char *s, size_t maxlen)
{
size_t ret;
for (ret = 0; (ret < maxlen) && (s[ret] != 0); ret++)
{
// Empty loop.
}
return ret;
}
#endif
typedef struct
{
va_list obj;
+2 -2
View File
@@ -35,7 +35,7 @@
#include "spinel_decoder.hpp"
#include "common/code_utils.hpp"
#include "utils/wrap_string.h"
#include "common/string.hpp"
namespace ot {
namespace Ncp {
@@ -299,7 +299,7 @@ otError SpinelDecoder::ReadUtf8(const char *&aUtf8)
// Ensure there is at least one byte (for null character).
VerifyOrExit(mIndex + sizeof(uint8_t) <= mEnd, error = OT_ERROR_PARSE);
len = strnlen(reinterpret_cast<const char *>(&mFrame[mIndex]), mEnd - mIndex);
len = StringLength(reinterpret_cast<const char *>(&mFrame[mIndex]), mEnd - mIndex);
VerifyOrExit(len < static_cast<uint16_t>(mEnd - mIndex), error = OT_ERROR_PARSE);
aUtf8 = reinterpret_cast<const char *>(&mFrame[mIndex]);
+1 -1
View File
@@ -531,7 +531,7 @@ otError RadioSpinel::ThreadDatasetHandler(const uint8_t *aBuffer, uint16_t aLeng
size_t len;
SuccessOrExit(error = decoder.ReadUtf8(name));
len = strnlen(name, OT_NETWORK_NAME_MAX_SIZE);
len = StringLength(name, OT_NETWORK_NAME_MAX_SIZE);
memcpy(opDataset.mNetworkName.m8, name, len);
opDataset.mNetworkName.m8[len] = '\0';
opDataset.mComponents.mIsNetworkNamePresent = true;
-5
View File
@@ -122,7 +122,6 @@ check_PROGRAMS += \
test-priority-queue \
test-pskc \
test-string \
test-strnlen \
test-timer \
$(NULL)
@@ -221,9 +220,6 @@ test_pskc_SOURCES = $(COMMON_SOURCES) test_pskc.cpp
test_string_LDADD = $(COMMON_LDADD)
test_string_SOURCES = $(COMMON_SOURCES) test_string.cpp
test_strnlen_LDADD = $(COMMON_LDADD)
test_strnlen_SOURCES = test_strnlen.c
test_spinel_decoder_LDADD = $(COMMON_LDADD)
test_spinel_decoder_SOURCES = $(COMMON_SOURCES) test_spinel_decoder.cpp
@@ -259,7 +255,6 @@ PRETTY_FILES = \
$(test_spinel_decoder_SOURCES) \
$(test_spinel_encoder_SOURCES) \
$(test_string_SOURCES) \
$(test_strnlen_SOURCES) \
$(test_timer_SOURCES) \
$(test_toolchain_SOURCES) \
$(NULL)
+23
View File
@@ -117,11 +117,34 @@ void TestString(void)
printf(" -- PASS\n");
}
void TestStringLength(void)
{
char string_a[5] = "\0foo";
char string_b[8] = "foo\0bar";
printf("\nTest 4: String::StringLength() method\n");
VerifyOrQuit(StringLength(string_a, 0) == 0, "StringLength() 0len 0 fails");
VerifyOrQuit(StringLength(string_a, 1) == 0, "StringLength() 0len 1 fails");
VerifyOrQuit(StringLength(string_a, 2) == 0, "StringLength() 0len 2 fails");
VerifyOrQuit(StringLength(string_b, 0) == 0, "StringLength() 3len 0 fails");
VerifyOrQuit(StringLength(string_b, 1) == 1, "StringLength() 3len 1 fails");
VerifyOrQuit(StringLength(string_b, 2) == 2, "StringLength() 3len 2 fails");
VerifyOrQuit(StringLength(string_b, 3) == 3, "StringLength() 3len 3 fails");
VerifyOrQuit(StringLength(string_b, 4) == 3, "StringLength() 3len 4 fails");
VerifyOrQuit(StringLength(string_b, 5) == 3, "StringLength() 3len 5 fails");
VerifyOrQuit(StringLength(string_b, 6) == 3, "StringLength() 3len 6 fails");
printf(" -- PASS\n");
}
} // namespace ot
int main(void)
{
ot::TestString();
ot::TestStringLength();
printf("\nAll tests passed.\n");
return 0;
}
-100
View File
@@ -1,100 +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"
static void fail(const char *msg)
{
fprintf(stderr, "%s\n", msg);
exit(EXIT_FAILURE);
}
int main(int argc, char **argv)
{
char string_a[5] = "\0foo";
char string_b[8] = "foo\0bar";
(void)argc;
(void)argv;
if (0 != missing_strnlen(string_a, 0))
{
fail("0len 0 fails");
}
if (0 != missing_strnlen(string_a, 1))
{
fail("0len 1 fails");
}
if (0 != missing_strnlen(string_a, 2))
{
fail("0len 2 fails");
}
if (0 != missing_strnlen(string_b, 0))
{
fail("3len 0 fails");
}
if (1 != missing_strnlen(string_b, 1))
{
fail("3len 1 fails");
}
if (2 != missing_strnlen(string_b, 2))
{
fail("3len 2 fails");
}
if (3 != missing_strnlen(string_b, 3))
{
fail("3len 3 fails");
}
if (3 != missing_strnlen(string_b, 4))
{
fail("3len 4 fails");
}
if (3 != missing_strnlen(string_b, 5))
{
fail("3len 5 fails");
}
if (3 != missing_strnlen(string_b, 6))
{
fail("3len 6 fails");
}
printf("OK\n");
return EXIT_SUCCESS;
}