Commit ced3ec4c authored by Marko Mäkelä's avatar Marko Mäkelä

Revert MDEV-20453 (string_view)

In fsp_path_to_space_name(), we would access a byte right before
the start of the string, tripping AddressSanitizer.

This reverts commit d87006a1
and commit a7634281.
parent 9936cfd5
This diff is collapsed.
......@@ -143,7 +143,7 @@ SET (SQL_SOURCE
opt_trace.cc
${WSREP_SOURCES}
table_cache.cc encryption.cc temporary_tables.cc
proxy_protocol.cc backup.cc xa.cc string_view.cc
proxy_protocol.cc backup.cc xa.cc
${CMAKE_CURRENT_BINARY_DIR}/sql_builtin.cc
${CMAKE_CURRENT_BINARY_DIR}/sql_yacc.cc
${CMAKE_CURRENT_BINARY_DIR}/sql_yacc_ora.cc
......
/*****************************************************************************
Copyright (c) 2020 MariaDB Corporation.
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA
*****************************************************************************/
#include "string_view.h"
#include <ostream>
std::basic_ostream<char> &operator<<(std::basic_ostream<char> &os,
string_view v)
{
// TODO standard requires a much more complicated code here.
auto size= static_cast<std::streamsize>(v.size());
os.write(v.data(), size);
return os;
}
......@@ -3487,6 +3487,53 @@ fil_space_read_name_and_filepath(
return(success);
}
/** Convert a file name to a tablespace name.
@param[in] filename directory/databasename/tablename.ibd
@return database/tablename string, to be freed with ut_free() */
char*
fil_path_to_space_name(
const char* filename)
{
/* Strip the file name prefix and suffix, leaving
only databasename/tablename. */
ulint filename_len = strlen(filename);
const char* end = filename + filename_len;
#ifdef HAVE_MEMRCHR
const char* tablename = 1 + static_cast<const char*>(
memrchr(filename, OS_PATH_SEPARATOR,
filename_len));
const char* dbname = 1 + static_cast<const char*>(
memrchr(filename, OS_PATH_SEPARATOR,
tablename - filename - 1));
#else /* HAVE_MEMRCHR */
const char* tablename = filename;
const char* dbname = NULL;
while (const char* t = static_cast<const char*>(
memchr(tablename, OS_PATH_SEPARATOR,
ulint(end - tablename)))) {
dbname = tablename;
tablename = t + 1;
}
#endif /* HAVE_MEMRCHR */
ut_ad(dbname != NULL);
ut_ad(tablename > dbname);
ut_ad(tablename < end);
ut_ad(end - tablename > 4);
ut_ad(memcmp(end - 4, DOT_IBD, 4) == 0);
char* name = mem_strdupl(dbname, ulint(end - dbname) - 4);
ut_ad(name[tablename - dbname - 1] == OS_PATH_SEPARATOR);
#if OS_PATH_SEPARATOR != '/'
/* space->name uses '/', not OS_PATH_SEPARATOR. */
name[tablename - dbname - 1] = '/';
#endif
return(name);
}
/** Discover the correct IBD file to open given a remote or missing
filepath from the REDO log. Administrators can move a crashed
database to another location on the same machine and try to recover it.
......
......@@ -30,7 +30,6 @@ Created 2013-7-26 by Kevin Lewis
#include "page0page.h"
#include "srv0start.h"
#include "string_view.h"
/** Initialize the name, size and order of this datafile
@param[in] name tablespace name, will be copied
@param[in] flags tablespace flags */
......@@ -259,28 +258,6 @@ Datafile::same_as(
#endif /* WIN32 */
}
/** Convert a file name to a tablespace name.
@param[in] filename directory/databasename/tablename.ibd
@return database/tablename string, to be freed with ut_free() */
static char *fsp_path_to_space_name(string_view filename)
{
auto last_slash= filename.rfind(OS_PATH_SEPARATOR);
auto prev_last_slash=
filename.substr(0, last_slash).rfind(OS_PATH_SEPARATOR);
filename.remove_prefix(prev_last_slash + 1);
ut_ad(filename.ends_with(DOT_IBD));
filename.remove_suffix(strlen(DOT_IBD));
char *name= mem_strdupl(filename.data(), filename.size());
#if OS_PATH_SEPARATOR != '/'
/* space->name uses '/', not OS_PATH_SEPARATOR. */
name[last_slash - prev_last_slash - 1]= '/';
#endif
return name;
}
/** Allocate and set the datafile or tablespace name in m_name.
If a name is provided, use it; else extract a file-per-table
tablespace name from m_filepath. The value of m_name
......@@ -294,7 +271,7 @@ Datafile::set_name(const char* name)
if (name != NULL) {
m_name = mem_strdup(name);
} else {
m_name = fsp_path_to_space_name(m_filepath);
m_name = fil_path_to_space_name(m_filepath);
}
}
......
......@@ -1495,6 +1495,13 @@ fil_space_read_name_and_filepath(
char** name,
char** filepath);
/** Convert a file name to a tablespace name.
@param[in] filename directory/databasename/tablename.ibd
@return database/tablename string, to be freed with ut_free() */
char*
fil_path_to_space_name(
const char* filename);
/** Generate redo log for swapping two .ibd files
@param[in] old_table old table
@param[in] new_table new table
......
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