Skip to content

Commit

Permalink
Clean up ut_strlcpy(), ut_strlcpy_rev()
Browse files Browse the repository at this point in the history
ut_strlcpy(): Replace with the standard function strncpy().

ut_strlcpy_rev(): Define in the same compilation unit where
the only caller resides. Avoid unnecessary definition
in non-debug builds.
  • Loading branch information
dr-m committed Oct 30, 2019
1 parent 8145347 commit 44b0c86
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 73 deletions.
24 changes: 1 addition & 23 deletions storage/innobase/include/ut0mem.h
@@ -1,6 +1,7 @@
/*****************************************************************************
Copyright (c) 1994, 2016, Oracle and/or its affiliates. All Rights Reserved.
Copyright (c) 2019, MariaDB Corporation.
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Expand Down Expand Up @@ -83,29 +84,6 @@ UNIV_INLINE
int
ut_strcmp(const char* str1, const char* str2);

/**********************************************************************//**
Copies up to size - 1 characters from the NUL-terminated string src to
dst, NUL-terminating the result. Returns strlen(src), so truncation
occurred if the return value >= size.
@return strlen(src) */
ulint
ut_strlcpy(
/*=======*/
char* dst, /*!< in: destination buffer */
const char* src, /*!< in: source buffer */
ulint size); /*!< in: size of destination buffer */

/**********************************************************************//**
Like ut_strlcpy, but if src doesn't fit in dst completely, copies the last
(size - 1) bytes of src, not the first.
@return strlen(src) */
ulint
ut_strlcpy_rev(
/*===========*/
char* dst, /*!< in: destination buffer */
const char* src, /*!< in: source buffer */
ulint size); /*!< in: size of destination buffer */

/********************************************************************
Concatenate 3 strings.*/
char*
Expand Down
11 changes: 11 additions & 0 deletions storage/innobase/mem/mem0mem.cc
Expand Up @@ -240,6 +240,17 @@ mem_heap_validate(

ut_ad(size == heap->total_size);
}

/** Copy the tail of a string.
@param[in,out] dst destination buffer
@param[in] src string whose tail to copy
@param[in] size size of dst buffer, in bytes, including NUL terminator
@return strlen(src) */
static void ut_strlcpy_rev(char* dst, const char* src, ulint size)
{
size_t src_size = strlen(src), n = std::min(src_size, size - 1);
memcpy(dst, src + src_size - n, n + 1);
}
#endif /* UNIV_DEBUG */

/***************************************************************//**
Expand Down
3 changes: 2 additions & 1 deletion storage/innobase/trx/trx0trx.cc
Expand Up @@ -72,7 +72,8 @@ trx_set_detailed_error(
trx_t* trx, /*!< in: transaction struct */
const char* msg) /*!< in: detailed error message */
{
ut_strlcpy(trx->detailed_error, msg, MAX_DETAILED_ERROR_LEN);
strncpy(trx->detailed_error, msg, MAX_DETAILED_ERROR_LEN - 1);
trx->detailed_error[MAX_DETAILED_ERROR_LEN - 1] = '\0';
}

/*************************************************************//**
Expand Down
50 changes: 1 addition & 49 deletions storage/innobase/ut/ut0mem.cc
@@ -1,6 +1,7 @@
/*****************************************************************************
Copyright (c) 1994, 2016, Oracle and/or its affiliates. All Rights Reserved.
Copyright (c) 2019, MariaDB Corporation.
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Expand All @@ -24,55 +25,6 @@ Created 5/11/1994 Heikki Tuuri
*************************************************************************/

#include "ut0mem.h"
#include "os0thread.h"
#include "srv0srv.h"
#include <stdlib.h>

/**********************************************************************//**
Copies up to size - 1 characters from the NUL-terminated string src to
dst, NUL-terminating the result. Returns strlen(src), so truncation
occurred if the return value >= size.
@return strlen(src) */
ulint
ut_strlcpy(
/*=======*/
char* dst, /*!< in: destination buffer */
const char* src, /*!< in: source buffer */
ulint size) /*!< in: size of destination buffer */
{
ulint src_size = strlen(src);

if (size != 0) {
ulint n = ut_min(src_size, size - 1);

memcpy(dst, src, n);
dst[n] = '\0';
}

return(src_size);
}

/**********************************************************************//**
Like ut_strlcpy, but if src doesn't fit in dst completely, copies the last
(size - 1) bytes of src, not the first.
@return strlen(src) */
ulint
ut_strlcpy_rev(
/*===========*/
char* dst, /*!< in: destination buffer */
const char* src, /*!< in: source buffer */
ulint size) /*!< in: size of destination buffer */
{
ulint src_size = strlen(src);

if (size != 0) {
ulint n = ut_min(src_size, size - 1);

memcpy(dst, src + src_size - n, n + 1);
}

return(src_size);
}

/********************************************************************
Concatenate 3 strings.*/
Expand Down

0 comments on commit 44b0c86

Please sign in to comment.