From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp40.i.mail.ru (smtp40.i.mail.ru [94.100.177.100]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 46E5145C304 for ; Fri, 11 Dec 2020 10:58:23 +0300 (MSK) References: <20201210161832.729439-1-gorcunov@gmail.com> <20201210161832.729439-2-gorcunov@gmail.com> From: Serge Petrenko Message-ID: <02c6bfc0-ceb1-c1bc-5d8b-2450dbc082bc@tarantool.org> Date: Fri, 11 Dec 2020 10:58:21 +0300 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="utf-8"; format="flowed" Content-Transfer-Encoding: 8bit Content-Language: en-GB Subject: Re: [Tarantool-patches] [PATCH v4 1/4] util: introduce strlcpy helper List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Cyrill Gorcunov , tml Cc: Mons Anderson , Vladislav Shpilevoy 11.12.2020 10:34, Serge Petrenko via Tarantool-patches пишет: > > 10.12.2020 19:18, Cyrill Gorcunov пишет: >> Very convenient to have this string extension. >> We will use it in crash handling. >> >> Signed-off-by: Cyrill Gorcunov >> --- > > Hi! Thanks for the patch! > > LGTM. > >> CMakeLists.txt            |  1 + >>   src/lib/core/util.c       | 14 ++++++++++++++ >>   src/trivia/config.h.cmake |  5 +++++ >>   src/trivia/util.h         | 15 +++++++++++++++ >>   4 files changed, 35 insertions(+) >> >> diff --git a/CMakeLists.txt b/CMakeLists.txt >> index fa6818f8e..aa23113b3 100644 >> --- a/CMakeLists.txt >> +++ b/CMakeLists.txt >> @@ -126,6 +126,7 @@ set(CMAKE_REQUIRED_LIBRARIES "") >>   check_function_exists(clock_gettime HAVE_CLOCK_GETTIME_WITHOUT_RT) >>     check_symbol_exists(__get_cpuid cpuid.h HAVE_CPUID) >> +check_symbol_exists(strlcpy string.h HAVE_STRLCPY) >>     # Checks for libev >>   include(CheckStructHasMember) >> diff --git a/src/lib/core/util.c b/src/lib/core/util.c >> index dfce317f0..7b168a177 100644 >> --- a/src/lib/core/util.c >> +++ b/src/lib/core/util.c >> @@ -250,6 +250,20 @@ int2str(long long int val) >>       return buf; >>   } >>   +#ifndef HAVE_STRLCPY >> +size_t >> +strlcpy(char *dst, const char *src, size_t size) >> +{ >> +    size_t src_len = strlen(src); >> +    if (size) { One nit: we usually use `if(smth)` for boolean values only. For integer values please use the explicit variant: `if(size != 0)` >> +        size_t len = (src_len >= size) ? size - 1 : src_len; >> +        memcpy(dst, src, len); >> +        dst[len] = '\0'; >> +    } >> +    return src_len; >> +} >> +#endif >> + >>   int >>   utf8_check_printable(const char *start, size_t length) >>   { >> diff --git a/src/trivia/config.h.cmake b/src/trivia/config.h.cmake >> index 89e0d39c6..21efb978e 100644 >> --- a/src/trivia/config.h.cmake >> +++ b/src/trivia/config.h.cmake >> @@ -47,6 +47,11 @@ >>    */ >>   #cmakedefine HAVE_CPUID 1 >>   +/** >> + * Defined if strlcpy() string extension helper present. >> + */ >> + #cmakedefine HAVE_STRLCPY 1 >> + >>   /* >>    * Defined if gcov instrumentation should be enabled. >>    */ >> diff --git a/src/trivia/util.h b/src/trivia/util.h >> index da5a3705e..8dcc48ada 100644 >> --- a/src/trivia/util.h >> +++ b/src/trivia/util.h >> @@ -450,6 +450,21 @@ fpconv_strtod(const char *nptr, char **endptr) >>       return strtod(nptr, endptr); >>   } >>   +#ifndef HAVE_STRLCPY >> +/** >> + * Copy string. Unlike \a strncpy the result string >> + * is always null-terminated. >> + * >> + * @param dst destination buffer. >> + * @param src source string. >> + * @param size destination buffer size. >> + * >> + * @return size of \a src string. >> + */ >> +size_t >> +strlcpy(char *dst, const char *src, size_t size); >> +#endif >> + >>   /** >>    * Check that @a str is valid utf-8 sequence and can be printed >>    * unescaped. > -- Serge Petrenko