Commit daeccfcf authored by Monty's avatar Monty Committed by Andrew Hutchings

Optimized version of safe_strcpy()

Note: We should replace most case of safe_strcpy() with strmake() to avoid
the not needed zerofill.
parent 620aeb44
...@@ -248,14 +248,15 @@ static inline void lex_string_set3(LEX_CSTRING *lex_str, const char *c_str, ...@@ -248,14 +248,15 @@ static inline void lex_string_set3(LEX_CSTRING *lex_str, const char *c_str,
*/ */
static inline int safe_strcpy(char *dst, size_t dst_size, const char *src) static inline int safe_strcpy(char *dst, size_t dst_size, const char *src)
{ {
memset(dst, '\0', dst_size); DBUG_ASSERT(dst_size > 0);
strncpy(dst, src, dst_size - 1); /* Note, strncpy will zerofill end of dst if src shorter than dst_size */
/* strncpy(dst, src, dst_size);
If the first condition is true, we are guaranteed to have src length if (dst[dst_size-1])
>= (dst_size - 1), hence safe to access src[dst_size - 1]. {
*/ /* Ensure string is zero terminated */
if (dst[dst_size - 2] != '\0' && src[dst_size - 1] != '\0') dst[dst_size-1]= 0;
return 1; /* Truncation of src. */ return 1;
}
return 0; return 0;
} }
......
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