Commit 44b0c869 authored by Marko Mäkelä's avatar Marko Mäkelä

Clean up ut_strlcpy(), ut_strlcpy_rev()

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.
parent 81453474
/***************************************************************************** /*****************************************************************************
Copyright (c) 1994, 2016, Oracle and/or its affiliates. All Rights Reserved. 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 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 the terms of the GNU General Public License as published by the Free Software
...@@ -83,29 +84,6 @@ UNIV_INLINE ...@@ -83,29 +84,6 @@ UNIV_INLINE
int int
ut_strcmp(const char* str1, const char* str2); 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.*/ Concatenate 3 strings.*/
char* char*
......
...@@ -240,6 +240,17 @@ mem_heap_validate( ...@@ -240,6 +240,17 @@ mem_heap_validate(
ut_ad(size == heap->total_size); 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 */ #endif /* UNIV_DEBUG */
/***************************************************************//** /***************************************************************//**
......
...@@ -72,7 +72,8 @@ trx_set_detailed_error( ...@@ -72,7 +72,8 @@ trx_set_detailed_error(
trx_t* trx, /*!< in: transaction struct */ trx_t* trx, /*!< in: transaction struct */
const char* msg) /*!< in: detailed error message */ 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';
} }
/*************************************************************//** /*************************************************************//**
......
/***************************************************************************** /*****************************************************************************
Copyright (c) 1994, 2016, Oracle and/or its affiliates. All Rights Reserved. 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 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 the terms of the GNU General Public License as published by the Free Software
...@@ -24,55 +25,6 @@ Created 5/11/1994 Heikki Tuuri ...@@ -24,55 +25,6 @@ Created 5/11/1994 Heikki Tuuri
*************************************************************************/ *************************************************************************/
#include "ut0mem.h" #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.*/ Concatenate 3 strings.*/
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment