Skip to content

Commit 44b0c86

Browse files
committed
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.
1 parent 8145347 commit 44b0c86

File tree

4 files changed

+15
-73
lines changed

4 files changed

+15
-73
lines changed

storage/innobase/include/ut0mem.h

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*****************************************************************************
22
33
Copyright (c) 1994, 2016, Oracle and/or its affiliates. All Rights Reserved.
4+
Copyright (c) 2019, MariaDB Corporation.
45
56
This program is free software; you can redistribute it and/or modify it under
67
the terms of the GNU General Public License as published by the Free Software
@@ -83,29 +84,6 @@ UNIV_INLINE
8384
int
8485
ut_strcmp(const char* str1, const char* str2);
8586

86-
/**********************************************************************//**
87-
Copies up to size - 1 characters from the NUL-terminated string src to
88-
dst, NUL-terminating the result. Returns strlen(src), so truncation
89-
occurred if the return value >= size.
90-
@return strlen(src) */
91-
ulint
92-
ut_strlcpy(
93-
/*=======*/
94-
char* dst, /*!< in: destination buffer */
95-
const char* src, /*!< in: source buffer */
96-
ulint size); /*!< in: size of destination buffer */
97-
98-
/**********************************************************************//**
99-
Like ut_strlcpy, but if src doesn't fit in dst completely, copies the last
100-
(size - 1) bytes of src, not the first.
101-
@return strlen(src) */
102-
ulint
103-
ut_strlcpy_rev(
104-
/*===========*/
105-
char* dst, /*!< in: destination buffer */
106-
const char* src, /*!< in: source buffer */
107-
ulint size); /*!< in: size of destination buffer */
108-
10987
/********************************************************************
11088
Concatenate 3 strings.*/
11189
char*

storage/innobase/mem/mem0mem.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,17 @@ mem_heap_validate(
240240

241241
ut_ad(size == heap->total_size);
242242
}
243+
244+
/** Copy the tail of a string.
245+
@param[in,out] dst destination buffer
246+
@param[in] src string whose tail to copy
247+
@param[in] size size of dst buffer, in bytes, including NUL terminator
248+
@return strlen(src) */
249+
static void ut_strlcpy_rev(char* dst, const char* src, ulint size)
250+
{
251+
size_t src_size = strlen(src), n = std::min(src_size, size - 1);
252+
memcpy(dst, src + src_size - n, n + 1);
253+
}
243254
#endif /* UNIV_DEBUG */
244255

245256
/***************************************************************//**

storage/innobase/trx/trx0trx.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ trx_set_detailed_error(
7272
trx_t* trx, /*!< in: transaction struct */
7373
const char* msg) /*!< in: detailed error message */
7474
{
75-
ut_strlcpy(trx->detailed_error, msg, MAX_DETAILED_ERROR_LEN);
75+
strncpy(trx->detailed_error, msg, MAX_DETAILED_ERROR_LEN - 1);
76+
trx->detailed_error[MAX_DETAILED_ERROR_LEN - 1] = '\0';
7677
}
7778

7879
/*************************************************************//**

storage/innobase/ut/ut0mem.cc

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*****************************************************************************
22
33
Copyright (c) 1994, 2016, Oracle and/or its affiliates. All Rights Reserved.
4+
Copyright (c) 2019, MariaDB Corporation.
45
56
This program is free software; you can redistribute it and/or modify it under
67
the terms of the GNU General Public License as published by the Free Software
@@ -24,55 +25,6 @@ Created 5/11/1994 Heikki Tuuri
2425
*************************************************************************/
2526

2627
#include "ut0mem.h"
27-
#include "os0thread.h"
28-
#include "srv0srv.h"
29-
#include <stdlib.h>
30-
31-
/**********************************************************************//**
32-
Copies up to size - 1 characters from the NUL-terminated string src to
33-
dst, NUL-terminating the result. Returns strlen(src), so truncation
34-
occurred if the return value >= size.
35-
@return strlen(src) */
36-
ulint
37-
ut_strlcpy(
38-
/*=======*/
39-
char* dst, /*!< in: destination buffer */
40-
const char* src, /*!< in: source buffer */
41-
ulint size) /*!< in: size of destination buffer */
42-
{
43-
ulint src_size = strlen(src);
44-
45-
if (size != 0) {
46-
ulint n = ut_min(src_size, size - 1);
47-
48-
memcpy(dst, src, n);
49-
dst[n] = '\0';
50-
}
51-
52-
return(src_size);
53-
}
54-
55-
/**********************************************************************//**
56-
Like ut_strlcpy, but if src doesn't fit in dst completely, copies the last
57-
(size - 1) bytes of src, not the first.
58-
@return strlen(src) */
59-
ulint
60-
ut_strlcpy_rev(
61-
/*===========*/
62-
char* dst, /*!< in: destination buffer */
63-
const char* src, /*!< in: source buffer */
64-
ulint size) /*!< in: size of destination buffer */
65-
{
66-
ulint src_size = strlen(src);
67-
68-
if (size != 0) {
69-
ulint n = ut_min(src_size, size - 1);
70-
71-
memcpy(dst, src + src_size - n, n + 1);
72-
}
73-
74-
return(src_size);
75-
}
7628

7729
/********************************************************************
7830
Concatenate 3 strings.*/

0 commit comments

Comments
 (0)