Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix string escaping #2

Merged
merged 3 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
# Ignore bazel target dirs
bazel-*
# Ignore all bazel-* symlinks. There is no full list since this can change
# based on the name of the directory bazel is cloned into.
/bazel-*

# Build Artifacts
build/
dist/
*.egg-info/
*__pycache__/
metadata_store_extension.so

# Compiled grpc/protobuf files
*_pb2.py
*_pb2_grpc.py
6 changes: 3 additions & 3 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ http_archive(
name = "postgresql",
build_file = "//ml_metadata:postgresql.BUILD",
workspace_file_content = "//ml_metadata:postgresql.WORKSPACE",
sha256 = "9868c1149a04bae1131533c5cbd1c46f9c077f834f6147abaef8791a7c91b1a1",
strip_prefix = "postgresql-12.1",
sha256 = "1cb8e3a59861be5175878159fc3a41240c379e9aabaabba8288e6cfd6980fff0",
strip_prefix = "postgresql-12.17",
urls = [
"https://ftp.postgresql.org/pub/source/v12.1/postgresql-12.1.tar.gz",
"https://ftp.postgresql.org/pub/source/v12.17/postgresql-12.17.tar.gz",
],
)

Expand Down
2 changes: 1 addition & 1 deletion ml_metadata/metadata_store/mysql_metadata_source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ std::string MySqlMetadataSource::EscapeString(absl::string_view value) const {
CHECK(mysql_real_escape_string(db_, buffer, value.data(), value.length()) !=
-1UL)
<< "NO_BACKSLASH_ESCAPES SQL mode should not be enabled.";
std::string result(buffer);
std::string result = absl::StrCat("'", buffer, "'");
delete[] buffer;
return result;
}
Expand Down
6 changes: 1 addition & 5 deletions ml_metadata/metadata_store/postgresql_metadata_source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,8 @@ std::string PostgreSQLMetadataSource::EscapeString(

char* escaped_str = PQescapeLiteral(conn_, value.data(), value.size());
std::string result{escaped_str};
// PQescapeLiteral will wrap the escaped string in '', which is redundant to
// the existing MLMD syntax. Therefore stripping the outer '' from the escaped
// string.
std::string substring = result.substr(1, std::strlen(result.data()) - 2);
PQfreemem(escaped_str);
return substring;
return result;
}

std::string PostgreSQLMetadataSource::EncodeBytes(
Expand Down
8 changes: 4 additions & 4 deletions ml_metadata/metadata_store/postgresql_query_executor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -374,10 +374,10 @@ absl::Status PostgreSQLQueryExecutor::DowngradeMetadataSource(
return absl::OkStatus();
}
std::string PostgreSQLQueryExecutor::Bind(const char* value) {
return absl::StrCat("'", metadata_source_->EscapeString(value), "'");
return metadata_source_->EscapeString(value);
}
std::string PostgreSQLQueryExecutor::Bind(absl::string_view value) {
return absl::StrCat("'", metadata_source_->EscapeString(value), "'");
return metadata_source_->EscapeString(value);
}
std::string PostgreSQLQueryExecutor::Bind(int value) {
return std::to_string(value);
Expand All @@ -390,10 +390,10 @@ std::string PostgreSQLQueryExecutor::Bind(double value) {
}
std::string PostgreSQLQueryExecutor::Bind(const google::protobuf::Any& value) {
return absl::StrCat(
"decode('",
"decode(",
metadata_source_->EscapeString(
metadata_source_->EncodeBytes(value.SerializeAsString())),
"', 'base64')");
", 'base64')");
}
std::string PostgreSQLQueryExecutor::Bind(bool value) {
return value ? "TRUE" : "FALSE";
Expand Down
4 changes: 2 additions & 2 deletions ml_metadata/metadata_store/query_config_executor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,11 @@ absl::Status QueryConfigExecutor::DowngradeMetadataSource(
}

std::string QueryConfigExecutor::Bind(const char* value) {
return absl::StrCat("'", metadata_source_->EscapeString(value), "'");
return metadata_source_->EscapeString(value);
}

std::string QueryConfigExecutor::Bind(absl::string_view value) {
return absl::StrCat("'", metadata_source_->EscapeString(value), "'");
return metadata_source_->EscapeString(value);
}

std::string QueryConfigExecutor::Bind(int value) {
Expand Down
8 changes: 4 additions & 4 deletions ml_metadata/postgresql.BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -1176,7 +1176,7 @@ genrule(
"#define PACKAGE_NAME \"PostgreSQL\"",
"",
"/* Define to the full name and version of this package. */",
"#define PACKAGE_STRING \"PostgreSQL 12.1\"",
"#define PACKAGE_STRING \"PostgreSQL 12.17\"",
"",
"/* Define to the one symbol short name of this package. */",
"#define PACKAGE_TARNAME \"postgresql\"",
Expand All @@ -1185,7 +1185,7 @@ genrule(
"#define PACKAGE_URL \"\"",
"",
"/* Define to the version of this package. */",
"#define PACKAGE_VERSION \"12.1\"",
"#define PACKAGE_VERSION \"12.17\"",
"",
"/* Define to the name of a signed 128-bit integer type. */",
"#define PG_INT128_TYPE __int128",
Expand All @@ -1204,13 +1204,13 @@ genrule(
"#define PG_PRINTF_ATTRIBUTE printf",
"",
"/* PostgreSQL version as a string */",
"#define PG_VERSION \"12.1\"",
"#define PG_VERSION \"12.17\"",
"",
"/* PostgreSQL version as a number */",
"#define PG_VERSION_NUM 120001",
"",
"/* A string containing the version number, platform, and C compiler */",
"#define PG_VERSION_STR \"PostgreSQL 12.1 on x86_64-apple-darwin19.2.0, compiled by Apple clang version 11.0.0 (clang-1100.0.33.17), 64-bit\"",
"#define PG_VERSION_STR \"PostgreSQL 12.17 on x86_64-apple-darwin19.2.0, compiled by Apple clang version 11.0.0 (clang-1100.0.33.17), 64-bit\"",
"",
"/* Define to 1 to allow profiling output to be saved separately for each",
" process. */",
Expand Down