diff options
author | Oleksandr Byelkin <sanja@mariadb.com> | 2023-01-31 09:33:58 +0100 |
---|---|---|
committer | Oleksandr Byelkin <sanja@mariadb.com> | 2023-01-31 09:33:58 +0100 |
commit | b923b80cfd8628ef973019bf3dba76dda8d940c4 (patch) | |
tree | 5511aaddb3b3a750cdca5c12906c7ccd58e0b628 /include | |
parent | cea50896d2ea0d18924d92d62a7ec1607d55e509 (diff) | |
parent | c3a5cf2b5bb0c0e1623a0ddc8e3d49557f35e800 (diff) | |
download | mariadb-git-b923b80cfd8628ef973019bf3dba76dda8d940c4.tar.gz |
Merge branch '10.6' into 10.7
Diffstat (limited to 'include')
-rw-r--r-- | include/m_string.h | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/include/m_string.h b/include/m_string.h index 7130a41438f..9e35be404ec 100644 --- a/include/m_string.h +++ b/include/m_string.h @@ -226,6 +226,44 @@ 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. + + 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 +*/ +static inline int safe_strcpy(char *dst, size_t dst_size, const char *src) +{ + memset(dst, '\0', dst_size); + strncpy(dst, src, dst_size - 1); + /* + If the first condition is true, we are guaranteed to have src length + >= (dst_size - 1), hence safe to access src[dst_size - 1]. + */ + if (dst[dst_size - 2] != '\0' && src[dst_size - 1] != '\0') + return 1; /* Truncation of src. */ + return 0; +} + +/* + Appends src to dst and ensures dst is a NULL 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 +*/ +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) + return 1; + return safe_strcpy(dst + init_len, dst_size - init_len, src); +} + #ifdef __cplusplus static inline char *safe_str(char *str) { return str ? str : const_cast<char*>(""); } |