• Mikhail Chalov's avatar
    Minimize unsafe C functions usage - replace strcat() and strcpy() (and... · 567b6812
    Mikhail Chalov authored
    Minimize unsafe C functions usage - replace strcat() and strcpy() (and strncat() and strncpy()) with custom safe_strcat() and safe_strcpy() functions
    
    The MariaDB code base uses strcat() and strcpy() in several
    places. These are known to have memory safety issues and their usage is
    discouraged. Common security scanners like Flawfinder flags them. In MariaDB we
    should start using modern and safer variants on these functions.
    
    This is similar to memory issues fixes in 19af1890
    and 9de9f105 but now replace use of strcat()
    and strcpy() with safer options strncat() and strncpy().
    
    However, add '\0' forcefully to make sure the result string is correct since
    for these two functions it is not guaranteed what new string will be null-terminated.
    
    Example:
    
        size_t dest_len = sizeof(g->Message);
        strncpy(g->Message, "Null json tree", dest_len); strncat(g->Message, ":",
        sizeof(g->Message) - strlen(g->Message)); size_t wrote_sz = strlen(g->Message);
        size_t cur_len = wrote_sz >= dest_len ? dest_len - 1 : wrote_sz;
        g->Message[cur_len] = '\0';
    
    All new code of the whole pull request, including one or several files
    that are either new files or modified ones, are contributed under the BSD-new
    license. I am contributing on behalf of my employer Amazon Web Services
    
    -- Reviewer and co-author Vicențiu Ciorbaru <vicentiu@mariadb.org>
    -- Reviewer additions:
    * The initial function implementation was flawed. Replaced with a simpler
      and also correct version.
    * Simplified code by making use of snprintf instead of chaining strcat.
    * Simplified code by removing dynamic string construction in the first
      place and using static strings if possible. See connect storage engine
      changes.
    567b6812
mysqld.cc 362 KB