mirror of
https://github.com/MariaDB/server.git
synced 2025-08-07 00:04:31 +03:00
Merge remote-tracking branch 'origin/11.2' into 11.4
This commit is contained in:
@@ -240,15 +240,14 @@ static inline void lex_string_set3(LEX_CSTRING *lex_str, const char *c_str,
|
||||
lex_str->length= len;
|
||||
}
|
||||
|
||||
/*
|
||||
Copies src into dst and ensures dst is a NULL terminated C string.
|
||||
/**
|
||||
Copies a string.
|
||||
|
||||
Returns 1 if the src string was truncated due to too small size of dst.
|
||||
Returns 0 if src completely fit within dst. Pads the remaining dst with '\0'
|
||||
|
||||
Note: dst_size must be > 0
|
||||
@param dst destination buffer, will be NUL padded.
|
||||
@param dst_size size of dst buffer, must be > 0
|
||||
@param src NUL terminated source string
|
||||
*/
|
||||
static inline int safe_strcpy(char *dst, size_t dst_size, const char *src)
|
||||
static inline void safe_strcpy(char *dst, size_t dst_size, const char *src)
|
||||
{
|
||||
DBUG_ASSERT(dst_size > 0);
|
||||
|
||||
@@ -257,45 +256,49 @@ static inline int safe_strcpy(char *dst, size_t dst_size, const char *src)
|
||||
*
|
||||
* 2) IF there is no 0 byte in the first dst_size bytes of src, strncpy will
|
||||
* copy dst_size bytes, and the final byte won't be 0.
|
||||
*
|
||||
* In GCC 8+, the `-Wstringop-truncation` warning will object to strncpy()
|
||||
* being used in this way, so we need to disable this warning for this
|
||||
* single statement.
|
||||
*/
|
||||
|
||||
#if defined(__GNUC__) && __GNUC__ >= 8
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wstringop-truncation"
|
||||
#endif
|
||||
strncpy(dst, src, dst_size);
|
||||
#if defined(__GNUC__) && __GNUC__ >= 8
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
dst[dst_size - 1]= 0;
|
||||
}
|
||||
|
||||
if (dst[dst_size-1])
|
||||
/**
|
||||
Copies a string, checking for truncation.
|
||||
|
||||
@param dst destination buffer, will be NUL padded.
|
||||
@param dst_size size of dst buffer, must be > 0
|
||||
@param src NUL terminated source string
|
||||
|
||||
@retval 1 if the src string was truncated due to too small size of dst.
|
||||
@retval 0 if src completely fit within dst,
|
||||
*/
|
||||
static inline int safe_strcpy_truncated(char *dst, size_t dst_size,
|
||||
const char *src)
|
||||
{
|
||||
DBUG_ASSERT(dst_size > 0);
|
||||
|
||||
strncpy(dst, src, dst_size);
|
||||
if (dst[dst_size - 1])
|
||||
{
|
||||
/* Only possible in case (2), meaning src was truncated. */
|
||||
dst[dst_size-1]= 0;
|
||||
dst[dst_size - 1]= 0;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
Appends src to dst and ensures dst is a NULL terminated C string.
|
||||
/**
|
||||
Appends src to dst and ensures dst is a NUL terminated C string.
|
||||
|
||||
Returns 1 if the src string was truncated due to too small size of dst.
|
||||
Returns 0 if src completely fit within the remaining dst space. Pads the
|
||||
remaining dst with '\0'.
|
||||
|
||||
Note: dst_size must be > 0
|
||||
@retval 1 if the src string was truncated due to too small size of dst.
|
||||
@retval 0 if src completely fit within the remaining dst space,
|
||||
including NUL termination.
|
||||
*/
|
||||
static inline int safe_strcat(char *dst, size_t dst_size, const char *src)
|
||||
{
|
||||
size_t init_len= strlen(dst);
|
||||
if (init_len >= dst_size - 1)
|
||||
if (init_len > dst_size)
|
||||
return 1;
|
||||
return safe_strcpy(dst + init_len, dst_size - init_len, src);
|
||||
return safe_strcpy_truncated(dst + init_len, dst_size - init_len, src);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
Reference in New Issue
Block a user